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

st: RE: RE: Calculating running cumulative totals


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: Calculating running cumulative totals
Date   Wed, 16 Apr 2003 09:53:51 +0100

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
> > 

Kieran McCaul
 
> sort month
> gen cum12month=month[1]
> replace cum12month=month[_n]+ cum12month[_n-1] if _n>1

That's not what Adam is looking for, setting aside 
the detail that he is summing Obs, not Month. 
If the cumulative sum were what was wanted, 
we could get it directly by 

gen Cum12month = sum(Obs)  

However, that is also the correct solution -- for the 
first twelve months -- or more precisely coorect if we are 
treating months before the start of the data as if they 
contained zero values, as does the calculation behind 
Adam's worked example. 

But we certainly need to fix it for months 13 on. The 
sum over the last twelve months is the 

cumulative sum so far 

MINUS the 

cumulative sum 12 months ago 

so we 

replace Cum12month = Cum12month - Cum12month[_n-12] if month > 13 

Note that this avoids a loop over observations, always expensive. 
Note also that you can do a lot of arithmetic within the subscript. 

Nick 
[email protected] 

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index