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

How to conditionally %include a *.sas file that defines a macro?

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

在批量运行ning的时候,我想这样使用:%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.

只需使用一个%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;

如我所料,错误发生是因为包含文件以注释开头,例如:

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

正如 Allan 所建议的,将 %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;