Date: Fri, 11 Jan 2008 11:15:25 -0600
Reply-To: Warren Schlechte <Warren.Schlechte@TPWD.STATE.TX.US>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Warren Schlechte <Warren.Schlechte@TPWD.STATE.TX.US>
Subject: Re: cumulative sum with subgroups ?
Content-Type: text/plain; charset="us-ascii"
data test;
input id date nnb sumnb;
datalines;
1 10 2 15
1 11 1 15
1 12 1 20
1 13 1 10
2 7 1 150
2 9 1 200
2 10 2 150
2 17 1 300
;
run;
proc sort data=test; by id;run;
data out;
set test;
by id;
retain cumsum;
if first.id then cumsum=sumnb; else cumsum=sumnb+cumsum;
run;
proc fsview data=out;run;
Warren Schlechte
-----Original Message-----
From: ChrisG [mailto:chris.godlewski@GMAIL.COM]
Sent: Friday, January 11, 2008 10:51 AM
Subject: cumulative sum with subgroups ?
Hi
Suppose i have such dataset :
id date nnb sumnb
1 10 2 15
1 11 1 15
1 12 1 20
1 13 1 10
2 7 1 150
2 9 1 200
2 10 2 150
2 17 1 300
i would like sas to do the following thing in a additional variable,
let call it cumsumnb :
id date nnb sumnb cumsumnb
1 10 2 15 15
1 11 1 15 30
1 12 1 20 50
1 13 1 10 60
2 7 1 150 150
2 9 1 200 350
2 10 2 150 500
2 17 1 300 800
in other words, to calculate in an 'incremental way' the cumulative
sum of sumnb by date within the id subgroup ...
i hope i was clear enough ...
many thanks for any help !
cheers
CG