SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Generating Data with DO Loops
Constructing DO Loops


Decrementing DO Loops

You can decrement a DO loop by specifying a negative value for the BY clause. For example, the specification in this iterative DO statement decreases the index variable by 1, resulting in values of 5, 4, 3, 2, 1.

                                  V
     DO index-variable=5 to 1 by -1;
        more SAS statements
     END;

When you use a negative BY clause value, the start value must always be greater than the stop value in order to decrease the index variable during each iteration.

                     start stop
                       V    V
     DO index-variable=5 to 1 by -1;
        more SAS statements
     END;


Specifying a Series of Items

You can also specify how many times a DO loop executes by listing items in a series.

     DO index-variable=2,5,9,13,27;
        more SAS statements
     END;

When the DO loop executes, it executes once for each item in the series. The index variable equals the value of the current item. You must separate each item in the series with a comma.

To list items in a series, you must specify either

  • all numeric values
     DO index-variable=2,5,9,13,27;
        more SAS statements
     END;

  • all character values, with each value enclosed in quotation marks
     DO index-variable='MON','TUE','WED','THR','FRI';
        more SAS statements
     END;

  • all variable names.
     DO index-variable=win,place,show;
        more SAS statements
     END;

Variable names must represent either all numeric or all character values. Do not enclose variable names in quotation marks.


back||next


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

Terms of Use & Legal Information | Privacy Statement