Statalist


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

st: generate with varying number of previous observations


From   "William Bishop" <[email protected]>
To   [email protected]
Subject   st: generate with varying number of previous observations
Date   Sat, 26 Jan 2008 07:15:07 -0800

I want to generate a variable by making calculations comparing the
current observation to ALL the previous observations with the same id.

Let's say I want to sum the difference in variable x for the
observation 5 vs all the previous obs. It would look something like:

gen diffx=(x[5]-x[4])+(x[5]-x[3])+(x[5]-x[2])+(x[5]-x[1])

So I'd like to calculate something like the above for a dataset.  The
result would look like:

id obs x diffx
1 1 2 0
1 2 5 3
1 3 6 5
1 4 8 11
1 5 11 23
2 1 3 0
2 2 4 1
2 3 6 5
2 4 7 8
2 5 9 16
2 6 11 26

I believe the solution is to use something more like:

gen diffx=(x[_n]-x[_n-1])+(x[_n]-x[_n-2])+(x[_n]-x[_n-3])+(x[_n]-x[_n-4])

This works if there are exactly 5 observations, but what if there is a
different number?

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