1
votes

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?

3

3 Answers

3
votes

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."

3
votes

You can use the -sysparm command line option to pass a value into your SAS program. You can reference the value in your SAS program using &sysparm macro variable reference.

2
votes

I'm not sure I understand your issue, but if I'm correct you want to loop from a year N to another year M, right ?

So in SAS you can write a loop inside a macro and run the macro. In your example, I'll do :

options nocenter nodate nonumber mprint;

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

%macro_loop(startyear,endyear);
%do year = &startyear. %to &endyear.;
    %macro1(aar = &year);
%end;
%mend;

%macro_loop(startyear = 2010, endyear = 2018);