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

Hello! 

for a project I need to make macro invocations at different levels in do loops (see next example) so inside a master macro I have to loop over other ones and these macros that I invoke can do de same. My problem is that if I use the same index in the do loop in the master macro and the invoked macro (in this case "i") when the invoked macro is executed it modifies the index of the loop in the master macro, resulting in an unexpected result:   

 

%macro macro1();
	%do i = 1 %to 5;
		%put first_level_macro: &i.;
		%macro2;
	%end;
%mend;

%macro macro2();
	%do i = 1 %to 5;
		%put second_level_macro: &i.;
	%end;
%mend;

%macro1;

Result: 

 

first_level_macro: 1
second_level_macro: 1
second_level_macro: 2
second_level_macro: 3
second_level_macro: 4
second_level_macro: 5

 

Desired result: 

 

first_level_macro: 1
second_level_macro: 1
second_level_macro: 2
second_level_macro: 3
second_level_macro: 4
second_level_macro: 5

first_level_macro: 2
second_level_macro: 1
second_level_macro: 2
second_level_macro: 3
second_level_macro: 4
second_level_macro: 5

first_level_macro: 3
second_level_macro: 1
second_level_macro: 2
second_level_macro: 3
second_level_macro: 4
second_level_macro: 5

first_level_macro: 4
second_level_macro: 1
second_level_macro: 2
second_level_macro: 3
second_level_macro: 4
second_level_macro: 5

first_level_macro: 5
second_level_macro: 1
second_level_macro: 2
second_level_macro: 3
second_level_macro: 4
second_level_macro: 5

I know that I can simply change the name of the index in the master macro, but given the requirements of my project this is not possible, because we are integrating a lot of macros at several levels and we can not change the indexes names on each one of them. 

 

Is there a way to solve this without changing the names of the indexes? 

 

Thank you,  

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Always use %local to define variables as local that are not needed elsewhere.

%macro macro1();
%local i;
	%do i = 1 %to 5;
		%put first_level_macro: &i.;
		%macro2;
	%end;
%mend;

%macro macro2();
%local i;
	%do i = 1 %to 5;
		%put second_level_macro: &i.;
	%end;
%mend;

%macro1

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Always use %local to define variables as local that are not needed elsewhere.

%macro macro1();
%local i;
	%do i = 1 %to 5;
		%put first_level_macro: &i.;
		%macro2;
	%end;
%mend;

%macro macro2();
%local i;
	%do i = 1 %to 5;
		%put second_level_macro: &i.;
	%end;
%mend;

%macro1
ballardw
Super User

FWIW this is almost exactly the example I used to help teach basic scope of macro variables and likely problems with nested macro calls.

 

And the solution as ably pointed out by @Kurt_Bremser .

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 427 views
  • 4 likes
  • 3 in conversation