SAS 将代码包含到数据步骤
SAS include code into data step
我动态创建了一个 myfile.sas,内容如下:
and a = 0
and b = 0
现在我想将此文件包含到数据步骤中:
data y;
set x;
if 1=1
%include incl("myfile.sas")
then selektion=0;
else selektion=1;
run;
结果应该是:
data y;
set x;
if 1=1
and a=0
and b=0
then myvar=0
else myvar=1;
run;
但是我收到以下错误:
ERROR 388-185: Expecting an arithmetic operator.
ERROR 200-322: The symbol is not recognized and will be ignored.
是否可以将文件包含到 if 语句中?
的确,那行不通。您可以在 data
或 proc
步骤中使用 %include
向其中添加一些行,但不能在不完整的语句中使用。
如果你的 myfile.sas
看起来像这样:
if 1=1
and a = 0
and b = 0
你可以写
data y;
set x;
%include "myfile.sas";;
then selektion=0;
else selektion=1;
run;
您不能将这些行放在宏而不是文件中吗?
%macro mymacro;
and a=0
and b=0
%mend;
data y;
set x;
if 1=1
%mymacro
then selektion=0;
else selektion=1;
run;
如果 myfile.sas
必须 保持原样,您可以通过这种相当复杂(但仍然通用)的方式解决它:
filename myfile temp;
data _null_;
file myfile2;
infile 'myfile.sas' eof=end;
input;
if _n_=1 then put '%macro mymacro;';
put _infile_;
return;
end:
put '%mend;';
run;
%include myfile;
data y;
set x;
if 1=1
%mymacro
then selektion=0;
else selektion=1;
run;
%INCLUDE
需要在语句边界。您可以将 IF 1=1
放入同一个文件或另一个文件中。确保包含分号以结束 %INCLUDE
命令,但不要在文件内容中包含分号。
data y;
set x;
%include incl("if1file.sas","myfile.sas") ;
then selektion=0;
else selektion=1;
run;
更好的解决方案可能是将代码放入宏变量中(如果小于 64K 字节)。
%let condition=
and a = 0
and b = 0
;
data y;
set x;
if 1=1 &condition then selektion=0;
else selektion=1;
run;
如果超过 64K 字节,则将其定义为宏。
%macro condition;
and a = 0
and b = 0
%mend;
data y;
set x;
if 1=1 %condition then selektion=0;
else selektion=1;
run;
根据 SAS 文档:
%INCLUDE Statement
Brings a SAS programming statement, data lines, or both, into a current SAS program.
您尝试的注入不是完整的语句,因此失败。您正在描述的操作的更具体描述是 %INLINE。但是,没有这样的SAS语句。
让我们调用一个输出代码 'codegener' 的程序,它产生 'codegen'
的输出
在您使用的上下文中,codegen 特定于单个语句。这强烈建议代码生成器应该将代码生成器放在宏变量中(以便于以后使用)而不是文件。
假设代码生成器使用关于语句构造的数据:
DATA statements_meta;
length varname operator value 0;
input varname operator value;
datalines;
a = 0
b = 0
run;
代码生成器是一个数据步骤
DATA _null_;
file "myfile.snippet";
... looping logic over data for statement construction ...
put " and " varname " = 0 "
...
run;
将代码生成器更改为如下所示:
DATA _null_;
length snippet 000;
snippet = "";
... looping logic over data for statement construction ...
snippet = catx (" ", snippet, "and", varname, comparisonOperator, comparisonValue);
... end loop
call symput('snippet', trim(snippet));
stop;
run;
...
DATA ...
if 1=1 &snippet then ... else ...
run;
我动态创建了一个 myfile.sas,内容如下:
and a = 0
and b = 0
现在我想将此文件包含到数据步骤中:
data y;
set x;
if 1=1
%include incl("myfile.sas")
then selektion=0;
else selektion=1;
run;
结果应该是:
data y;
set x;
if 1=1
and a=0
and b=0
then myvar=0
else myvar=1;
run;
但是我收到以下错误:
ERROR 388-185: Expecting an arithmetic operator.
ERROR 200-322: The symbol is not recognized and will be ignored.
是否可以将文件包含到 if 语句中?
的确,那行不通。您可以在 data
或 proc
步骤中使用 %include
向其中添加一些行,但不能在不完整的语句中使用。
如果你的 myfile.sas
看起来像这样:
if 1=1
and a = 0
and b = 0
你可以写
data y;
set x;
%include "myfile.sas";;
then selektion=0;
else selektion=1;
run;
您不能将这些行放在宏而不是文件中吗?
%macro mymacro;
and a=0
and b=0
%mend;
data y;
set x;
if 1=1
%mymacro
then selektion=0;
else selektion=1;
run;
如果 myfile.sas
必须 保持原样,您可以通过这种相当复杂(但仍然通用)的方式解决它:
filename myfile temp;
data _null_;
file myfile2;
infile 'myfile.sas' eof=end;
input;
if _n_=1 then put '%macro mymacro;';
put _infile_;
return;
end:
put '%mend;';
run;
%include myfile;
data y;
set x;
if 1=1
%mymacro
then selektion=0;
else selektion=1;
run;
%INCLUDE
需要在语句边界。您可以将 IF 1=1
放入同一个文件或另一个文件中。确保包含分号以结束 %INCLUDE
命令,但不要在文件内容中包含分号。
data y;
set x;
%include incl("if1file.sas","myfile.sas") ;
then selektion=0;
else selektion=1;
run;
更好的解决方案可能是将代码放入宏变量中(如果小于 64K 字节)。
%let condition=
and a = 0
and b = 0
;
data y;
set x;
if 1=1 &condition then selektion=0;
else selektion=1;
run;
如果超过 64K 字节,则将其定义为宏。
%macro condition;
and a = 0
and b = 0
%mend;
data y;
set x;
if 1=1 %condition then selektion=0;
else selektion=1;
run;
根据 SAS 文档:
%INCLUDE Statement
Brings a SAS programming statement, data lines, or both, into a current SAS program.
您尝试的注入不是完整的语句,因此失败。您正在描述的操作的更具体描述是 %INLINE。但是,没有这样的SAS语句。
让我们调用一个输出代码 'codegener' 的程序,它产生 'codegen'
的输出在您使用的上下文中,codegen 特定于单个语句。这强烈建议代码生成器应该将代码生成器放在宏变量中(以便于以后使用)而不是文件。
假设代码生成器使用关于语句构造的数据:
DATA statements_meta;
length varname operator value 0;
input varname operator value;
datalines;
a = 0
b = 0
run;
代码生成器是一个数据步骤
DATA _null_;
file "myfile.snippet";
... looping logic over data for statement construction ...
put " and " varname " = 0 "
...
run;
将代码生成器更改为如下所示:
DATA _null_;
length snippet 000;
snippet = "";
... looping logic over data for statement construction ...
snippet = catx (" ", snippet, "and", varname, comparisonOperator, comparisonValue);
... end loop
call symput('snippet', trim(snippet));
stop;
run;
...
DATA ...
if 1=1 &snippet then ... else ...
run;