Stata 与多个命令一起使用时,捕获不起作用

Stata 与多个命令一起使用时,捕获不起作用,stata,Stata,我的do文件中有3000多个变量和标签,但在我的数据集中,我需要~300个标签。为了绕过没有变量的错误,我使用了capture命令 在我的数据中,没有hhid或hh\u cu\u 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." lab

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

在我的数据中,没有
hhid
hh\u cu\u 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"
}
但是,当我运行这段代码时,我看到命令的第一行出现了错误(variable
hhid
notfound),看起来Stata正在执行其余的行,但是标签没有变化


如果我只运行命令的最后一行,它可以正常工作(它为
newid
添加标签)。

这是正常的行为,因为您正在整个命令块中应用
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

这是正常的行为,因为您正在整个命令块中应用
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

谢谢,很好用。在帮助页面中有一部分说我们可以在整个块上应用
capture
。不管怎样,我在所有行的前面都添加了捕获。再次感谢!的确“你可以做到,通常这是你想要的,但不是在这里。”帕汉姆感谢你的接受。同样重要的是,用上面的箭头向上投票你看到的所有有用的答案。谢谢,效果很好。在帮助页面中有一部分说我们可以在整个块上应用
capture
。不管怎样,我在所有行的前面都添加了捕获。再次感谢!的确“你可以做到,通常这是你想要的,但不是在这里。”帕汉姆感谢你的接受。同样重要的是,用上面的箭头向上投你看到的所有有用的答案。