Change Font in SAS Windows

Change the font of the code editor: Tools -> Options -> Enhanced Editor.

Change the font of the data table view: Add -SYSGUIFONT "Consolas" NORMAL 11 to the SAS command line.

Change the font of the treeview and menu, etc. : This is controlled by Windows, not by SAS. If you want to increase text size without making everything bigger, see Change the size of text in Windows 10. This seems crash SAS Windows on my machine…

Export R data to SAS

The Problem

Import data from SAS to R is easy. Library haven has the read_sas() function to import *.sas7bdat files to R data.frame. However, the *.sas7bdat files create by the write_sas() function mostly won’t be able to be read by SAS.

It seems the alternative way of transferring data from R to SAS should be easy: just find a common data format supported by both R and SAS. For example, R can export data to CSV, and SAS can import CSV. Wouldn’t that be easy?

Actually, no. Accurately importing CSV in SAS is never an easy task. SAS has to guess the type of data for each column, and weird data cells (quotes, commas, special characters, etc.) screw up the importing process all the time. How about other data formats?

Excel? It could be even worse than CSV.

How about SQL server (MySQL, MS SQL, etc.) then? Do you really want to setup a SQL server just for this?

If SAS supports the CSV format with YAML frontmatter, that will certainly help a lot, but it doesn’t.

The Solution

So, the only sane solution is to run R code in SAS. Let the R code read the R data, and then use the ImportDataSetFromR() function to transfer the R data.frame to a SAS dataset. The SAS code would look like this:

If you use the CSVY format in R, the 3rd line would be something like this:

This is a fairly simple solution, and unlike haven::write_sas(), it has a 100% success rate.

Yes, this solution requires the SAS software on the machine. But I guess most people need to use SAS data should already have SAS installed on their machines.

The R Workflow Solution

One thing I don’t like the above solution is that it needs to run a SAS script, which disrupts my workflow.

When I need to export R data to SAS, that’s because I am working on a R project, and at the end of the process, I need to export my R data products to share with colleagues who only use SAS.

I could open SAS and run the above SAS script. But I don’t like that. Since I am already using R, why don’t I just run everything in R? It is certainly doable because we can run SAS script in R.

What I will need first is a SAS script, which will have the code from the above solution and will be named as “ImportFromR.sas”. And then, I will call this script from R use the following code:

Two notes:

  • “-RLANG” is added to the SAS command line call because I don’t use it in the SAS config file.
  • “R_HOME” need to be assigned in either the SAS config file, or the SAS autoexec file, or the “ImportFromR.sas” file. See Setup SAS to Run R Code inside SAS.

In addition, to make the entire process adaptable, you can write a R function which creates the SAS script “ImportFromR.sas” (so that it contains the real path of the R data), and then call this SAS script. Following pseudo code demonstrates the general idea:

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.

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:

Setup SAS to Run R Code inside SAS

If you have a modern version of SAS and an up-to-date version of R on Windows, you are mostly likely to be able to run R code inside SAS. All you need to do for the setup is in the following two steps:

First, tell SAS to launch with the R functionality turned on. You can achieve this by either:

  • a) add -RLANG┬áto the SAS command line, or
  • b) add -RLANG to the end of the SAS config file (sasv9.cfg).

Second, tell SAS where to find R. You can achieve this by either:

  • a) add options set=R_HOME='C:\Program Files\R\R-3.6.2'; to your autoexec.sas, or
  • b) add -SET R_HOME "C:\Program Files\R\R-3.6.2" to the end of the SAS config file (sasv9.cfg).

Please change the R path according to the R version installed on your machine.

Now, you can run proc options option=RLANG; run; to check whether the R functionality has been turned on in SAS.

In summary, if you prefer not to change the SAS config file (sasv9.cfg), you only need to do the following two things:

  1. add -RLANG to the SAS command line,
  2. add the following two lines to your autoexec.sas:
    • options set=R_HOME='C:\Program Files\R\R-3.6.2'; . Please change the R path according to the R version installed on your machine.
    • proc options option=RLANG; run; . So that you can always see whether SAS is ready to run R when SAS is launched.