How do I calculate a cumulative sum in SAS, and can you provide an example? 2

How do I calculate a cumulative sum in SAS, and can you provide an example?

Calculating a cumulative sum in SAS refers to the process of obtaining a running total of a series of values in a dataset. This can be useful in analyzing and tracking the growth or accumulation of a particular variable over time. To calculate a cumulative sum in SAS, the sum function can be used in conjunction with the retain statement to continuously add the previous sum to the current value. An example of this would be:

data cumsum;
set dataset;
retain total 0;
total + variable;
run;

This code will create a new variable called “total” which will contain the cumulative sum of the variable in the dataset. This process can be repeated for multiple variables or across different time periods to get a comprehensive view of the cumulative trends in the data.

Calculate a Cumulative Sum in SAS (With Example)


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 printdata=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 printdata=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:

Cite this article

stats writer (2024). How do I calculate a cumulative sum in SAS, and can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-i-calculate-a-cumulative-sum-in-sas-and-can-you-provide-an-example/

stats writer. "How do I calculate a cumulative sum in SAS, and can you provide an example?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-do-i-calculate-a-cumulative-sum-in-sas-and-can-you-provide-an-example/.

stats writer. "How do I calculate a cumulative sum in SAS, and can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-i-calculate-a-cumulative-sum-in-sas-and-can-you-provide-an-example/.

stats writer (2024) 'How do I calculate a cumulative sum in SAS, and can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-i-calculate-a-cumulative-sum-in-sas-and-can-you-provide-an-example/.

[1] stats writer, "How do I calculate a cumulative sum in SAS, and can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How do I calculate a cumulative sum in SAS, and can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top