variables - How do I confirm existence of all respective variables of a variable list?
问题描述
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:
clear
set obs 5
local vlist caseid midx hidx v000 v013 v014 v015 v016 v021 v025 v101 v102
foreach v of local vlist {
generate `v' = runiform()
}
This works for me:
foreach v of local vlist {
capture confirm variable `v'
if !_rc {
display in red "variable `v' exists"
}
else {
display in red "variable `v' does not exist"
}
}
variable caseid exists
variable midx exists
variable hidx exists
variable v000 exists
variable v013 exists
variable v014 exists
variable v015 exists
variable v016 exists
variable v021 exists
variable v025 exists
variable v101 exists
variable v102 exists
If i drop a variable:
drop v000
(run the second loop again)
variable caseid exists
variable midx exists
variable hidx exists
variable v000 does not exist
variable v013 exists
variable v014 exists
variable v015 exists
variable v016 exists
variable v021 exists
variable v025 exists
variable v101 exists
variable v102 exists
If instead you define your local macro vlist
as follows:
local vlist caseid midx hidx v000 v013-v016 v021 v025 v101 v102
(run the second loop again)
variable caseid exists
variable midx exists
variable hidx exists
variable v000 exists
variable v013-v016 does not exist
variable v021 exists
variable v025 exists
variable v101 exists
variable v102 exists
Similarly, if you add two variables var1
and var5
that do not exist:
local vlist caseid midx hidx var1 v000 v013 v014 v015 v016 var5 v025 v101 v102
(run the second loop again)
variable caseid exists
variable midx exists
variable hidx exists
variable var1 does not exist
variable v000 exists
variable v013 exists
variable v014 exists
variable v015 exists
variable v016 exists
variable var5 does not exist
variable v025 exists
variable v101 exists
variable v102 exists
推荐阅读
- python - 熊猫使用“b”列之前和之后的单元格平均值填充“a”列
- python-3.x - 使用python比较2个图像内容
- c# - Azure C# Cosmos DB:插入数据时缺少必需的属性“id”
- swagger-ui - Springfox swagger-ui 3.0.0 不调出 swagger-ui.html 页面
- node.js - 如何保存 jwt 令牌以在下一个请求中使用?在nodejs中
- java - 如果发生 StackOverflowError,有没有办法在 Java 中查看“堆栈内存转储”
- node.js - 如何在 NodeJS 中模拟嵌套依赖项
- speech-to-text - .opus 音频文件的谷歌语音到文本配置是什么样的
- android - 如何使用地图功能将列表从一种类型更改为另一种类型
- c# - HTTP 函数 C# SQL 异步