Hello,
I was trying to understand how does the statements between outer and inner %do loop work as this psuedo code.
%macro mac;
................;
%do i = 1 %to m;
statement ;
%do j = 1 %to n;
.................;
.................;
%end;
%end;
...............;
%mend mac;
You're mixing DATA step code and macro language code in a way that will never run. Macro language does not understand that "m" or "n" is a variable in a data set. If you were to correct that by hard-coding a value like 3 or 5 instead of m or n, what happens depends on what the statement is. If it is a macro language statement, it executes for each value of &i, after which the inner loop executes. However, if statement is a DATA step statement (or some other form of SAS code), it gets added to your program as a statement to be executed, for each value of &i. After each addition, the inner loop runs.
Mixing DATA step code and macro language code is a common mistake, but without a more specific example it's not possible to guide you in the right direction.
You're mixing DATA step code and macro language code in a way that will never run. Macro language does not understand that "m" or "n" is a variable in a data set. If you were to correct that by hard-coding a value like 3 or 5 instead of m or n, what happens depends on what the statement is. If it is a macro language statement, it executes for each value of &i, after which the inner loop executes. However, if statement is a DATA step statement (or some other form of SAS code), it gets added to your program as a statement to be executed, for each value of &i. After each addition, the inner loop runs.
Mixing DATA step code and macro language code is a common mistake, but without a more specific example it's not possible to guide you in the right direction.
In your example the macro won't generate anything since N and M are not numbers. I suspect that you meant to use &M and &N instead as the upper bounds for your %DO loops.
In that case it will generate "statement;" &M times and it will generate the "....;....." statements inside the inner loop &M*&N times.
Run it, put unique %PUT statements at each level to trace it. Always learn how to trace your code mentally and practically. It's faster to test.
Yes. I meant &m and &n. Thank you all.
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!
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.