Run R Code in SAS

After setting up SAS to run R code, now we should be able to run R code inside SAS. It is achieved by running submit / R inside proc iml:

However, the SAS session and the R session do not automatically share data between each other.

The SAS function to send data from the SAS session to the R session is ExportDataSetToR(SASDatasetName, RDataframeName). For example, the following code sends the “SASHELP.class” SAS dataset to the R session, and it becomes a dataframe with the name “df”. And then the R code prints the dataframe in the R session. The R output is then captured and printed in the SAS session.

The SAS function to send data from the R session to the SAS session is ImportDataSetFromR(SASDatasetName, RDataframeName). For example, the following code loads the built-in R data “mtcars” in the R session, and then send it to the SAS session, and it becomes the SAS dataset “sas_data” in the Work library.

Put these two pieces together, a simple workflow can look like this:

One thought on “Run R Code in SAS

Leave a Reply