Writing
Run R Code in SAS
SAS can run R code inside a SAS program, but only after R support is enabled. The setup has two parts: turn on R integration in SAS, and tell SAS where R is installed.
Set Up R Support in SAS
First, launch SAS with R support enabled. You can do this in either of two ways:
- Add
-RLANGto the SAS command line. - Add
-RLANGto the end of the SAS configuration file,sasv9.cfg.
Second, tell SAS where to find R. You can do this in either of two ways:
- Add
options set=R_HOME='C:\Program Files\R\R-x.y.z';toautoexec.sas. - Add
-SET R_HOME "C:\Program Files\R\R-x.y.z"to the end ofsasv9.cfg.
Change the R path to match the version installed on your machine.
After that, run proc options option=RLANG; run; to confirm that R support is enabled in SAS.
If you prefer not to edit sasv9.cfg, the simplest setup is:
- Add
-RLANGto the SAS command line. - Add the following two lines to
autoexec.sas:options set=R_HOME='C:\Program Files\R\R-x.y.z';Change this path to match your R installation.proc options option=RLANG; run;This lets you confirm at startup that SAS is ready to run R.
Run R Code Through PROC IML
After SAS is configured with R support, you can run R code through PROC IML. The basic pattern is to open a submit / R block, write the R code inside that block, and then return to SAS.
For example, the following SAS program creates a small data frame in R and then imports it back into SAS:
proc iml;
submit / R;
df = data.frame(x = 1:3, y = c("a", "b", "c"))
endsubmit;
run ImportDataSetFromR("sas_data", "df");
run;
If you only need to run analysis in R and inspect the printed output, the submit / R block may be enough. If you need to continue working with the result in SAS, use ImportDataSetFromR() to bring an R object back as a SAS dataset.
The key setup requirements are -RLANG, which enables R integration, and R_HOME, which tells SAS where R is installed. Once both are set, the submit / R block gives SAS a direct way to call R and bring results back into the SAS session.