Loops


SAS has 3 types of loops available in the DATA step: interative DO, DO-WHILE and DO-UNTIL. The most simple is the iterative DO loop.

The iterative DO loop allows you to step through a set of statements, for a given number of loops. Each step of the loop is performed on the same observation.

Example:

DATA data1;
  DO i = 1 TO 10;
    x = ranuni(-1);
    output;
  END;

RUN;
The above example generates a uniform random number and outputs it to the dataset "data1". These steps are inside a loop that will generate and output 10 random numbers.

The variable "i" is an indicator variable that indicates the step number of the loop.

The DO-WHILE and the DO-UNTIL loops are similar, except that they don't have a specified number of steps like the iterative DO loop does. Instead, they each have a condition expression and continute to loop either while the condition is true, or until the expression becomes true, thus their names.