Date: Mon, 14 Jan 2008 04:40:28 -0800
Reply-To: ChrisG <chris.godlewski@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: ChrisG <chris.godlewski@GMAIL.COM>
Organization: http://groups.google.com
Subject: Re: cumulative sum with subgroups ?
Content-Type: text/plain; charset=ISO-8859-1
On 12 jan, 09:30, jiangtan...@GMAIL.COM ("Hu, Jiangtang") wrote:
> 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.Schlec...@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.godlew...@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
thanks to everybody !!!
it works !
cheers
CG
|