DATA Step, Macro, Functions and more

Nested macro

Accepted Solution Solved
Reply
Contributor
Posts: 50
Accepted Solution

Nested macro

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


Accepted Solutions
Solution
‎12-11-2014 07:16 PM
Super User
Super User
Posts: 7,039

Re: Nested macro

Posted in reply to kaushal2040

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


All Replies
Super User
Posts: 11,343

Re: Nested macro

Posted in reply to kaushal2040

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.

Solution
‎12-11-2014 07:16 PM
Super User
Super User
Posts: 7,039

Re: Nested macro

Posted in reply to kaushal2040

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

Valued Guide
Posts: 2,177

Re: Nested macro

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

Contributor
Posts: 50

Re: Nested macro

Thanks ballardw, Tom, and Peter.C.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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