Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: RE: RE: Calculating running cumulative totals (2.1)


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: RE: RE: Calculating running cumulative totals (2.1)
Date   Thu, 17 Apr 2003 09:18:55 +0100

> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of Nick Cox
> Sent: 17 April 2003 09:11
> To: statalist@hsphsun2.harvard.edu
> Subject: st: RE: RE: RE: Calculating running cumulative totals (2)
>
>
> Adam Chapman
>
> > > > I'm trying to calculate a running cumulative total for
> > > observations over
> > > > the previous 12 months (cum12month).  My data looks like:
> > > >
> > > > # Obs  Month  Cum12month
> > > > 2	1	2
> > > > 4	2	6
> > > > 3	3	9
> > > > 1	4	10
> > > > 2	5	12
> > > > 4	6	16
> > > > 2	7	18
> > > > 1	8	19
> > > > 5	9	24
> > > > 3	10	27
> > > > 2	11	29
> > > > 4	12	33
> > > > 5	13	36
> > > > 2	14	34
> > > > 3	15	34
> > > > 2	16	35
> > > > 4	17	37
> > > > etc
>
> Nick Cox
>
> > gen Cum12month = sum(Obs)
> > replace Cum12month = Cum12month - Cum12month[_n-12] if month > 13
>
> Another way:
>
> tsset month
> gen Cum12month = sum(Obs) - sum(L12.Obs)
>
> Both these methods extend to panel data. The second is more
> easily done (and works better if there are gaps):
>
> tsset id month
> gen Cum12month = sum(Obs) - sum(L12.Obs)

Meaning (!)

bysort id (month) : gen Cum12month = sum(Obs) - sum(L12.obs)

Nick
n.j.cox@durham.ac.uk

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index