Reka Sundaram-Stukel
> > I am trying to create a lag y for data that looks like
> the following:
> >
> > id type choice Year lag_choice
> > 1 a 1 1 -
> > 1 b 0 1 -
> > 1 c 0 1 -
> > 1 d 0 1 -
> > 1 a 0 2 1
> > 1 b 0 2 0
> > 1 c 1 2 0
> > 1 d 0 2 0
> >
> > how do I generate lag_choice. I have tried
> >
> > by id type: gen lag_choice=choice[_n-1] and various
> combinations of this
> > quite unsuccessfully. Help please
Oleksandr Talavera, BC
> Try the following:
> *----------
> egen type1 = group(type)
> g newid = 10^6*type1+id
> tsset newid year
> g lag_choice=L.choice
> sort id year type
Oleksandr's approach works. What's not
clear is why Reka reported failure.
bysort id type (year) : gen lag_choice = choice[_n-1]
works with this example.
The nuance separating this from an
approach based on -tsset- is that [_n-1]
looks backwards across a gap, but L. doesn't.
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/