Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Re: Average and count from a given number of previous observations


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Re: Average and count from a given number of previous observations
Date   Tue, 11 Dec 2007 19:14:38 -0000

This can be done more directly without any loops or 
multiple new variables. 

Generate the cumulative rainfall 

. gen curain = sum(rain) 

The rain in the previous 28 days is then 

. gen prev28 = curain[_n-1] - curain[_n-29] 

subject to details about the precise definition. 

I learned this trick from Michael Blasnik. 

Nick
[email protected] 

Steven Proud

One way of doing this would be with a loop

so, could:
tsset date
local i=1
while `i'<=84 {
gen l`i'rainfall=l`i'.rainfall
local i=`i'+1
}
gen lag28rainfall=rsum(l1rainfall-l28rainfall)
gen lag84rainfall=rsum(l1rainfall-l84rainfall)
drop l1rainfall-l84rainfall


gen rain=0
replace rain=1 if rainfall>0
local i=1
while `i'<=84 {
gen l`i'rain=l`i'.rain
local i=`i'+1
}

gen lag28rain=rsum(l1rain-l28rain)
gen lag84rain=rsum(l1rain-l84rain)
drop l1rain-l84rain

I think that should work


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