Published on :
ETL SASHELP

Understanding the SUM Statement and Accumulation

This code is also available in: Deutsch Español Français
Awaiting validation
This script compares three techniques to calculate a running total of the 'age' variable from the sashelp.class table. It demonstrates the equivalence and nuances between the RETAIN statement combined with a simple addition, the SAS© sum statement (variable+expression), and the use of the SUM function with RETAIN.
Data Analysis

Type : SASHELP


The data comes from the standard SASHELP.CLASS example table.

1 Code Block
DATA STEP Data
Explanation :
Using the RETAIN statement to preserve the value of 'totage' from one observation to the next, initialized to 0, and manually adding the age.
Copied!
1DATA totalage;
2 SET sashelp.class;
3 retain totage 0;
4 totage = totage+age;
5 RUN;
2 Code Block
DATA STEP Data
Explanation :
Using the Sum Statement 'variable+expression'. This syntax automatically implies RETAIN and handles missing values as zeros for accumulation.
Copied!
1DATA totalage;
2 SET sashelp.class;
3 totage+age;
4 RUN;
3 Code Block
DATA STEP Data
Explanation :
Using the SUM function with explicit RETAIN. The SUM function ignores missing values, unlike the simple '+' operator used in the first example.
Copied!
1DATA totalage;
2 SET sashelp.class;
3 retain totage 0;
4 totage = sum(totage,age);
5 RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.