Hello All,
I have a rather interesting issue. I am attempting to use some if-else statements for some conditions within a foreach loop. Here is the issue. The syntax that I am posting works beautifully. When I take it to my data, STATA will only read and perform the condition in the first "if" statement. To be clear, the syntax #1 works perfectly, but when I try this with my data, using syntax #2, STATA will only read the first "if" statement and not the second one. I first thought it was a data formatting problem, but the auto.dta variables are: int and byte formats. My data are both byte format. Any ideas how to get this to consistently work across all data?
Syntax #1
sysuse auto.dta, clear
local dv foreign
foreach var of varlist `dv' {
if `var' == mpg {
di 2+2+4/2
}
else if `var' == foreign {
di 10/5
}
}
Syntax #2.
local dv victim
foreach var of varlist `dv' {
if `var' == violence {
di "violence"
}
else if `var' == victim {
di "victim"
}
}
Any help is welcomed. Thank you.
0 Response to If-Else Issue
Post a Comment