Do-loops are used to repeat a sequence of operations. Here is an example.
Each row of data in the file iris.dat contains 12 measurements:
measurements of four characteristics for each of the 3 species of iris.
The 4 measurements are to be read into the 4 variables
m1-m4 and an
species will contain a number designating the
type of iris from which the measurements were taken, say, 1 for iris
setosa, 2 for versicolor, 3 for virginica.
The do loop reads in turn the 4 measurements for each type
in a given row of the data set and defines the variable
Note that the do loop MUST BE paired with an
statement to mark the end of the loop.
The output statement is used to write the variables to the data set being created. If the output statement above were omitted, only the last group of measurments would appear in the SAS data set.
The counter for a do-loop can take values specified by a comma delimited list. Examples:
do sex='m','f'; ... end; do parity=1,3,5; ... end;
A counterless do-loop can be used to have more complicated processing after an if-branch. Example:
data old; input age sex income; if age<30 then do; if income<20000 then cat=1; if income>20000 and income <30000 then cat=2; end; if age>= 30 then do; ....
Do-loops and if-then statements can be nested.
The continue statement can be used to branch to the bottom of the inner most enclosing do loop.
The return statement can be used to branch to the bottom of the data step program.
The stop command can be used to end datastep processing.