*34*

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.

**Additional Resources**

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

How to Calculate the Sum by Group in SAS

How to Calculate the Mean by Group in SAS

How to Calculate a Moving Average in SAS