Similar threads have not led me to solve the following problem.
I use a local macro to specify a varlist
with multiple variables and want to check whether each of the variables in this varlist
exists in the dataset I use. So as to get a quick overview which variables do not exist in the dataset.
I have tried the following code so far:
local vlist caseid midx hidx v000-v013 v016 v021-v025 v101 v102
foreach v of local vlist {
capture confirm variable `v'
if !_rc {
display in red "variable exists"
}
else {
display in red "variable does not exist"
}
}
The code runs through but nothing is displayed. I've also tried intentionally inserting variables in the varlist
that do not exist in the dataset. Nothing changed.
Does anyone know how I can overcome this problem?
When i generate the following toy variables:
This works for me:
If i drop a variable:
If instead you define your local macro
vlist
as follows:Similarly, if you add two variables
var1
andvar5
that do not exist:Leaving your display problem aside, you could have a look at the user-written command
checkfor2
. It will give you three returns, containing a list with variables that are a) non-existing, b) existing, but with many missing and c) existing and with few missings.