Hello Forum,
I was trying to come up with some macro code to understand nested macro.
options mprint mlogic symbolgen;
%macro test1(parm1);
%macro test(par);
proc print data=∥
run;
%mend test;
%test(&parm1);
%mend test1;
%test1(sashelp.class);
Is this valid way to write nested macro or are there alternatives for this?
Thanks !!!
It is the execution of the macros that can be nested, not the definition. In fact writing that way will just make it harder to maintain the code.
In your example you are simply calling the macro TEST from within the execution of TEST1.
%macro test(par);
proc print data=∥
run;
%mend test;
%macro test1(parm1);
%test(&parm1);
%mend test1;
%test1(sashelp.class);
Nesting macro definitions is possible but seldom actually needed. I would spend more time on learning how to pass parameters and control logic.
If your program logic doesn't lead you to wanting to have a %macro &nestedmacroname (); kind of statement you probably don't need to nest a macro.
Debugging a program with a nested macro can become extremely difficult in very short order.
Another reason to avoid the nested construction is for a macro called frequently the nested macro are recompiled every execution, which can result in a performance hit, especially the resulting nested macros are generally the same.
A more esoteric argument against them is if using the SOURCE option for saving a macro in a permanent macro catalog does not work for the nested macros.
It is the execution of the macros that can be nested, not the definition. In fact writing that way will just make it harder to maintain the code.
In your example you are simply calling the macro TEST from within the execution of TEST1.
%macro test(par);
proc print data=∥
run;
%mend test;
%macro test1(parm1);
%test(&parm1);
%mend test1;
%test1(sashelp.class);
I find it hard to imagine how nesting the compilation of macros would become helpful or neccessary.
Please explain the perceived benefits.
Macros can achieve much more diverse complexity in other ways. It is not often helpful.
To invoke a list processing engine I crafted a macro that invokes that engine - the core looked like
%&execut
where &execut names the (macro) process to be applied repeatedly.
There is a SUGI paper on this - search for mloopsx at http://www.lexjansen.com
Thanks ballardw, Tom, and Peter.C.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.