BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kaushal2040
Calcite | Level 5

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 !!!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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);

View solution in original post

4 REPLIES 4
ballardw
Super User

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.

Tom
Super User Tom
Super User

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);

Peter_C
Rhodochrosite | Level 12

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

kaushal2040
Calcite | Level 5

Thanks ballardw, Tom, and Peter.C.

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 11371 views
  • 6 likes
  • 4 in conversation