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]
Re: st: RE: strange foreach loop result
From
Klaus Pforr <[email protected]>
To
[email protected]
Subject
Re: st: RE: strange foreach loop result
Date
Wed, 15 Dec 2010 18:04:42 +0100
<>
David,
I guess what you want to do is something like this:
use result_pilot.dta, clear
keep slope lower upper ed50
foreach var of varlist slope lower upper ed50 {
if "`var'" == "slope" local myvar 1
if "`var'" == "lower" local myvar 2
if "`var'" == "upper" local myvar 3
if "`var'" == "ed50" local myvar 4
display "`var'", "`myvar'"
}
I.e. you want to compare the variable names, i.e. strings, not the
values that the variables have. I guess both the variables slope and
ed50 have the same values in the first observation, so you overwrite
your local with 4 in the first loop run.
You can verify this easily
di slope
di ed50
which is equivalent to
di slope[1]
di ed50[1]
best
Klaus
Am 15.12.2010 17:53, schrieb Matthew Watt:
David,
Do you get the same results if you use an "if/else if" structure instead of a pure if structure? As you have it now, each of your loop's tests is independent of the previous tests, meaning that even if `var' == slope, you test to see if `var' also equals lower, upper, and ed50. The result of this is that if `var' equals both slope and ed50, you will assign a value of 4 to slope. If you use an if/else structure, the else clause will only be tested if the "if" clause is false.
Of course, you may end up with ed50 having a value of 1 if ed50 and slope are equal, should you use if/else.
--Matt
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Airey, David C
Sent: Wednesday, December 15, 2010 11:40 AM
To: [email protected]
Subject: st: strange foreach loop result
.
I'm getting a really strange foreach loop result I don't understand.
In the first code snippet on fictitious data, I get what I want. In the second code snippit on the real data I get a strange result for when the condition:
if `var' == slope local myvar 1
is evaluated when `var' is slope. It produces the result myvar==4, when it should return myvar==1.
Anyone have a clue where my error is?
clear
set obs 10
gen a = 1
gen b = 2
gen c = 3
gen d = 4
foreach var of varlist a b c d {
if `var' == a local myvar 1
if `var' == b local myvar 2
if `var' == c local myvar 3
if `var' == d local myvar 4
display "`var'", "`myvar'"
}
produces:
a 1
b 2
c 3
d 4
use result_pilot.dta, clear
keep slope lower upper ed50
foreach var of varlist slope lower upper ed50 {
if `var' == slope local myvar 1
if `var' == lower local myvar 2
if `var' == upper local myvar 3
if `var' == ed50 local myvar 4
display "`var'", "`myvar'"
}
produces:
slope 4<--weird, should be slope 1
lower 2
upper 3
ed50 4
*
* 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/
This email message and any attachments are for the sole use of the intended recipients and may contain proprietary and/or confidential information which may be privileged or otherwise protected from disclosure. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not an intended recipient, please contact the sender by reply email and destroy the original message and any copies of the message as well as any attachments to the original message. http://www.mscibarra.com/legal/local_registered_entities.html
*
* 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/
--
__________________________________
Klaus Pforr
MZES AB - A
Universität Mannheim
D - 68131 Mannheim
Tel: +49-621-181 2801 (nachmittags)
fax: +49-621-181 2803
URL: http://www.mzes.uni-mannheim.de
Besucheranschrift: A5, Raum A312
__________________________________
*
* 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/