SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Creating Variables
Accumulating Totals


To create a variable that accumulates values down observations, you can use a sum statement in your DATA step.


General form, sum statement:
variable+expression;

where

  • variable is a numeric variable that accumulates and retains values down observations. The variable is automatically set to 0 before the first observation is read.
  • expression is any valid SAS expression.

NOTE: If the expression produces a missing value, SAS software treats it as a 0.


The sum statement adds the result of the expression on the right side of the plus sign (+) to the numeric variable on the left side of the plus sign. Then the sum statement retains the new value for use in subsequent observations.

To find the total dollar amount for loan payments, you need a variable (in this example, TotalLoan) whose value begins at 0 and increases by the amount of the payment in each observation.


TotalLoan =Payment +previous total
   0.00      
 467.43 = 467.43 +    0.00
1426.00 = 958.57 +  467.43
1751.02 = 325.02 + 1426.00
2059.54 = 308.52 + 1751.02
2463.01 = 403.47 + 2059.54
2856.08 = 393.07 + 2463.01


In your DATA step, you create Finance.Newloan from Finance.Records. The variables Amount and Rate are dropped because they are not needed in the new data set. To create the total amount for loan payments, you use the sum statement shown below:
     data finance.newloan;
        set finance.records(drop=amount rate);
        TotalLoan+payment;
     run;

The value of the variable on the left of the plus sign (here, TotalLoan) begins at 0 and increases by the value of Payment with each observation.



back||next


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

Terms of Use & Legal Information | Privacy Statement