SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Generating Data with DO Loops
Constructing DO Loops


General form, DO loop:
DO index-variable=specification-1<,...specification-n>;
more SAS statements
END;

where

  • index-variable controls the execution of the DO group
  • specification includes start, stop, and increment values
  • SAS statements create, modify, or process variables
  • END terminates the loop.


When creating a DO loop with the iterative DO statement, you must specify an index variable. The index variable stores the value of the current iteration of the DO loop. Use any valid SAS name.
        V
     DO index-variable=specification-1...specification-n;
        more SAS statements
     END;

Next, specify the conditions that execute the DO loop. A simple specification contains a start value, a stop value, and an increment value for the DO loop.

                        s p e c i f i c a t i o n
                      |                          |
     DO index-variable=start TO stop BY increment;
        more SAS statements
     END;

The start value specifies the initial value of the index variable.

                        s p e c i f i c a t i o n
                      |V                         |
     DO index-variable=start TO stop BY increment;
        more SAS statements
     END;

The TO clause specifies the stop value. The stop value is the last index value that executes the DO loop.

                        s p e c i f i c a t i o n
                      |      V                   |
     DO index-variable=start TO stop BY increment;
        more SAS statements
     END;

The BY clause specifies an increment value for the index variable.

                        s p e c i f i c a t i o n
                      |              V           |
     DO index-variable=start TO stop BY increment;
        more SAS statements
     END;

The start, stop, and increment values can be any number, numeric variable, or SAS expression that results in a number. The BY clause is optional. Typically, you want the DO loop to increment by 1 for each iteration. If you do not specify a BY clause, the default increment value is 1.

For example, the specification below increments the index variable by 1, resulting in quiz values of 1, 2, 3, 4, 5:

     do quiz=1 to 5;

However, this specification increments the index variable by 2, resulting in rows values of 2, 4, 6, 8, 10, 12:

     do rows=2 to 12 by 2;

back||next


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

Terms of Use & Legal Information | Privacy Statement