## Nested do loop in sas

Solved
Super Contributor
Posts: 285

# 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: 9,366

## 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

All Replies
Super User
Posts: 23,695

## Re: Nested do loop in sas

No. It's repeated 3 times

Super Contributor
Posts: 324

## Re: Nested do loop in sas

The inner loop repeats 3 times and the outer 2 times, making 6 iterations in all.
Super User
Posts: 23,695

## Re: Nested do loop in sas

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: 285

## Re: Nested do loop in sas

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

## Re: Nested do loop in sas

Both @Reeza...?
Data never sleeps
Super User
Posts: 23,695

## 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: 9,366

## 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

Super User
Posts: 6,759

## Re: Nested do loop in 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: 9,366

## Re: Nested do loop in sas

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: 285

## Re: Nested do loop in sas

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

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