DATA Step, Macro, Functions and more

Nested do loop in sas

Accepted Solution Solved
Reply
Super Contributor
Posts: 271
Accepted Solution

Nested do loop in sas

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;


Accepted Solutions
Solution
‎08-02-2016 11:32 AM
SAS Super FREQ
Posts: 8,862

Re: Nested do loop in sas

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


All Replies
Super User
Posts: 19,770

Re: Nested do loop in sas

Posted in reply to SAS_inquisitive

No. It's repeated 3 times 

Super Contributor
Posts: 298

Re: Nested do loop in sas

Posted in reply to SAS_inquisitive
The inner loop repeats 3 times and the outer 2 times, making 6 iterations in all.
Super User
Posts: 19,770

Re: Nested do loop in sas

Posted in reply to SAS_inquisitive

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;
Super Contributor
Posts: 271

Re: Nested do loop in sas

Reeza, I mean the entire inner do loop block.
Super User
Posts: 5,424

Re: Nested do loop in sas

Posted in reply to SAS_inquisitive
Both @Reeza...?
Data never sleeps
Super User
Posts: 19,770

Re: Nested do loop in sas

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

Solution
‎08-02-2016 11:32 AM
SAS Super FREQ
Posts: 8,862

Re: Nested do loop in sas

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

Super User
Posts: 5,497

Re: Nested do loop in sas

Posted in reply to Cynthia_sas

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).

 

SAS Super FREQ
Posts: 8,862

Re: Nested do loop in sas

Posted in reply to Astounding
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
Super Contributor
Posts: 271

Re: Nested do loop in sas

Posted in reply to Cynthia_sas
Cynthia_sas, can you give an example of later case? Thanks !
☑ This topic is solved.

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

Discussion stats
  • 10 replies
  • 845 views
  • 4 likes
  • 6 in conversation