BookmarkSubscribeRSS Feed
eduard1231
Fluorite | Level 6

Hi everyone,

 

I am planning to create following macro structure:

 

%macro macro1(x,y,z)
.
.
.
%macro macro2(a,b,c)
.
some manipulation with x,y,z here
.
%mend macro2
%mend macro1

 

 

Will this work? I am worried for x y z manipulation in macro2.

 

Thanks

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Preferred and highly recommended is to not nest the macros

 

%macro macro1(x,y,z)
.
.

%mend macro1;


%macro macro2(a,b,c)
.
some manipulation with x,y,z here
.
%mend macro2;

 

However, it's not really clear to me what your concern is. It's also not clear to me how or when or where in the code you are calling these two macros.

--
Paige Miller
Reeza
Super User

Nesting macros is a bad idea. 
Separate them out into their own definition and then call the other macro if needed, but there should never be a need to nest like this and I would highly recommend against it. 

 


@eduard1231 wrote:

Hi everyone,

 

I am planning to create following macro structure:

 

%macro macro1(x,y,z)
.
.
.
%macro macro2(a,b,c)
.
some manipulation with x,y,z here
.
%mend macro2
%mend macro1

 

 

Will this work? I am worried for x y z manipulation in macro2.

 

Thanks


 

Tom
Super User Tom
Super User

First of all the name space for macros is flat.  You cannot actually nest macro definitions. There can only be one macro named MACRO2 at any point in time.  So it will just confuse you if you structure your source code to have one macro defined inside of another macro.

 

I think you are actually worried about the possibility of the execution of macro2 when called by macro1 making changes to the local macro variables in macro1.  There is nothing that MACRO1 can do to prevent this.  It is up to macro2 to not modify macro variables it does not intend to modify.  In your example if MACRO2 wants to use macro variable name X then it should take care to define it as LOCAL.

 

%macro macro1(x,y,z);
...
%macro2;
...
%mend macro1;

%macro macro2(a,b,c);
%local x ;
%do x=1 %to &a ;
  ...
%end;
%mend macro2;

%macro1(2,3,4);

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 3 replies
  • 1097 views
  • 2 likes
  • 4 in conversation