I'm trying to create a loop in STata that creates coefplots, which show the coefficients for multiple regressions for subgroups of a population. Each of the plots would use different regressors.
I'm running into an issue with quotations using the loop. Ideally each of the graphs would look like the picture I included.

sysuse auto, clear
replace foreign = 3 if (rep78 == 3)
regress price mpg length if foreign == 1
estimates store mpg_foreign_1_length
regress price mpg length if foreign == 0
estimates store mpg_foreign_0_length
regress price mpg length if foreign == 3
estimates store mpg_foreign_3_length
regress price mpg trunk if foreign == 1
estimates store mpg_foreign_1_trunk
regress price mpg trunk if foreign == 0
estimates store mpg_foreign_0_trunk
regress price mpg trunk if foreign == 3
estimates store mpg_foreign_3_trunk
global list_graphing "trunk" "length"
foreach x of global list_graphing {
coefplot "mpg_foreign_3_`x' mpg_foreign_1_`x'" ||, drop(_cons)
}
foreach x of global list_graphing {
coefplot "mpg_foreign_3_`x' mpg_foreign_1_`x' mpg_foreign_0_`x'" ||, drop(_cons)
}
//invalid something: quotes do not match
foreach x of global list_graphing {
coefplot "mpg_foreign_3_`x'" "mpg_foreign_1_`x'" "mpg_foreign_0_`x'"
}
The first graph displays essentially what I'm trying to create; however, it doesn't work when a third variable is added. I can't split them up with quotes because, as the code clearly shows, that makes it so the coefficients are no longer on the same graph.
I don't have much experience with
coefplot, but it seems this works if you just don't use quotes (indeed, I'm somewhat surprised that it does work with quotes sometimes).