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: AW: is looping slower


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   st: AW: is looping slower
Date   Thu, 27 May 2010 09:41:51 +0200

<> 

Are "var1-var9" really string variables? Otherwise you will end up with a
"type mismatch" error. 

You could make your -if- qualifier shorter via -inlist()-:


*************
clear*
set obs 100000

gen myvar=_n

cou if regexm(string(myvar), "^29") |  /* 
*/ regexm(string(myvar), "^30") | /* 
*/ regexm(string(myvar), "^31") 

cou if inlist(substr(string(myvar),1,2),"29","30","31")
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Jing Xia
Gesendet: Donnerstag, 27. Mai 2010 06:39
An: [email protected]
Betreff: st: is looping slower

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/


*
*   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