BookmarkSubscribeRSS Feed
NilNidhiEva
Calcite | Level 5

Hi All,

 

I have Do Loop within a Do Loop and Macro's inside of that.

 

Below is the Sequence.

 

can anyone please help on this ?

 

%MACRO My_Macro_1;

%do i = 1 %to 2; <---this loop only execute 1st time, but, does not calling 2nd time - that's the issue and questions
proc sql;
select
One_Variable
from Table1;
Quit;
%My_Macro_2(&Para1.);

proc sql noprint;
Select
.....
Quit;

%MACRO My_Macro_3;
%do i = 1 %to 47; <---this loop execute 47 times, when i = 1 for outer loop, but, does not calling 2nd time - that's the issue and questions
proc sql;
select
.....
.....
Quit;
%My_Macro_4(&Para2.);

%end;
%MEND My_Macro_3;
%My_Macro_3;

%end;
%MEND My_Macro_1;
%My_Macro_1;

2 REPLIES 2
yabwon
Onyx | Level 15

HI,

 

I would say 3 words: macro variables scoping.

 

Try to add;

 

%local i;

 

inside macro3 and macro2.

 

Somme additional reading:

1) https://documentation.sas.com/?docsetId=mcrolref&docsetTarget=p1b76sxg9dbcyrn1l5age5j5nvgw.htm&docse...

2) https://blogs.sas.com/content/sgf/2015/02/13/sas-macro-variables-how-to-determine-scope/

 

All the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

You are just making the code more confusing by placing macro definitions inside of each other.  There is just a single name space for macros.  But every macro does create its own name space (scope) for macro variables.  If you don't define a macro variable as LOCAL to the macro and there already exists a macro variable in an outer scope then it will be used.  So when control returns from macro three the value of I is now larger than the upper limit of the do loop in the macro one.

 

%MACRO My_Macro_3;
%local i;
%do i = 1 %to 47;
  .....
%end;
%MEND My_Macro_3;


%MACRO My_Macro_1;
%local i;
%do i = 1 %to 2;
  ...
%My_Macro_2(&Para1.); ..... %My_Macro_3; %end; %MEND My_Macro_1; %My_Macro_1;

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