SAS continuous loop

847 views Asked by At
data m1;
set t1;
do i = 1 to 29;
    set t2 point=i;
end;
run;

data test;
do i=1 to 29;
    set t2 point=i;
end;
run;

I can understand why it is a continuous loop for data test but I don't know how to explain why data m1 is not a infinite loop. (t1 has 29 records)

Does anyone have any ideas? Thanks for your help.

3

There are 3 answers

1
DomPazz On

The first SET statement in the M1 step defines when the DATA Step stops. It sees the end of the T1 data set and stops.

0
BellevueBob On

Data steps are "stopped" whenever an end-of-file condition is raised (attempting to read from a file or data set when no more records exist).

In your first example, the data step reads records from a table (data set) named t1 followed by reading 29 records from table t2. The output table m1 will have the same number of records as exist in t1. Once the step attempts to read a new record from t1, the step will immediately stop.

Your second example runs forever because it never encounters an end-of-file condition. It will continue to output records until you either run out of disk space or you interrupt the step.

0
Yick Leung On

What the others have said is right. The SAS data step is an implicit loop that will end when a particular condition is met. I think the second data step is continuously infinite because there is no set dataset statement outside of the do loop. Hence, the data step loop does not know anything about the end of file (EOF). The point inside the do loop does not observe the EOF, so it cannot stop.

In data test, a conditional trigger for ending the infinite loop was the set statement outside of the do loop because data sets have a finite number of entries. This means that EOF is reachable. Once the data set loop is informed that the EOF has been reached and can read no further, it will end the loop.

In addition, other triggers could also be used such as the stop; statement or prompting the user to press some keys to end the loop.