Statalist The Stata Listserver


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

Re: st: help with conditional loop


From   Justin Smith <[email protected]>
To   [email protected]
Subject   Re: st: help with conditional loop
Date   Sat, 05 Aug 2006 14:42:13 -0400

Hi Shawn:

perhaps try this:

foreach i of numlist 1/30 {
	qui replace x`i' = `i' if x`i' == 1
}

gen firstcontact = rowmin(x1-x30)


This replaces the ones in your variables with the value of month, then
takes the lowest one in the row.  Of course, you may need to change
those values back to ones if they are important.

Hope this helps,

Justin Smith



On Sat, 05 Aug 2006 13:56:22 -0400
 Shawn Bauldry <[email protected]> wrote:
JS>I think I have a fairly simple loop question that I can't seem to
JS>get my
JS>head around.
JS>
JS>I have a series of variables, say x1-x30, that correspond to months
JS>in a
JS>program (a value of 1 indicates a person was present in the program
JS>in a
JS>given month).  I simply want to find the first month a person shows
JS>up.
JS>
JS>I tried the following, but it's not working and I'm not sure why.
JS>
JS>gen FirstContact = .
JS>local i = 0
JS>while FirstContact == . {
JS> 	local i = `i' + 1
JS> 	replace FirstContact = `i' if x`i' == 1
JS>}
JS>
JS>The code runs without error, but does not find the correct first
JS>month
JS>someone shows up.  For example, in one case I have:
JS>
JS>x1 = ., x2 = 1, x3 = 1, x4 = 1, x5 = 1, x6 = 1, ... and it returns
JS>FirstContact = 5.
JS>
JS>
JS>Any thoughts on how to make this code work or better code are much
JS>appreciated.
JS>
JS>
JS>Thanks,
JS>Shawn
JS>*
JS>*   For searches and help try:
JS>*   http://www.stata.com/support/faqs/res/findit.html
JS>*   http://www.stata.com/support/statalist/faq
JS>*   http://www.ats.ucla.edu/stat/stata/

Justin Smith
PhD Candidate
Department of Economics
McMaster University
Phone: (905) 962-0353
E-mail: [email protected] 
*
*   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