如何将 %LET 参数从 R 传递到 sas 脚本?

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

我有一个 R 脚本,可以让我 运行 在 SAS 中创建一个宏,就像这样(灵感来自 ):

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)

但我想将一个参数传递给宏,这样我就可以 运行 它,例如从 2010 年到 2018 年的所有年份。我的 SAS 宏(调用其他几个宏)看起来有点像像这样:

options nocenter nodate nonumber mprint;

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

%let aar=2018;

%macro1(aar=&aar);

所以我不想在 sas 中 %let aar=2018;,而是想在 R 中做这样的事情 for(aar in 2010:2018){ code }

有什么建议吗?

我不确定我是否理解你的问题,但如果我是对的,你想从 N 年循环到另一年 M,对吧?

因此,在 SAS 中,您可以在宏中编写一个循环,然后 运行 宏。在你的例子中,我会这样做:

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);

您可以使用 -sysparm 命令行选项将值传递到您的 SAS 程序中。您可以使用 &sysparm 宏变量引用在 SAS 程序中引用该值。

您可以在 SAS 命令行上使用 -SET 选项将参数传递到 SAS。然后使用 %SYSGET 宏在您的程序中检索这些值。有关进一步的讨论和示例,请参阅文章 "How to pass parameters to a SAS program."