SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Generating Data with DO Loops
Lesson Summary

This page contains


I. Text Summary

To go to the page where a task, programming feature, or concept was presented, select a link.


Purpose of DO Loops
DO loops process groups of SAS statements repeatedly, reducing the number of statements required in repetitive calculations.

Syntax of DO Loops
To construct a DO loop, specify an index variable and the conditions that will execute the loop. These include a start value for the index variable, a stop value, and an increment value. Start, stop, and increment values can be any number, numeric variable, or SAS expression that results in a number.

DO Loop Execution
During each iteration of a DO loop new values are created in the SAS program data vector. Once the loop's index value exceeds the stop value, the DO loop stops and processing continues with the following DATA step statement.

Counting DO Loop Iterations
A simple way to track DO loop iterations is to create a temporary counting variable, then drop this variable from the data set. Or, include an OUTPUT statement within the DO loop to write an observation for each iteration. This overrides automatic output at the end of the DATA step.

Decrementing DO Loops
You can decrement a DO loop by specifying a negative value for the BY clause. The start value must be greater than the stop value.

Specifying a Series of Items
You can specify how many times a DO loop executes by listing items in a series; the DO loop will execute once for each item, with the index variable equal to the value of each item. A series can consist of all numeric values, all character values (enclosed in quotation marks), or all variable names (without quotation marks).

Nesting DO Loops
DO loops can run within DO loops, as long as you assign each loop a unique index variable and terminate each DO loop with its own END statement.

Iteratively Processing Data Read from a Data Set
You can use a DO loop to read a data set and compute the value of a new variable. DO loop start and stop values, for example, can be read from a data set.

Conditionally Executing DO Loops
The DO UNTIL statement executes a DO loop until a condition is true. Because the expression is not evaluated until the bottom of the loop, a DO UNTIL loop will execute at least once. The DO WHILE statement is used to execute a DO loop while a condition is true. Because the DO WHILE statement is evaluated at the top of the DO loop, if the expression is false the first time it is evaluated, the DO loop never executes.

Conditional Clauses within Iterative DO Statements
DO WHILE and DO UNTIL statements can be used within iterative DO loops to combine conditional and unconditional execution.


II. Syntax

To go to the page where a statement or option was presented, select a link.

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

DO UNTIL(expression);

DO WHILE(expression);

DO index-variable=start TO stop BY increment UNTIL(expression);

DO index-variable=start TO stop BY increment WHILE(expression);

III. Sample Programs

     data work.earn;
        Value=2000;
        do year=1 to 20;
           Interest=value*(.075/12);
           value+interest;
        end;
     run;

     data work.earn;
        do year=1 to 20;
           Capital+2000;
           do month=1 to 12;
              Interest=capital*(.075/12);
              capital+interest;
           end;
        end;
     run;

     data work.invest(drop=i);
        do i=1 to 10 until(capital>50000);
           Year+1;
           Capital+2000;
           capital+capital*.10;
        end;
     run;


IV. Points to Remember
  • If you do not specify a BY clause, the increment value for DO loops is 1.

  • In most cases, the index variable is only needed to process the DO loop and can be dropped from the data set.

  • The index variable always increments one value beyond the stop value unless you terminate the DO loop in some other manner.

  • It's easier to manage nested DO loops if you indent the statements in each loop.


  back||next

 

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

Terms of Use & Legal Information | Privacy Statement