Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: Re: How to detect the change of i over t?


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Re: How to detect the change of i over t?
Date   Mon, 9 Feb 2009 12:37:03 -0000

Your primary concern should be the unbalanced panels. Does t - 1 mean
what it says, or you intend the previous observation? 

For _balanced panels_, one line of code 

bysort i (t) : gen become = x == 1 & x[_n-1] == 0 

gets you most if not all of the way. 

The right-hand side (RHS) 

x == 1 & x[_n-1] == 0

is either true (numerically 1) or false (numerically 0). 

Benson is right to be concerned about boundary conditions. 

For the first observation in each panel, we are asking whether 

x[1] == 1 & x[0] == 0 

subscripts being evaluated within panels under the aegis of -by:-. 

But x[0] is evaluated as missing, and the RHS will be 0 (false). 

If Benson wants . for the first observation in each panel, then you can
do this 

bysort i (t) : gen become = cond(_n == 1, ., x == 1 & x[_n-1] == 0) 

For _unbalanced panels_, you may want something more like 

bysort i (t) : gen become = cond(_n == 1, ., x == 1 & L.x == 0) 

Nick 
[email protected] 

Martin Weiss

See NJC`s FAQ: http://www.stata.com/support/faqs/data/panel.html

*********
clear*
set obs 20
egen float id = seq(), from(1) to(4) block(5)
egen float t = seq(), from(1) to(5) block(1)
g x=cond(runiform()<=0.5,0,1)
g byte become=.
bys id: replace become=cond(x[_n-1]==0 & x[_n]==1,1,0)
list, noo sepby(id)
***********

Benson Limann
>
> I am using an unbalanced panel dataset. i is ID and t is time. Every
(i,t) 
> has a characteristic x=0 or 1.
>
> I want to generate a variable called "become" such that for each
(i,t):
>
> become=1, if x=0 at time t-1, and x=1 at time t
> become=0, otherwise
>
> How to write it? My primary concern is how to deal with the first t
for 
> each i--this observation does not have t-1.
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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