Statalist The Stata Listserver


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

st: help with conditional loop


From   Shawn Bauldry <[email protected]>
To   [email protected]
Subject   st: help with conditional loop
Date   Sat, 05 Aug 2006 13:56:22 -0400

I think I have a fairly simple loop question that I can't seem to get my
head around.

I have a series of variables, say x1-x30, that correspond to months in a
program (a value of 1 indicates a person was present in the program in a
given month).  I simply want to find the first month a person shows up.

I tried the following, but it's not working and I'm not sure why.

gen FirstContact = .
local i = 0
while FirstContact == . {
	local i = `i' + 1
	replace FirstContact = `i' if x`i' == 1
}

The code runs without error, but does not find the correct first month
someone shows up.  For example, in one case I have:

x1 = ., x2 = 1, x3 = 1, x4 = 1, x5 = 1, x6 = 1, ... and it returns
FirstContact = 5.


Any thoughts on how to make this code work or better code are much
appreciated.


Thanks,
Shawn
*
*   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