Hello everyone, I am back with another question. I have been trying to use if else statement in a loop and I am having some difficulty making it work. I will now explain the scenario.
Scenario:
I have noticed that the if else command in my loop is not working correctly. It gives the correct output for the first observation. However, for all the observations i > 1, the results are a copy of the results from the first observation. What am I doing wrong here?
STATA Code:
file open trail1 using trail1.txt, text write replace
file write trail1 "*** For each article you will receive two options about the presence of disagreements. Your job is to read the article and resolve the disagreement. The answer that is incorrect should be starred." _n
file write trail1 _n
local N = _N
forvalues i = 1 / `N' {
foreach var of varlist model_cs model_ts model_tscs model_spatial model_other {
if `var' == 1 {
file write trail1 "replace `var' = 0 if paper_id == `= paper_id[`i']'" _n
file write trail1 "replace `var' = 1 if paper_id == `= paper_id[`i']'" _n
file write trail1 _n
}
else if `var' == 0 {
file write trail1 _n
}
}
}
file close trail1
Scenario:
I have noticed that the if else command in my loop is not working correctly. It gives the correct output for the first observation. However, for all the observations i > 1, the results are a copy of the results from the first observation. What am I doing wrong here?
STATA Code:
file open trail1 using trail1.txt, text write replace
file write trail1 "*** For each article you will receive two options about the presence of disagreements. Your job is to read the article and resolve the disagreement. The answer that is incorrect should be starred." _n
file write trail1 _n
local N = _N
forvalues i = 1 / `N' {
foreach var of varlist model_cs model_ts model_tscs model_spatial model_other {
if `var' == 1 {
file write trail1 "replace `var' = 0 if paper_id == `= paper_id[`i']'" _n
file write trail1 "replace `var' = 1 if paper_id == `= paper_id[`i']'" _n
file write trail1 _n
}
else if `var' == 0 {
file write trail1 _n
}
}
}
file close trail1

Comment