与多个命令配合使用时,捕获功能不起作用

时间:2019-08-04 18:01:05

标签: stata

我的3,000文件中有超过do个变量和标签,但是在我的数据集中,我需要~300个标签。为了绕过没有变量的错误,我使用了capture命令。

在我的数据中,没有hhidhh_cu_q变量,只有newid变量:

capture noisily {
    label var hhid "Identifier for household with more than one CU. Household with only one CU will be set to missing."
    label var hh_cu_q "Count of Consumer Units in this household"
    label var newid "Public use microdata identifier"
}

但是,当我运行这段代码时,我看到命令第一行的错误(未找到变量hhid),似乎Stata正在执行其余的行,但是没有更换标签!

如果仅运行命令的最后一行,它将运行正常(它将为newid添加标签)。

1 个答案:

答案 0 :(得分:3)

这是正常现象,因为您在整个命令的整个 block 中都应用了capture。结果,当出现错误时,Stata会跳过该块中所有剩余的命令,然后继续执行do文件的其余部分。

单独应用capture时,一切都会按您的预期进行:

clear
set obs 1

generate newid = .

. capture noisily label var hhid "Identifier for household with more than one CU. Household with only one CU will be set to missing."
variable hhid not found

. capture noisily label var hh_cu_q "Count of Consumer Units in this household"
variable hh_cu_q not found

. capture noisily label var newid "Public use microdata identifier"

. display "`: variable label newid'"
Public use microdata identifier