I'm trying to create a loop in Stata to run a bunch of regressions. This is a very simplified version of how it looks:
foreach y of local years{
forval i=1/50 {
local cov `: word `i' of `covariates''
local int `: word `i' of `interactions''
if `cov' != "N" {
*regression stuff*
}
}
}
years
, covariates
, and interactions
are local macros that I'm looping through, and the latter two have length 50. They each consist of strings, e.g.
local covariates "covariate1 covariate2 covariate3 covariate4"
The key thing here is that there are certain instances in covariates
(and also interactions
in the full code) where I have a string called "N", meaning I don't want to do the regression with the covariate in that instance. Hence, the
if `cov' != "N"
condition. The problem is that that line gives me a type mismatch error, and I'm not sure why. Would anyone know why this is the case? Thanks!
Here: if `cov' != "N"
, cov
evaluates to a variable name, and the first observation is compared with string "N", hence the type error.
You probably need:
if "`cov'" != "N"