Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

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

st: is looping slower

From   Jing Xia <>
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

Thank you!

*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index