Sunday, January 4, 2009

CALL EXECUTE change the "macro execution path"

As for CALL EXECUTE, SAS Manual says that
"If argument resolves to a macro invocation, the macro executes immediately and DATA step execution pauses while the macro executes. If argument resolves to a SAS statement or if execution of the macro generates SAS statements, the statement(s) execute after the end of the DATA step that contains the CALL EXECUTE routine."

And it is interesting that the macro execution path are different for CALL EXECUTE type and standard type.

Standard type executes Macro language (statement, function, variable) and SAS statements step by step. However, CALL EXECUTE type do NOT execute SAS statement, which are created by macro, but put them after the end of that DATA step.

Sample code:

%macro test(a);
%let b=2;
%put 1> a is "&a" and b is "&b";
data _null_;
put "2> a is '&a' and b is '&b'";
call symput('b', '3');
run;

%put 3> a is "&a" and b is "&b";
%mend;

* Standard Type;
%test(1)

* Output:
1> a is "1" and b is "2"
2> a is '1' and b is '2'
3> a is "1" and b is "3"
;


* CALL EXECUTE Type;
data _null_;
call execute('%test(1)');
run;

* Output:
1> a is "1" and b is "2"
3> a is "1" and b is "2"
2> a is '1' and b is '2'
;

No comments: