Search code examples
rsassas-macro

How to pass an %LET argument into a sas script from R?


I have an R script that let me run a macro in SAS, like this (inspiration from here):

setwd("C:/myplace")
sas_script <- "MyScript.sas"
sas_log <- tempfile()
sas_out <- tempfile()


cmd <- sprintf(
  'sas.exe -nosplash -icon -sysin "%s"  -log "%s" -print "%s"',
  sas_script, sas_log, sas_out
)

return_code  <- system(cmd)  # Runs sas and saves the return code
print(return_code)

But I would like to pass an argument into the macro, so I can run it, for example with all the years from 2010 to 2018. My SAS macro (that calls several other macros) look something like this:

options nocenter nodate nonumber mprint;

%let sti=C:\Mypath\;
%include "&sti.macro1.sas" ;

%let aar=2018;

%macro1(aar=&aar);

So instead of %let aar=2018; in sas, I would like to do something like this for(aar in 2010:2018){ code } in R.

Any suggestions?


Solution

  • You can use the -SET option on the SAS command line to pass arguments into SAS. Then use the %SYSGET macro to retrieve those values in your program. For further discussion and an example, see the article "How to pass parameters to a SAS program."