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

From   "Martin Weiss" <>
To   <>
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()-:

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")


-----Ursprüngliche Nachricht-----
[] Im Auftrag von Jing Xia
Gesendet: Donnerstag, 27. Mai 2010 06:39
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

Thank you!

*   For searches and help try:

*   For searches and help try:

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