Showing posts with label SAS statement leave and continue in DO loop. Show all posts
Showing posts with label SAS statement leave and continue in DO loop. Show all posts

Wednesday, November 18, 2009

do leave loop and continue !!!

There are two statements in SAS, leave and continue. Difference between two is seldom not understood.

data leave;
do i = 1 to 5;
do j = 1 to 5;
if j = 2 or j = 4 then leave;
output;
end;
end;
run;

Here SAS will leave inner do loop completely once the first condition j=2 gets satisfied. So in output you will never have j = 2 or 3 or 4 or 5.

OUTPUT
i j
1 1
2 1
3 1
4 1
5 1

Same program with continue statement.


data continue;
do i = 1 to 5;
do j = 1 to 5;
if j = 2 or j = 4 then continue;
output;
end;
end;
run;

Here SAS will skip the inner loop for both conditions j = 2 and j=4, but it will not completely leave the loop. So ouput for this will look like

i j
1 1
1 3
1 5
2 1
2 3
2 5
. .
. .
. .
5 3
5 5