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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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