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

Hello,

 

 

Can Anybody please clarify below question:

 

"Can a macro create within another macro? If so, how would SAS know where the current macro ended and the new one began? "

 

 

Regards,

Jaiganesh

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You can, but you should never do this. It's really bad practice, and I've never come across such a use case in 15+ years. It can be done, but there are usually better ways. 

 

%macro embeddedexample;

proc print data=sashelp.class (obs=2);
run;

%macro test;

proc print data=sashelp.cars (obs=5);
run;

%mend test;

%mend embeddedexample;


%embeddedexample;

%test;

@jaiganesh wrote:

Hello,

 

 

Can Anybody please clarify below question:

 

"Can a macro create within another macro? If so, how would SAS know where the current macro ended and the new one began? "

 

 

Regards,

Jaiganesh


 

View solution in original post

6 REPLIES 6
Astounding
PROC Star

As @Kurt_Bremser showed, it can be done.

 

However, good programmers consider that bad practice.

 

The easiest way to follow this type of code is to add to your %MEND statements, naming the macro being ended:

 

%mend inner;

 

or

 

%mend outer;

 

It's not a requirement, it just is easier to follow.

Kurt_Bremser
Super User

An addendum: all macros are always defined in the global symbol table, so nesting macro definitions only causes confusion when reading the code, but serves no other purpose. DON'T DO IT.

jaiganesh
Obsidian | Level 7
Thanks for suggestions. I would keep this in mind.
Reeza
Super User

You can, but you should never do this. It's really bad practice, and I've never come across such a use case in 15+ years. It can be done, but there are usually better ways. 

 

%macro embeddedexample;

proc print data=sashelp.class (obs=2);
run;

%macro test;

proc print data=sashelp.cars (obs=5);
run;

%mend test;

%mend embeddedexample;


%embeddedexample;

%test;

@jaiganesh wrote:

Hello,

 

 

Can Anybody please clarify below question:

 

"Can a macro create within another macro? If so, how would SAS know where the current macro ended and the new one began? "

 

 

Regards,

Jaiganesh


 

Tom
Super User Tom
Super User

Are you asking about actual macros or perhaps you mean macro variables (aka symbols) instead?

 

To let the macro processor know where the name of the macro variable being referenced ends use a period.

So this code is looking for a macro variable named XXS.

%let xx=dataset;
%let n=32;
%put There are &n &xxs.;

But this code is looking for a macro variable named XX and appending the letter s and a period to the result.

%put There are &n &xx.s.;

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 1013 views
  • 6 likes
  • 5 in conversation