我的 do
文件中有超过 3,000
个变量和标签,但在我的数据集中我需要 ~300
个标签。为了绕过没有变量的错误,我使用了 capture 命令。
在我的数据中,没有 hhid
或 hh_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/