BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

4 REPLIES 4
Astounding
PROC Star

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.

Tom
Super User Tom
Super User

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.

Reeza
Super User

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. 

SAS_inquisitive
Lapis Lazuli | Level 10

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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