| |
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
Re: st: Re: how to create an average of previous observations
works perfectly! thanks a lot, Michael.
Daniel
At 03:02 PM 10/18/2006 -0400, you wrote:
How about
sort date
gen x_avg=sum(x[_n-1])/sum(x[_n-1]<.)
by date: replace x_avg=x_avg[1]
Michael Blasnik
----- Original Message ----- From: "Daniel Simon" <[email protected]>
To: <[email protected]>
Sent: Wednesday, October 18, 2006 2:40 PM
Subject: st: how to create an average of previous observations
Hi - I have data on a variable, X, for a set of individuals. Each
observation also has the date that X was recorded. I want to create a new
variable, X_AVG, which is the mean value of X for all observations with a
date that precedes the date for the current observation. X_AVG would be a
moving average, that would incorporate all observations with earlier dates.
Example:
Obs Date X X_AVG
1 1/1/2004 4 .
2 1/2/2004 3 4
3 1/4/2004 5 3.5
4 1/5/2004 2 4
5 1/8/2004 8 3.5
6 1/10/2004 5 4.4
7 1/10/2004 8 4.4
8 1/11/2004 3 5
I want to know how to create X_AVG. And, in particular, is there a way to
deal with cases like observation 7, which has the same date as
observation 6. In that case, I would not want to use the value of X in
observation 6 to create X_AVG for observation 7.
I hope that this is clear enough. Thanks. Daniel
*
* 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/
*
* 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/
*
* 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/