| ![](/includes/contimages/spacer.gif) |
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
Re: st: help with conditional loop
Works well. Thanks.
-Shawn
Justin Smith wrote:
sorry, small typo........
the last line in that code should be:
egen firstcontact = rowmin(x1-x30)
Justin Smith
On Sat, 05 Aug 2006 14:42:13 -0400
Justin Smith <[email protected]> wrote:
JS>Hi Shawn:
JS>
JS>perhaps try this:
JS>
JS>foreach i of numlist 1/30 {
JS> qui replace x`i' = `i' if x`i' == 1
JS>}
JS>
JS>gen firstcontact = rowmin(x1-x30)
JS>
JS>
JS>This replaces the ones in your variables with the value of month,
JS>then
JS>takes the lowest one in the row. Of course, you may need to change
JS>those values back to ones if they are important.
JS>
JS>Hope this helps,
JS>
JS>Justin Smith
JS>
JS>
JS>
JS>On Sat, 05 Aug 2006 13:56:22 -0400
JS> Shawn Bauldry <[email protected]> wrote:
JS>JS>I think I have a fairly simple loop question that I can't seem to
JS>JS>get my
JS>JS>head around.
JS>JS>
JS>JS>I have a series of variables, say x1-x30, that correspond to
JS>months
JS>JS>in a
JS>JS>program (a value of 1 indicates a person was present in the
JS>program
JS>JS>in a
JS>JS>given month). I simply want to find the first month a person
JS>shows
JS>JS>up.
JS>JS>
JS>JS>I tried the following, but it's not working and I'm not sure why.
JS>JS>
JS>JS>gen FirstContact = .
JS>JS>local i = 0
JS>JS>while FirstContact == . {
JS>JS> local i = `i' + 1
JS>JS> replace FirstContact = `i' if x`i' == 1
JS>JS>}
JS>JS>
JS>JS>The code runs without error, but does not find the correct first
JS>JS>month
JS>JS>someone shows up. For example, in one case I have:
JS>JS>
JS>JS>x1 = ., x2 = 1, x3 = 1, x4 = 1, x5 = 1, x6 = 1, ... and it
JS>returns
JS>JS>FirstContact = 5.
JS>JS>
JS>JS>
JS>JS>Any thoughts on how to make this code work or better code are
JS>much
JS>JS>appreciated.
JS>JS>
JS>JS>
JS>JS>Thanks,
JS>JS>Shawn
JS>JS>*
JS>JS>* For searches and help try:
JS>JS>* http://www.stata.com/support/faqs/res/findit.html
JS>JS>* http://www.stata.com/support/statalist/faq
JS>JS>* http://www.ats.ucla.edu/stat/stata/
JS>
JS>Justin Smith
JS>PhD Candidate
JS>Department of Economics
JS>McMaster University
JS>Phone: (905) 962-0353
JS>E-mail: [email protected]
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/
*
* 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/