如何确认变量列表中所有各个变量的存在? [英] How do I confirm existence of all respective variables of a variable list?

查看:120
本文介绍了如何确认变量列表中所有各个变量的存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似的线程并没有使我解决以下问题。

Similar threads have not led me to solve the following problem.

我使用本地宏来指定 varlist 具有多个变量,并且要检查此 varlist 中的每个变量在我使用的数据集中是否存在。为了快速了解哪些变量在数据集中不存在。

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"
    }
}

该代码一直运行,但是没有显示任何内容。我还尝试过有意将变量插入数据集中不存在的 varlist 中。没什么改变。

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()
}

这对我有用:

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

如果我删除变量:

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

如果相反,您可以如下定义本地宏 vlist

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

类似地,如果添加两个变量 var1 var5 不存在:

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

这篇关于如何确认变量列表中所有各个变量的存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆