There is a series of regressions I want to run, and in each regression, I decide if I want a covariate or not. So for example, let's say I have a local macro with 10 covariates:
local covariates "`N' age `N' sex `N' age2 `N' job job2 `N'"
I want 10 regressions, each with one covariate in order as listed. However, the `N'
terms are placeholders meant to show that I don't want a covariate in that regression. So when running the regressions, it looks something like:
forval i=1/10 {
local cov `: word `i' of `covariates''
if "`cov'" != "N" {
reg y x `cov'
}
else {
reg y x
}
}
where y
is an outcome, and x
is another covariate that's included in every regression.
My issue is that Stata does not recognize the `N'
placeholders. More specifically, it seems like the if "`cov'" != "N"
condition filters out the placeholder variables from going to the reg y x `cov'
step, but they don't go to the else
condition to run reg y x
. After some debugging, it seems like Stata simply doesn't realize that the length of covariates
is 10: when I check the length of the macro, it returns 5, and when I print each term in the macro, it only returns 5 values. Since Stata thinks the length of the macro is 5, then it breaks once the forval
loop hits i=6
. Hence I only get 5 regressions of the form reg y x `cov'
.
So, is there a clean way to include some sort of placeholder such that Stata will recognize the length of the macro, and thus run all 10 regressions?