Introduction

There are two methods for calling R from SAS; in batch mode as described in (Holland, Wei) and via the PROC IML interface provides by SAS. This tutorial will only go through the setting up for the second method, follows by some usage examples.

Requirements

Check that SAS and R are installed on the same machine (server), then run (PROC SETINIT; RUN;) to see if SAS IML is licensed.

Settings

For SAS to communicate with R, the RLANG option and the R_HOME environment variable need setting up. Both can be done in the config file (sasv9.cfg) but an alternative is shown here.

Figure 1

To check that the RLANG option is set, you can run:

PROC OPTIONS OPTION=RLANG; RUN;

And if successful, you should see this message in the log window that says RLANG option is enabled.

RLANG Enables SAS to execute R language statements.

The R_HOME environment variable

You can specify this option in your SAS program as shown here (change to the path where your R is stored accordingly).

options set=R_HOME=“C:\Progra~1\R\R-4.2.2\bin”;

Running R Codes within SAS Codes

Now how do we run the R codes? Simply put the R codes between the SUBMIT and ENDSUBMIT block within the PROC IML procedure. This example will print the variables in the mtcars data frame to the screen.

Example 1

proc iml;
  submit / R;
    df <- mtcars
    df
  endsubmit;
quit;

                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
...
...

   

If when running Example 1 gives the ERROR shown in Figure 2 then you may have to copy C:\Progra~1\R\R-4.2.2\bin\R.exe to C:\Progra~1\R\R-4.2.2\R.exe

Figure 2


The next two examples show what are often required in practice; that is the exchange of data between the two applications, and PROC IML conveniently provides two functions (ExportDataSetToR, ImportDataSetFromR) to do this.

Example 2 - Exporting SAS data to R

proc iml;
  /* SAS dataset (Sashelp.Class) is being exported to R data frame (dfclass) */
  call ExportDataSetToR("Sashelp.Class", "dfclass");
  submit / R;
    library(dplyr)
    dfclass %>%
    summarize(n   = n(),
             mean = mean(Age, na.rm = TRUE),
             min  = min(Age),
             max  = max(Age)
             )
  endsubmit;
quit;

n    mean      min   max
19   13.31579  11    16
Example 3 - Importing data from R

proc iml;
  submit / R;
     library(base)
     df <- mtcars
  endsubmit;
  /* R data frame (df) is being imported to SAS dataset (dset) */
  call ImportDataSetFromR("odset", "df");
run;
quit;

proc means data=odset;
  var mpg wt;
run;

Variable     N            Mean         Std Dev         Minimum         Maximum
==============================================================================
mpg         32      20.0906250       6.0269481      10.4000000      33.9000000
wt          32       3.2172500       0.9784574       1.5130000       5.4240000
==============================================================================


Another often desires function is the passing of parameters (macro variables). This is allowed, though rather cumbersomely implemented in PROC IML as you can see in this example.

Example 4 - Passing macro variables

proc iml;
  /* you can use the macro language here */
  pkg = "base";
  var = "mpg";
  submit pkg var / R;
     /* you cannot use the macro language here */
     library(&pkg)
  endsubmit;
  call ImportDataSetFromR("odset", "mtcars$&var");
run;
quit;

proc means data=odset;
run;
  
N           Mean         Std Dev         Minimum         Maximum
==================================================================
32      20.0906250       6.0269481      10.4000000      33.9000000
==================================================================


Note, while you can pass macro variables into the SUBMIT / ENDSUBMIT block, you cannot use the macro language inside this block, though it is permissible outside of this block.

 

Example 5 - Submit block inside a macro definition

This is NOT allows so to get round this, saved the submit block or section of codes in a separate file and
include it with the "%inc" statement as shown below.

submit_block.sas
================
submit / R;
  ...
endsubmit;


%marco IML;
  proc iml;
    ...
    %inc "submit_block.sas";
    ...
  run;
  quit;
%mend;
  

tempfile.sas
============
%marco IML(param=);
  data _null_;
    file "tempfile.sas";
    put @1 "proc iml;                     ";
    put @1 "  submit / R;                 ";   
    put @1 "                              ";
    put @1 "    whatever &param here!!    ";
    put @1 "    note: macro language and  ";
    put @1 "    macro variables are allow ";
    put @1 "    per usual here            ";
    put @1 "                              ";
    put @1 "  endsubmit;                  ";
    put @1 "run;                          ";
    put @1 "quit;                         ";
  run;
  %inc "tempfile.SAS";  
%mend IML;

%IML(param=SomeValue);

 

Reference

[1] Holland P (2008). SAS to R to SAS https://www.lexjansen.com/phuse/2005/cc/cc03.pdf

[2] Xin Wei (2012). %PROC_R: A SAS Macro that Enables Native R Programming in the Base SAS Environment https://www.jstatsoft.org/article/view/v046c02



Contact

Email: