DATA Step, Macro, Functions and more

statement between outer and inner %do loop

Accepted Solution Solved
Reply
Regular Contributor
Posts: 234
Accepted Solution

statement between outer and inner %do loop

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;


Accepted Solutions
Solution
‎06-04-2017 11:09 AM
Super User
Posts: 5,082

Re: statement between outer and inner %do loop

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.

View solution in original post


All Replies
Solution
‎06-04-2017 11:09 AM
Super User
Posts: 5,082

Re: statement between outer and inner %do loop

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.

Super User
Super User
Posts: 6,500

Re: statement between outer and inner %do loop

[ Edited ]

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.

Super User
Posts: 17,824

Re: statement between outer and inner %do loop

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. 

Regular Contributor
Posts: 234

Re: statement between outer and inner %do loop

Yes. I meant &m and &n. Thank you all.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 196 views
  • 4 likes
  • 4 in conversation