Reputation:
I'm trying to use if
statements to assign correct labels for my graphs created inside foreach
loop in Stata:
foreach major in var1 var2 {
* conditional labelling
if "`major'" == "var1" {
local ytitle "title for var1"
}
else if "`major" == "var2" {
local ytitle "title for var2"
}
di in red "____________"
di in red "`major'"
di in red "`ytitle'"
di in red "____________"
}
The output of this exercise is
____________
var1
should be var1
____________
____________
var2
should be var1
____________
My question is - why isn't the local changed in the second instance of the loop?
Upvotes: 2
Views: 12841
Reputation: 37183
The example is a little confusing, as the words "should be" in the output should be (so to speak) "title for", matching the code. That aside, your bug is an unmatched single quote. Try
foreach major in var1 var2 {
* conditional labelling
if "`major'" == "var1" {
local ytitle "title for var1"
}
else if "`major'" == "var2" {
local ytitle "title for var2"
}
di in red "____________"
di in red "`major'"
di in red "`ytitle'"
di in red "____________"
}
Upvotes: 5