The DO statement supports clauses that iterate over compound statements. With an iterative DO statement, you can repeatedly execute a set of statements until some condition stops the execution. The following table lists the different kinds of iteration statements in the SAS/IML language:
Clause |
DO Statement |
|
---|---|---|
DATA |
DO DATA statement |
|
variable = start TO stop < BY increment > |
Iterative DO statement |
|
WHILE(expression) |
DO WHILE statement |
|
UNTIL(expression) |
DO UNTIL statement |
A DO statement can have any combination of these four iteration clauses, but the clauses must be specified in the order listed in the preceding table.
The general form of the DO DATA statement is as follows:
The DATA keyword specifies that iteration stops when an end-of-file condition occurs. Other DO specifications exit after tests are performed at the top or bottom of a loop.
See Chapter 7, Working with SAS Data Sets, and Chapter 8, File Access, for more information about processing data.
You can use the DO DATA statement to read data from an external file or to process observations from a SAS data set. In the DATA step in Base SAS software, the iteration is usually implied. The DO DATA statement simulates this iteration until the end of a file is reached.
The following example reads data from an external file named MyData.txt that contains the following data:
Rick |
2.40 |
3.30 |
Robert |
3.90 |
4.00 |
Simon |
3.85 |
4.25 |
The data values are read one at a time into the scalar variables Name, x, and y.
filename MyFile 'MyData.txt'; infile MyFile; /* infile statement */ do data; /* begin read loop */ input Name $6. x y; /* read a data value */ /* do something with each value */ end; closefile MyFile;
The general form of the iterative DO statement is as follows:
The value of the variable matrix is initialized to the value of the start matrix. This value is then incremented by the increment value (or by 1 if increment is not specified) until it is greater than or equal to the stop value. (If increment is negative, then the iterations stop when the value is less than or equal to stop.)
For example, the following statement specifies a DO loop that initializes i to the value 1 and increments i by 2 after each loop. The loop ends when the value of i is greater than 10.
y = 0; do i = 1 to 10 by 2; y = y + i; end;
The general form of the DO WHILE statement is as follows:
With a WHILE clause, the expression is evaluated at the beginning of each loop, with iterations continuing until the expression is false (that is, until the expression contains a zero or a missing value). Note that if the expression is false the first time it is evaluated, the loop is not executed.
For example, the following statements initialize count to 1 and then increment count four times:
count = 1; do while(count<5); count = count+1; end;
The general form of the DO UNTIL statement is as follows:
The UNTIL clause is like the WHILE clause except that the expression is evaluated at the bottom of the loop. This means that the loop always executes at least once.
For example, the following statements initialize count to 1 and then increment count five times:
count = 1; do until(count>5); count = count+1; end;