How to Calculate a Cumulative Sum in SAS (With Example)

To calculate a cumulative sum in SAS, you first need to create a data set with values and then use the Cumulative Sum (CUSUM) function to calculate the cumulative sum of those values. You can then use a PROC PRINT or PROC MEANS statement to display the results. An example of how to calculate a cumulative sum in SAS would be to use the CUSUM function to calculate the cumulative sum of a variable called SALES over a set of observations and then use a PROC PRINT statement to display the results.


You can use the following basic syntax to calculate a cumulative sum in SAS:

data new_data;
    set original_data;
    retain cum_sum;
    cum_sum+sales;
run;

This particular syntax creates a new dataset called new_data that contains a new column called cum_sum that contains the cumulative values of the column called sales.

The following example shows how to use this syntax in practice.

Example: Calculate a Cumulative Sum in SAS

Suppose we have the following dataset in SAS that shows the number of sales made by some store during 10 consecutive days:

/*create dataset*/
data original_data;
    input day sales;
    datalines;
1 7
2 12
3 14
4 12
5 16
6 18
7 11
8 10
9 14
10 17
;
run;

/*view dataset*/
proc print data=original_data;

The following code shows how to create a new dataset that calculates the cumulative sum of values in the sales column:

/*calculate cumulative sum of sales*/
data new_data;
    set original_data;
    retain cum_sum;
    cum_sum+sales;
run;

/*view results*/
proc print data=new_data;

The new column called cum_sum contains the cumulative sum of values in the sales column.

For example:

  • Cumulative Sum on Day 1: 7
  • Cumulative Sum on Day 2: 7 + 12 = 19
  • Cumulative Sum on Day 3: 7 + 12 + 14 = 33

And so on.

The following tutorials explain how to perform other common tasks in SAS:

x