stata - 与多个命令一起使用时捕获不起作用

标签 stata

我的 do 文件中有超过 3,000 个变量和标签,但在我的数据集中我需要 ~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 的标签)。

最佳答案

这是正常行为,因为您正在整个命令 block 中应用捕获。因此,当出现错误时,Stata 会绕过 block 中的所有剩余命令,并继续执行 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

关于stata - 与多个命令一起使用时捕获不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57349051/

相关文章:

loops - 在本地宏中使用通配符循环变量

r - 像Stata命令计数一样如何计算R中的观察数

syntax-error - 使用putpdf的“Failed to add table”错误(Stata)

interaction - Stata 中的非线性交互项

r - data.table 中的唯一标识符

stata - Stata 的替代 IDE

Stata esttab 输出

list - 访问Stata中的列表元素

stata - 在命令 nlcom 之后检索标准错误

r - 来自 Stata 的 xtpcse - 如何在 R 中重写