SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Creating Multiple Observations from a Single Record
Reading the Same Number of Repeating Fields


Executing SAS Statements Based on a Variable Value

Each record contains four different values for Sales, so the INPUT statement must execute four times. Rather than writing four INPUT statements, you can execute one INPUT statement repetitively in an iterative DO loop.


General form, iterative DO loop:
DO index-variable=specification-1
     
< , . . . specification-n>;

       . . . more SAS statements . . .
END;

where

  • index-variable names a variable whose value governs the execution of the DO loop.

  • specification can be a
    • discrete value such as 1, 45, 10342
    • range of values such as 1 to 5 or 10 to 100 by 10.

  • more SAS statements are statements that are executed within the loop. END closes the DO loop and returns control to the DO statement.

NOTE: When a discrete value is specified for the index-variable, the loop executes only once for that value.


In the following example, the index-variable count is assigned an initial value of 1. The expression count=1 is evaluated before the statements in the loop are executed. The END statement closes the loop and passes control back to the DO statement, where the value of count is then set to 16. The expression count=16 is evaluated and the loop executes once more. Now there are no more values for count, so the loop stops.

     do count=1,16;
Cost=salary+equipment;
end;
If the specification is a range of values, the loop executes from the initial value to the ending value. For example, the following statement executes the loop when the value of count is 1, 2, 3, and 4. The count is incremented at the bottom of the loop. When the value of count exceeds 4, the loop does not execute.

     do count=1 to 4;
Cost=salary+equipment;
end;
The default increment is one; however, you can specify another increment. Here the value of count is incremented by two, so the loop executes only when the value of count is 2, 4, 6, and 8.

     do count=2 to 8 by 2;
Cost=salary+equipment;
end;

back||next


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

Terms of Use & Legal Information | Privacy Statement