I try calling three macros from within a macro conditionally using call execute, however, the macros are resolved but the SAS statements dont get executed. This code behaves capriciously. Is there a way to call macros like this? Moreover, if i try breaking out from a do loop, can i come back in the same loop after the macro work is done to execute further statements ?
%macro superset;
%if <condition> %then %do;
data _null_;
call execute(%subset1);
call execute(%subset2);
run;
%else do;
call execute(%subset3);
%end;
%macro subset1;
<SAS statements>
%mend subset1;
%macro subset2;
<SAS statements>
%mend subset2;
%macro subset3;
<SAS statements>
%mend subset3;
%mend superset;
The argument string for Call Execute has to be in quotes: call execute('%subset1'); for example.
Sorry I forgot to put quotes here..But i have used them in the original code..
You do not need to use CALL EXECUTE to call a macro. Especially when already executing a macro.
Also it is better to not nest macro definitions. It is just confusing to read and extra work for the SAS run time compiler to keep re-compiling the sub macros.
%macro subset1;
<SAS statements>
%mend subset1;
%macro subset2;
<SAS statements>
%mend subset2;
%macro subset3;
<SAS statements>
%mend subset3;
%macro superset;
%if <condition> %then %do;
%subset1;
%subset2;
%else do;
%subset3;
%end;
%mend superset;
%superset;
Thanks Tom, however, if my sub macros need to have access to the local macro variables of the superset macro and I havent nested them then I will have to use parametric definitions and pass those variables as parameters..am i right?
No. The nesting of macro variable scope is related to execution and not where/when the macro was defined.
If supermacro creates variable X then calls submacro the value of X is available to submacro to read and to modify.
Thanks, and it means that after calling the sub macro , may be in a loop, the control can return back to that loop and execute the SAS statements inside the loop and move forward?.. The control executes the sub macro and comes back , right?
Right, but you need to remember what control we are talking about. The macro executes and basically produces text that SAS will interpret as statements to be compiled and run. The macro could be something that generates many procs and data steps or it could generate just a few statements, or even part of a statement or a single word or token. Or it could even only manipulate macros variables and not generate any code that SAS could execute.
Thanks to Tom to remember me taht the call execute will be executed after the datastep close. I fogot this.
Instead to use this I prefer use this
%macro condition_1;
sas code ...
%mend;
%macro condition_2;
sas code ...
%mend;
filename x temp lrecl=1024;
data _null_;
file x;
length flow $2000;
if index("&flow",'FIRST CONDITION ') gt 0 then do;
put '%condifiton_1;';
End;
else do;
if index("&flow",'KPI_MO_REPORT') gt 0 then do;
put '%condittion_2;';
End;
else do;
put '%put call other macro programs if need ;';
end;
end;
run;
%inc x;;
filename x;
to debug it is more easy.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.