Help using Base SAS procedures

Cumulative sum

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
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"
so, please do reply if u have an answer for this query
Super Contributor
Posts: 474

Re: Cumulative sum


First sort the data as you wish.
see PROC SORT procedure:

Then through datastep add the DEMAND variable to a CAL retained variable.
see how to RETAIN a value:

Cheers from Portugal.

Daniel Santos @
Super User
Posts: 10,483

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;

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

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


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;

Posts: 1,561

Re: Cumulative sum

using the syntax

A + B;

(with no = sign) does an implicit retain.
Ask a Question
Discussion stats
  • 4 replies
  • 5 in conversation