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 am trying to understand the nested do loop concept. In this example, can we say inner do loop is repeated 2 times ?

 

data test;

    do i = 1 to 2;

       do j = 1 to 3;

          output;

       end;

    end;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

Hi:

  I agree, it depends on context and how you write the code. I think this comparison of the original do loop example and a modified version with i, j, and k loops in a different position explains how the iterations work -- 30 observations each time (because 2*3*5 or 5*2*3 or 3*2*5 will always equal 30), but the values fall into a different pattern.

 

cynthia

 

ijk_loop.png

View solution in original post

10 REPLIES 10
Reeza
Super User

No. It's repeated 3 times 

KachiM
Rhodochrosite | Level 12
The inner loop repeats 3 times and the outer 2 times, making 6 iterations in all.
Reeza
Super User

Also, inner/outer aren't always clear. Is J an inner or outer loop?

 

data test;
    do i = 1 to 2;
       do j = 1 to 3;
           do k=1 to 5;
                output;
            end;
       end;
    end;
run;
SAS_inquisitive
Lapis Lazuli | Level 10
Reeza, I mean the entire inner do loop block.
LinusH
Tourmaline | Level 20
Both @Reeza...?
Data never sleeps
Reeza
Super User

@LinusH No idea, it probably depends on context. Just that as terminology goes, it's not definitive. 

Cynthia_sas
SAS Super FREQ

Hi:

  I agree, it depends on context and how you write the code. I think this comparison of the original do loop example and a modified version with i, j, and k loops in a different position explains how the iterations work -- 30 observations each time (because 2*3*5 or 5*2*3 or 3*2*5 will always equal 30), but the values fall into a different pattern.

 

cynthia

 

ijk_loop.png

Astounding
PROC Star

One consideration is worth noting for efficiency purposes ... even though the inner loop executes 30 times in both cases, the total number of loops is different.  The left-hand program is the most efficient order and executes a total of 38 loops (2 outer, 6 middle, 30 inner), while the right-hand program executes a total of 45 loops (5 outer, 10 middle, 30 inner).  The least efficient order would put the fastest-moving variables on the outside:

 

do k=1 to 5;

do j=1 to 3;

do i=1 to 2;

end;

end;

end;

 

Now there are a total of 50 loops (5 outer, 15 middle, 30 inner).

 

Cynthia_sas
SAS Super FREQ
Hi:
Most of the time, I use nested DO loops for data generation. If I am using the DO loops to generate some data for testing, then my primary consideration would be to use the looping syntax that produces the structure I need. Otherwise, I generally find the data structure of data I'm reading will dictate the form of the nested loops.

cynthia
SAS_inquisitive
Lapis Lazuli | Level 10
Cynthia_sas, can you give an example of later case? Thanks !

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
  • 10 replies
  • 3385 views
  • 4 likes
  • 6 in conversation