DATA Step, Macro, Functions and more

A method for CALL Executing a macro

Posts: 0

A method for CALL Executing a macro

Let's say you want to iterate through multiple "PROC ... " or some other processes for a varying set of stuff. A way to do this is by encasing the process in a macro and then using CALL EXECUTE within a data step. The explanation for CALL EXECUTE states that macros are resolved within the context of the data step but, the resulting generated lines are executed after the data step. This can cause improper processing of some Macroized processes, where the desire is that the whole macro is executed outside of the calling data step. How do you do this? One solution I've used is to write the macro call out to a single line file, and then use CALL EXECUTE to %include the file. That is kludgy and prone to other maintenance issues. Here is another way to defer the macro call until after the composing data step. This is simply a demonstration set of code.

%macro log_this(message);
%put message = &message;
%mend log_this;

%let dummy = %qsubstr('%log_this',2,9) ;

data _null_;
length statement $256;
statement = '%superq(dummy)(' || "Hi there" || ')' ;
put "before call";
call execute(statement);
put "after call";

With this you will see that
1) the macro %log_this is not executed by accident in the %let statement because of the use of %Qsubstr and single quoting %log_this.
2) %log_this is not accidentally executed when forming the "statement" variables string value, again due to the use of single quotes
3) %log_this is not accidentally executed after "before call" and before "after call". This is because "statement" resolves to "%superq(dummy)(Hi there)" and %superq(dummy) resolves to %log_this inside the data step, but prevents the resolution of %log_this.
4) After the data step, the log shows the CALL EXECUTE generated line, and then the execution of the %log_this macro.
Ask a Question
Discussion stats
  • 0 replies
  • 1 in conversation