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 2025: Call for Content

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!

Submit your idea!

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