Hello!
I was running a series of regressions, with some if and else statments, when I realized that Stata wasn´t following the if and else statements as I assumed it would. I started the debugging process by using some displays and making the code simpler. I even made generated a new dataset with code to post here, so that the problem could be fully replicated, only to realized that the code worked fine in that scenario. I then realized that having a dataset already loaded in the memory changed how Stata would run the code.
The following two blocks of code are the same, with the exception that I first load a dataset into memory (also works with others, and different Stata versions, in different computers).
Block 1 (does not run the code as it is suppose to):
Block 2 (Follows the correct if statement, but requires the memory to be clear).
The correct output would be "Running 3", as `qwe' is never == a2. Any suggestions on what could be causing this?
Thanks a lot of your help!
Best,
Hélder
I was running a series of regressions, with some if and else statments, when I realized that Stata wasn´t following the if and else statements as I assumed it would. I started the debugging process by using some displays and making the code simpler. I even made generated a new dataset with code to post here, so that the problem could be fully replicated, only to realized that the code worked fine in that scenario. I then realized that having a dataset already loaded in the memory changed how Stata would run the code.
The following two blocks of code are the same, with the exception that I first load a dataset into memory (also works with others, and different Stata versions, in different computers).
Block 1 (does not run the code as it is suppose to):
Code:
clear all
webuse auto, clear
capture gen a1 = .
capture gen a2 = .
capture gen b1 = .
capture gen b2 = .
foreach qwe of varlist a1 {
foreach asd of varlist b1 b2 {
di "`qwe'"
di "`asd'"
if `qwe' == a2 {
di "Running 1"
}
else {
di "Running 3"
}
}
}
Code:
clear all
capture gen a1 = .
capture gen a2 = .
capture gen b1 = .
capture gen b2 = .
foreach qwe of varlist a1 {
foreach asd of varlist b1 b2 {
di "`qwe'"
di "`asd'"
if `qwe' == a2 {
di "Running 1"
}
else {
di "Running 3"
}
}
}
Thanks a lot of your help!
Best,
Hélder

Comment