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.
Related Posts with If-Else Issue
Export ErrorHi all I am trying to export the data from Stata to Excel but getting the following error message o…
Coding for multiple medicinesI was wondering if anyone had some ideas on how I can approach a problem with my dataset. I have a v…
Error message "outcome does not vary; remember 0=negative outcome, all other nonmissing values=positive outcome"Hello, I have a binary variable that I called SRI. This variable equals 1 if an investor invests in…
generate a dummy by timeDear All, I have this data set (in reality, I have data in years 2020, 2021, and 2022), Code: * Exa…
Coefplot for balance check of treatmentsHi, I am trying to use coefplot command to see the balance of the "regression outcomes" on four tre…
Subscribe to:
Post Comments (Atom)
0 Response to If-Else Issue
Post a Comment