Chapter Contents
Chapter Contents
Previous
Previous
Next
Next
Programming Statements

Iterative Execution

The DO statement also serves the feature of iteration. With a DO statement, you can repeatedly execute a set of statements until some condition stops the execution. A DO statement is iterative if you specify it with any of the following iteration clauses. The type of clause determines when to stop the repetition.

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 a given DO statement must be specified in the order listed in the preceding table.

DO DATA Statement

The general form of the DO DATA statement is

DO DATA ;

The DATA keyword specifies that iteration is to stop when an end-of-file condition occurs. The group is exited immediately upon encountering the end-of-file condition. Other DO specifications exit after tests are performed at the top or bottom of the loop. See Chapter 6, "Working with SAS Data Sets," and Chapter 7, "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 file is reached.

The following example reads data from an external file named MYDATA and inputs the data values into a vector. The data values are read one at a time into the dummy variable XX and collected into the vector X using the vertical concatenation operator (//) after each value is read.
   infile 'mydata';                    /* infile statement       */
   do data;                            /* begin read loop        */
      input xx;                        /* read a data value      */
      x=x//xx;                         /* concatenate values     */
   end;                                /* end loop               */


Iterative DO Statement

The general form of the iterative DO statement is

DO variable=start TO stop < BY increment > ;

The variable sequence specification assigns the start value to the given variable. 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 executes by multiples of 10 until I is greater than 100:
   do i=10 to 100 by 10;


DO WHILE Statement

The general form of the DO WHILE statement is

DO WHILE expression;

With a WHILE clause, the expression is evaluated at the beginning of each loop, with repetition continuing until the expression is false (that is, until the value contains a 0 or missing value). Note that if the expression is false the first time it is evaluated, the loop is not executed.

For example, if the variable COUNT has an initial value of 1, the statements
   do while(count<5);
      print count;
      count=count+1;
   end;
print COUNT four times.

DO UNTIL Statement

The general form of the DO UNTIL statement is

DO UNTIL expression;

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, if the variable COUNT has an initial value of 1, the statements

   do until(count>5);
      print count;
      count=count+1;
   end;
print COUNT five times.

Chapter Contents
Chapter Contents
Previous
Previous
Next
Next
Top
Top

Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.