题
我使用SAS 9.1.3调用在DATA步骤宏,但宏生成一个PROC REPORT步骤,所以我使用CALL EXECUTE调用它,产生所有这些PROC REPORT步骤,然后执行它们之后的所有数据的步骤。
我使用的阵列,和宏每次执行用于该阵列中的每个元素:
DATA macro_test;
ARRAY questions[3] $ 32 ('question1' 'question2' 'question3');
DO i=1 to 3;
a_question = questions(i);
CALL EXECUTE( "%report_by_question(a_question)" );
end;
RUN;
的事情是,报表输出被向后脱出(通常) - 它将打印问题3,再如图2所示,然后1
是否有修改调用的执行顺序执行,所以我可以有顺序的问题报告打印,还是只是做自己的事情呢?
谢谢!
解决方案
我假定你的意思是更多像这样YOUT call execute()
行:
CALL EXECUTE( "%report_by_question(" || trim(left(a_question)) || ");" );
通过一个测试宏我得到一些日志行像这样,显示出call execute()
s被以正确的顺序发生。你得到了类似的话?
宏
%macro report_by_question(a);
data test_&a;
do i=1 to 10000000;
output;
end;
run;
%mend;
日志
NOTE: CALL EXECUTE generated line. 1 + data test_question1; do i=1 to 10000000; output; end; run; NOTE: The data set WORK.TEST_QUESTION1 has 10000000 observations and 1 variables. NOTE: DATA statement used (Total process time): real time 6.14 seconds cpu time 0.45 seconds 1 + ; 2 + data test_question2; do i=1 to 10000000; output; end; run; NOTE: The data set WORK.TEST_QUESTION2 has 10000000 observations and 1 variables. NOTE: DATA statement used (Total process time): real time 3.87 seconds cpu time 0.53 seconds 2 + ; 3 + data test_question3; do i=1 to 10000000; output; end; run; NOTE: The data set WORK.TEST_QUESTION3 has 10000000 observations and 1 variables. NOTE: DATA statement used (Total process time): real time 3.12 seconds cpu time 0.45 seconds
其他提示
一个数据步骤被编译并执行。所述call execute(str);
推STR到输入队列中,以便它们被弹出的之后数据一步完成执行。的顺序被保留,周期。
不过,如果你把你的宏调用的双引号字符串中,你在做的: 调用execute( “%报告(Q)”); 然后当数据步骤被编译,这是数据步骤开始运行前,即使在宏被调用。
如果你不想调用宏在编译的时候,那么无论宏观引号,或把它放在单引号的字符串中。下面是一个例子。希望这有助于。
/* create a dataset with 1 var and 3 obs */
data qs;
input q $;
cards;
q1
q2
q3
;
run;
/* reporting macro -- a mockup */
%macro report(q=);
%put report for q=&q;
%mend report;
/* call the reporting macro for each q */
data _null_;
set qs;
macro = catx(q, '%report(q=', ')');
call execute(macro);
run;
/* on log
report for q=q1
report for q=q2
report for q=q3
*/
/* to show what happens when the
macro is invoked during the compile
time */
data _null_;
call execute("%report(q=q1)");
put "after call execute";
run;
/* on log
1 data _null_;
2 call execute("%report(q=q1)");
report for q=q1
3 put "after call execute";
4 run;
after call execute
*/
我喜欢做的一切宏中使用宏语言有关。我猜权衡的是,你必须分散在整个程序中的小宏。但是,为了防止程序生成报告,只是注释掉宏调用(*%loopit;)同时,你不必键入了“问题1”,“问题2”,“问题3”等!结果 希望这是对你有用!
%macro report_by_question(input);
%put "Question: " &input;
%mend;
%macro loopit;
%do i=1 %to 3;
%report_by_question("question&i.");
%end;
%mend loopit;
%loopit;
不隶属于 StackOverflow