Date: Sat, 12 Jan 2008 16:30:22 +0800
Reply-To: "Hu, Jiangtang" <jiangtanghu@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Hu, Jiangtang" <jiangtanghu@GMAIL.COM>
Subject: Re: cumulative sum with subgroups ?
In-Reply-To: <43C07A163F7E764A8B27F6DAE2B126BF19BC32E6@tpwd-mx0.tpwd.state.tx.us>
Content-Type: text/plain; charset=ISO-8859-1
A modified version of Warren's, delete "retain cumsum", and replace
"cumsum=sumnb+cumsum" with " cumsum+sumnb":
data out;
set test;
by id;
if first.id then cumsum=sumnb;
else cumsum+sumnb;
run;
On Jan 12, 2008 1:15 AM, Warren Schlechte <Warren.Schlechte@tpwd.state.tx.us>
wrote:
> 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
>
--
John Jiangtang Hu
|