首页 > 解决方案 > 与多个命令一起使用时,捕获不起作用

问题描述

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

在我的数据中,没有hhidorhh_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)。

标签: stata

解决方案


这是正常行为,因为您正在应用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

推荐阅读