Statalist


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

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


From   Austin Nichols <[email protected]>
To   [email protected]
Subject   Re: st: RE: Re: How to detect the change of i over t?
Date   Mon, 9 Feb 2009 09:48:25 -0500

Nick--
I think you mean:

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

since _n!=1 is no guarantee that x and L.x are nonmissing.

On Mon, Feb 9, 2009 at 7:37 AM, Nick Cox <[email protected]> wrote:
> 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]
*
*   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