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 the iterations stop.

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 as follows:

DO DATA ;

The DATA keyword specifies that iteration is to stop when an end-of-file condition occurs. The group is exited immediately when the end-of-file condition is encountered. Other DO specifications exit after tests are performed at the top or bottom of the loop.

See Chapter 6 and Chapter 7 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 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 by 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 as follows:

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 as follows:

DO WHILE expression;

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 value is 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 following statements print COUNT four times:
  
    do while(count<5); 
       print count; 
       count=count+1; 
    end;
 


DO UNTIL Statement

The general form of the DO UNTIL statement is as follows:

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 following statements print COUNT five times:

  
    do until(count>5); 
       print count; 
       count=count+1; 
    end;
 

Previous Page | Next Page | Top of Page