Statalist


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

RE: st: let loop stop when the first variable x becomes 0


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: let loop stop when the first variable x becomes 0
Date   Fri, 20 Jun 2008 13:22:03 +0100

Keith's answer is in essence identical to mine. 

But to me such a solution is still less than ideal. You would be much better off, I guess, reshaping your data to long. That will make most things you might want to do with such data much easier. 

reshape long x, i(ID) j(year) 
bysort ID (x year) : gen firstzero = year if x[1] == 0 

If you really want the present data structure, you can 

reshape wide 

to get it back. 

Nick 
[email protected] 

Man Jia

Thanks very much for your help! It works well.  I appreciate it.

Keith Dear 

>>You could do it like this:
>>
>>gen int firstzero=.
>>forval y=1995/2002 {
>>         replace firstzero=`y' if firstzero==. & x`y'==0
>>}

>>>I have a data set like this:
>>>-------------------------------------------------------------------
>>>ID,x1995, x1996, x1997, x1998, x1999, x2000, x2001, x2002
>>>
>>>  1   1         0          1          1       0          1          0
>>>   0
>>>  2   0         0          0          0       1          0         0
>>>   0
>>>  3   1         1          0          1       0          0         0
>>>   0
>>>---------------------------------------------------------------------
>>>I want to find out for each observation the first year when the value
>>>of the variable x becomes 0.
>>>
>>>For example, for observation 1, the first year x becomes 0 is 1996. For
>>>observation 2 and 3, 1995 and 1997 respectively.
>>>
>>>I'm not sure how to let the "searching the first 0" loop stop when x
>>>becomes 0. I checked STATA's help; I was wondering if the command
>>>"continue" will work here. Could someone give me some hints about this?

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