Statalist The Stata Listserver


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

st: how to create an average of previous observations


From   Daniel Simon <[email protected]>
To   [email protected]
Subject   st: how to create an average of previous observations
Date   Wed, 18 Oct 2006 14:40:07 -0400

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/




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