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   Keith Dear <keith.dear@anu.edu.au>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: let loop stop when the first variable x becomes 0
Date   Fri, 20 Jun 2008 10:33:31 +1000

Oops - needs to be an "int" variable to hold years:
gen int firstzero=.

At 10:26 AM 20/06/2008, you wrote:
Mandy,
You could do it like this:

gen byte firstzero=.
forval y=1995/2002 {
        replace firstzero=`y' if firstzero==. & x`y'==0
}

Keith



At 10:05 AM 20/06/2008, you wrote:
Dear Statalists,

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?

Thanks a lot!

Mandy Jia


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

Dr Keith B.G. Dear
Senior Fellow in Biostatistics
National Centre for Epidemiology and Population Health
Australian National University
Canberra, ACT 0200, Australia
Tel: 02 612 54865, Fax: 02 612 50740
http://nceph.anu.edu.au/Staff_Students/staff_pages/dear.php
CRICOS provider #00120C

http://canberragliding.org/

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

Dr Keith B.G. Dear
Senior Fellow in Biostatistics
National Centre for Epidemiology and Population Health
Australian National University
Canberra, ACT 0200, Australia
Tel: 02 612 54865, Fax: 02 612 50740
http://nceph.anu.edu.au/Staff_Students/staff_pages/dear.php
CRICOS provider #00120C

http://canberragliding.org/

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index