Run SAS Script in R

Unlike SAS, which provides designated syntax to run a R session alongside the SAS session, R does not have the capability to run a SAS session.

However, R has the function system() to invoke any command from the OS. On the other hand, SAS provides a functionality to allow us to run a SAS script through command line. By putting those two together, we can use R to call the SAS command to run a SAS script.

The command line syntax to call SAS to run a SAS script is:

All we need to do is call the above command from the R system() function. The R code will look like:

Notice two small changes:

  1. The whole command is put within the single quotes because we use double quotes in the command, which is because the paths could have spaces in it.
  2. Backslashes \ are escaped with additional backslashes for the R syntax.

The above code is hard to modify and expand. We can make things easier by splitting the pieces and paste them back together.

Using the above syntax, we can add more to the SAS command:

Note: the SAS installation path can be different, please check if the above code does not run on your machine.

One thought on “Run SAS Script in R

Leave a Reply