Generating Data with DO Loops |
Constructing DO
Loops |
In the previous practice, you might have noticed that the
value of year stored in the Work.Earn data
set is 21 rather than 20. To understand why, let's follow the execution of
the DO loop. |
data work.earn; Value=2000; do year=1 to 20; Interest=value*.075; value+interest; end; run; |
SAS Data Set Work.Earn
|
When the DO loop begins to execute, the value of the index variable,
year , is initialized to 1 and the stop value is set to 20. The
DO loop executes as long as the index value does not exceed the stop value,
20. When the value of year reaches 20, the iterative DO statement
evaluates the value as being within the range set for the start and stop
values, and the DO loop executes one last time.
data work.earn; Value=2000; 20 do year=1 to 20; Interest=value*.075; value+interest; end; run;
At the END statement, the index variable data work.earn; Value=2000; 21 do year=1 to 20; Interest=value*.075; value+interest; end; run;
Any remaining statements in the DATA step now execute. In this example, the
end of the DATA step is reached and the values are written to the data set.
When the values are written to the data set, the last calculated values of
|
SAS Data Set Work.Earn
Value
year
Interest
8495.70
21
592.723
Remember that the purpose of the index variable is to control the execution of the DO loop. The index variable always increments one value beyond the stop value unless you terminate the DO loop in some other manner. |
Copyright © 2002 SAS Institute Inc.,
Cary, NC, USA. All rights reserved.