Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: is looping slower


From   Jing Xia <[email protected]>
To   [email protected]
Subject   st: is looping slower
Date   Thu, 27 May 2010 00:38:59 -0400

Hello, All,

I have var1 to var9, and I'd like to create a variable outcome=1 if
any of these 9 variables satisfay condition A.

I can use a loop to loop over var1 to var9 below, but I suspect that
it takes 9 times as long if I had written it in one line:

outcome=1 if var1==conditionA | var2==conditionA | var3==conditionA |
var4==conditionA | .......var9==conditionA

Here's the loop:

gen outcome=0
foreach i of numlist 1/9 {
replace outcome = 1 if regexm(var`i', "^29")==1 |regexm(var`i',
"^30")==1 |regexm(var`i', "^31")==1
}

Is it true that looping will take much longer? (I have millions of
records) If so, is there a way to write the one line code in a concise
manner?

Thank you!

J.
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index