我从不知道您不能在 %MACRO 语句中使用变量……但情况似乎如此。正如 SAS 文档(http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#macro-stmt.htm)中所说,“您不能使用文本表达式在 %MACRO 语句中生成宏名称。”
我的下一个想法是您可能能够将 %MACRO 语句创建为变量,但我找不到在创建变量时屏蔽 %MACRO 的方法。
我终于想出了一个解决方法,但这可能不是最好的方法(而且它可能不适用于你想要做的事情)。我发现我可以在数据步骤中编译宏语句。不幸的是,当整个宏代码(从 %MACRO 到 %MEND 语句)保存在变量中时,我只能从变量中运行宏。请参阅下面的代码。
%MACRO test(name);
data test;
*COMPILE MACRO STATEMENT;
pct=%nrstr('%');
name="new_&name";
beginning=pct||'MACRO '||strip(name)||'();';
*CODE TO BE INSIDE MACRO;
/*Note: SAS will encounter errors if you try to assign text containing macro
functions (e.g., %PUT, %IF, etc.) to a variable. To get around this, you must
put hide the % in the following syntax, %nrstr('%'), and concatenate/join the
syntax with the rest of the string */
code=pct||'PUT HELLO!;';
*COMPILE MEND STATEMENT;
end=pct||'MEND;';
call symput('MacroStatement',beginning||code||end); *Output var containing macro;
call symput('Execute',pct||strip(name)||';'); *Output var containing statement to run macro;
output;
run;
&MacroStatement
&Execute
%MEND;
%test(name1);