2

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?

Joe
  • 62,789
  • 6
  • 49
  • 67
Jeppe Olsen
  • 968
  • 8
  • 19

3 Answers3

4

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.

Tom
  • 47,574
  • 2
  • 16
  • 29
  • Could you give an example on how that could work, please? – Jeppe Olsen Jan 14 '19 at 12:46
  • When you run a SAS program from the command line you usually use command like `sas myfile.sas`. Instead add in the `-sysparm` option and your command becomes `sas -sysparm 2018 myfile.sas`. You can then use the value in your SAS program by referencing it as `&sysparm`. – Tom Jan 14 '19 at 14:00
  • Can't get it to work. I tried cmd <- sprintf( 'sas.exe -nosplash -icon -sysin "%s" -log "%s" -print "%s" -set aar "%s -set aar1 "%s"', sas_script, sas_log, sas_out, aar, aar1 ) and cmd <- sprintf( paste0('sas.exe -nosplash -icon -sysin "%s" -log "%s" -print "%s" -set aar \"', aar,'" -set aar \"', aar1, '"'), sas_script, sas_log, sas_out ) – Jeppe Olsen Jan 16 '19 at 09:36
  • What values are you trying to set to the variable `aar`? Why are you trying to set a value to the same variable multiple times? – Tom Jan 16 '19 at 15:02
  • Well... the sas code is not mine, but guess i could just make aar1 from aar. Is it a problem to add two variables? – Jeppe Olsen Jan 16 '19 at 15:04
  • Fix one problem at a time. Figure out how to pass single value like `2010:2018` into your SAS program or two separate values of `2010` and `2018`. Then figure how to get the SAS program to use that to control a loop. – Tom Jan 16 '19 at 15:53
4

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

Rick
  • 1,210
  • 6
  • 11
2

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);
Rhesous
  • 984
  • 6
  • 12
  • 1
    I guess that is one way to do it. Not quite what i asked for, as I would like to control the startyear and endyear from R, as several other people are working on the SAS code, so would be more convienient for me to just edit the years in R – Jeppe Olsen Aug 07 '18 at 08:43