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
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
Maxim 4: Try It.
Run this code and look at the log:
%macro inner;
%put inner;
%mend;
%put end of outer;
%mend;
%outer
%inner
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.
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.
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
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.;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.