Solved
Contributor
Posts: 23

# do within do loop

data test888;
set test88;
array gap(*) gap1-gap370;
do r=1 to 370;
do y=1 to 370;
if ((gap(y) - gap(r)) >= 90) then leave;
end;
end;
run;

I cannot get the code to work.

I want to find the combination of gap(R) and gap(y) which have a difference of more than 89.

Accepted Solutions
Solution
‎10-25-2016 11:44 AM
PROC Star
Posts: 2,316

## Re: do within do loop

[ Edited ]

Like this?

``````data HAVE;
array GAP(*) GAP1-GAP370;
do I=1 to 370;
GAP[I]=ranuni(0)*100;
end;
run;

data WANT;
keep I J VAL_I VAL_J;
set HAVE;
array GAP(*) GAP1-GAP370;
do I=1 to 369;
do J=I+1 to 370;
if abs(GAP[I]-GAP[J]) >= 90 then do;
VAL_I=GAP[I];
VAL_J=GAP[J];
output;
end;
end;
end;
run;``````

All Replies
Solution
‎10-25-2016 11:44 AM
PROC Star
Posts: 2,316

## Re: do within do loop

[ Edited ]

Like this?

``````data HAVE;
array GAP(*) GAP1-GAP370;
do I=1 to 370;
GAP[I]=ranuni(0)*100;
end;
run;

data WANT;
keep I J VAL_I VAL_J;
set HAVE;
array GAP(*) GAP1-GAP370;
do I=1 to 369;
do J=I+1 to 370;
if abs(GAP[I]-GAP[J]) >= 90 then do;
VAL_I=GAP[I];
VAL_J=GAP[J];
output;
end;
end;
end;
run;``````

Contributor
Posts: 23

## Re: do within do loop

Thank you so much for your help !!
Super User
Posts: 6,629

## Re: do within do loop

Chris has the right idea, changing the limits on your loops.  The program will run faster if you don't check the combinations that you don't need to check.

Your original program is probably encountering this issue.  (I can't test it right now.)  The LEAVE statement only applies to a single loop.  So it gets you out of the interior loop, without getting you out of the exterior loop.  Just take that entire statement and repeat it after the first END statement.

Contributor
Posts: 23