如何有条件地 %include 一个定义宏的 *.sas 文件?

Posted

技术标签:

【中文标题】如何有条件地 %include 一个定义宏的 *.sas 文件?【英文标题】:How to conditionally %include a *.sas file that defines a macro? 【发布时间】:2022-01-14 03:06:42 【问题描述】:

我在几个 SAS 程序中使用了一个宏,所以我在一个单独的文件 /myFolder/myMacro.sas 中定义了它。

批量运行时,我想这样使用:%include '/myFolder/myMacro.sas;'

在企业指南中测试代码更改时,我想编辑并运行/myFolder/myMacro.sas,然后编辑并运行使用它的程序。如何有条件地包含宏定义?

%if &server = BATCH_SERVER %then %include '/myFolder/myMacro.sas;' 不起作用:文件仍然包含在内,%if 语句应用于文件顶部的注释并导致

ERROR: Expected %DO not found.
ERROR: Skipping to next %END statement.

【问题讨论】:

所以您只希望 %include 在批处理模式下运行? 【参考方案1】:

只需使用%then %do

%let mode=BATCH;
filename mac1 temp;
filename mac2 temp;

data _null_;
  file mac1;
  put '%macro mac1;%put mac1;%mend;%mac1;';
data _null_;
  file mac2;
  put '%macro mac2;%put mac2;%mend;%mac2';
run;
%if &mode=BATCH %then %do;
  %inc mac2;
%end;
%else %do;
  %inc mac1;
%end;

【讨论】:

对,艾伦,这就是解决方案,但你的答案与问题的偏差太大了,我不能接受它作为问题的答案。【参考方案2】:

正如我所怀疑的,发生错误是因为包含文件以 cmets 开头,类似于:

* MyMacro is written to do this and that *;
* ************************************** *;
%macro MyMacro;
    proc this;
    proc that;
    run;
%mend;

所以在包含文件之后,就变成了

%if &server = BATCH_SERVER %then * MyMacro is written to do this and that *;
* ************************************** *;
%macro MyMacro;
    proc this;
    proc that;
    run;
%mend;

这是无效的。

在宏内部工作时:添加%do;%end;

正如艾伦建议的那样,将%inlcude 放在%do;%end; 之间就足够了

%if &server = BATCH_SERVER %then %do;
    %include '/myFolder/myMacro.sas;'
%end;

所以在包含文件之后,就变成了

%if &server = BATCH_SERVER %then %do;
    * MyMacro is written to do this and that *;
    * ************************************** *;
    %macro MyMacro;
        proc this;
        proc that;
        run;
    %mend;
%end;

哪个有效。

在开放代码中工作时:使用call execute

data _null_;
    if "&mode"="BATCH" then call execute ("%include /myFolder/myMacro.sas;");
run;
%DoIt;

【讨论】:

以上是关于如何有条件地 %include 一个定义宏的 *.sas 文件?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Eclipse 中更改条件宏的背景颜色?

宏的一些常用使用

如何使用一个宏的值作为参数传递给另一个宏?

如何有条件地定义 azure 模板参数?

查看linux系统某宏的定义(另类)

unity平台的预处理(宏的定义)