The SAS Analysis blog post ‘A macro calls R in SAS for paneled 3d plotting’ influenced my macro coding. The following macro call:
%EXPORT_TO_R(DATA = YOURDATA)
exports the SAS data set ‘YOURDATA’ as .csv and produces the R code for setting the R working directory and reading the file.
R-code written by %EXPORT_TO_R :
# set R working directory setwd("C:\\Documents and Settings\\wkuuser\\Desktop\\PROJECTS\\Stats Training") # get data dat.from.SAS <- read.csv("fromSAS_delete.CSV", header=T) # check data dimensions dim(dat.from.SAS) names(dat.from.SAS)
Actual SAS code is below.
An application of this can be found here.
One potential drawback is that the file path for where you are exporting the data and the R code is hard coded in the macro. When I'm typically working in SAS and need this capability, I typically have a single 'development' folder for ad hoc support work in R. Ultimately when I've finished the program, I save the final R code in the specific project directory I'm working in. So it's not a big deal once the macro is set up for the computer you are using.
There are many times where you may be in the SAS environment and require a specific statistical capability (like spatial regression ) that is in R or perhaps you don't have liscence for in SAS (like decision trees with Enterprise Miner). Being able to quickly move into the R environment is nice. I've worked with submitting R code within the SAS IML environment, & it works great. However, its another system to start and run (outside of base SAS as I prefer the base SAS text editor over the IML environment). Updates to SAS PROC IML are supposed to deliver this capability within the base SAS environment, but I have not been able to get it to work. (I believe there are some timing issues with the SAS version and compatability with recent releases of R).
SAS code for macro: