有没有办法将宏作为参数传递给宏?
类似
%macro outer_macro(macro);
%do i=1 %to 5;
¯o
%end;
%mend outer_macro;
%macro inner_macro;
/* do something usefull */
%mend inner_macro;
%outer_macro(%inner_macro)
这个例子当然不起作用。
答案 0 :(得分:5)
您可以使用宏引用来实现此目的。
您示例的简单版本:
%macro outer_macro(macro);
%put Starting outer macro;
%unquote(¯o) /* %unquote removes the NRSTR escape characters */
%put Ending outer macro;
%mend outer_macro;
%macro inner_macro;
%put Starting inner macro;
proc print data=sashelp.class;
run;
%put Ending inner macro;
%mend inner_macro;
%outer_macro(%nrstr(%inner_macro)) /* %nrstr is execution-time quoting, so %inner_macro is passed and not treated as a macro until %unquote removes the quoting */
要包含类似循环的东西,你只需要确保所有东西都设置为采用循环迭代器 - 一种方法就是简单地用循环附加()。
%macro outer_macro(macro);
%put Starting outer macro;
%do i = 1 %to 19;
%unquote(¯o)(n=&i.) /* resolves to %inner_macro(n=&i.) */
%end;
%put Ending outer macro;
%mend outer_macro;
%macro inner_macro(n=);
%put Starting inner macro;
proc print data=sashelp.class(obs=&n. firstobs=&n.);
run;
%put Ending inner macro;
%mend inner_macro;
%outer_macro(%nrstr(%inner_macro))
答案 1 :(得分:2)
为什么不按名称传递宏?
%macro outer_macro(macro);
%put Starting &sysmacroname - MACRO=%superq(macro);
%¯o
%put Ending &sysmacroname;
%mend outer_macro;
%macro inner_macro(dsn);
%put Starting &sysmacroname - &=dsn;
proc print data=&dsn;
run;
%put Ending &sysmacroname;
%mend inner_macro;
%outer_macro(inner_macro(sashelp.class)) ;
带有MPRINT的结果日志已打开。
Starting OUTER_MACRO - MACRO=inner_macro(sashelp.class)
Starting INNER_MACRO - DSN=sashelp.class
MPRINT(INNER_MACRO): proc print data=sashelp.class;
MPRINT(INNER_MACRO): run;
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.04 seconds
cpu time 0.00 seconds
Ending INNER_MACRO
Ending OUTER_MACRO