Generating Data with DO Loops |
Constructing DO
Loops |
General form, DO loop:
where
|
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 do quiz=1 to 5;
However, this specification increments the index variable by 2, resulting
in do rows=2 to 12 by 2; |
Copyright © 2002 SAS Institute Inc.,
Cary, NC, USA. All rights reserved.