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;
HI,
I would say 3 words: macro variables scoping.
Try to add;
%local i;
inside macro3 and macro2.
Somme additional reading:
2) https://blogs.sas.com/content/sgf/2015/02/13/sas-macro-variables-how-to-determine-scope/
All the best
Bart
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.