Not applicable
Posts: 0

# Cumulative sum

data is as follows
date meter demand cal;
1 A 5 5
2 B 10 15
3 C 15 30
4 D 20 50
5 E 25 75
6 F 30 105
7 G 35 140
8 H 40 1 80

in this table
there are three variables and "cal" is the result i require
so
i need the code such as i get the cumulative sum as shown in "Cal"
my input are date,meter(charachter) and demand and i need "cal" which
is shown in the table
and i need to sort it by meter and date
like for meter A (date 1 to 8) its "cal"
then meter B (date 1 to 8) its "cal"
thanks
Super Contributor
Posts: 474

## Re: Cumulative sum

Hi!

First sort the data as you wish.
see PROC SORT procedure: http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/sort-overview.htm

Then through datastep add the DEMAND variable to a CAL retained variable.
see how to RETAIN a value: http://support.sas.com/documentation/cdl/en/lrdict/61724/HTML/default/a000214163.htm

Cheers from Portugal.

Daniel Santos @ www.cgd.pt.
Super User
Posts: 13,577

## Re: Cumulative sum

An approach without datastep would be:

proc sort data=; by meter date;run;

proc freq data= noprint;
by meter;
tables date/list outcum out= (drop=percent cum_pct rename=(count=demand cum_freq=cal));
weight demand;
run;

You'll have labels for the demand and cal variable related to frequency count but that's fixeable lots of ways. May not do what you want if any demand values are missing or negative.
New Contributor
Posts: 4

## Re: Cumulative sum

A CUMSUM
1 5 5
2 10 15
3 15 30
4 20 50
5 25 75
6 30 105
7 35 140
8 40 180

Hello,

I just want to calculate CUMSUM column (cumulative sum of column A). Do I still have to use RETAIN? I found the following sample code, but;

data a;
set a;
xsum + x;
run;

Thanks
PROC Star
Posts: 2,369

## Re: Cumulative sum

using the syntax

A + B;

(with no = sign) does an implicit retain.
Discussion stats
• 4 replies
• 13251 views
• 0 likes
• 5 in conversation