次のおもちゃの変数を生成すると:
clear
set obs 5
local vlist caseid midx hidx v000 v013 v014 v015 v016 v021 v025 v101 v102
foreach v of local vlist {
generate `v' = runiform()
}
これは私のために働く:
foreach v of local vlist {
capture confirm variable `v'
if !_rc {
display in red "variable `v' exists"
}
else {
display in red "variable `v' does not exist"
}
}
variable caseid exists
variable midx exists
variable hidx exists
variable v000 exists
variable v013 exists
variable v014 exists
variable v015 exists
variable v016 exists
variable v021 exists
variable v025 exists
variable v101 exists
variable v102 exists
変数を削除した場合:
drop v000
(run the second loop again)
variable caseid exists
variable midx exists
variable hidx exists
variable v000 does not exist
variable v013 exists
variable v014 exists
variable v015 exists
variable v016 exists
variable v021 exists
variable v025 exists
variable v101 exists
variable v102 exists
代わりに、次のようにローカル マクロを定義する場合vlist
:
local vlist caseid midx hidx v000 v013-v016 v021 v025 v101 v102
(run the second loop again)
variable caseid exists
variable midx exists
variable hidx exists
variable v000 exists
variable v013-v016 does not exist
variable v021 exists
variable v025 exists
variable v101 exists
variable v102 exists
同様に、存在しない2 つの変数var1
を追加すると、次のようになります。var5
local vlist caseid midx hidx var1 v000 v013 v014 v015 v016 var5 v025 v101 v102
(run the second loop again)
variable caseid exists
variable midx exists
variable hidx exists
variable var1 does not exist
variable v000 exists
variable v013 exists
variable v014 exists
variable v015 exists
variable v016 exists
variable var5 does not exist
variable v025 exists
variable v101 exists
variable v102 exists