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 at the end of May, and its replacement, statalist.org is already up and running.


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

st: R: strange foreach loop result


From   "Carlo Lazzaro" <carlo.lazzaro@tin.it>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: R: strange foreach loop result
Date   Wed, 15 Dec 2010 18:34:12 +0100

Dear David,
your code works perfectly for me (Stata 9/2.SE):
--------------------------------------------------------------
drop _all
set obs 10
g slope=uniform()
g lower=uniform()
g upper = uniform()
g ed50=uniform()
g Carlo=uniform()
save "C:\Documents and Settings\carlo\Desktop\David.dta", replace
file C:\Documents and Settings\carlo\Desktop\ David.dta saved
use "C:\Documents and Settings\carlo\Desktop\ David.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 1
lower 2
upper 3
ed50 4


Sorry I cannot be more helpful.

Kind Regards,
Carlo
-----Messaggio originale-----
Da: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Per conto di Airey, David C
Inviato: mercoledì 15 dicembre 2010 17.40
A: statalist@hsphsun2.harvard.edu
Oggetto: 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/


*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index