SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

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


Creating a Counter Variable

Because the DO WHILE statement does not create an index-variable, you can create your own "counter" variable. You can then use a sum statement to increment the value of the accumulator variable each time the DO WHILE loop executes.


General form, sum statement:
variable+expression; 

where

  • variable specifies the name of an accumulator variable, which contains a numeric value
  • expression is any valid SAS expression.
CAUTION: The sum statement adds the result of expression to the accumulator variable. Examples of valid sum statements that illustrate various expressions include:
sumxsq+x*x;   balance+(-debit);


In the example below, the assignment statement before the loop creates the accumulator variable Quarter and assigns it an initial value of zero. Each time the DO WHILE loop executes, the sum statement increments the value of Quarter by one.

     data perm.sales97;
        infile data97 missover;
        input ID $4. Sales : comma. @;
        Quarter=0;
        do while (sales ne .);
           quarter+1;

back||next


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

Terms of Use & Legal Information | Privacy Statement