Reputation: 157
I am looking for a way to check whether a variable exists in a Stata dataset, and if not to repeatedly check variable names over a loop until I find one that exists. I am aware of the other responses to similar questions that involve confirm
and depend on testing the value of _rc
.
However, if you try to use these approaches repeatedly the value of _rc
in any given iteration of a loop is only informative of whether there has been at least one error in any prior iteration. I wish to continue testing until there is not an error, but whenever the first attempt results in an error there is no way to tell if the second attempt also results in an error because _rc
remains the same regardless (assuming all errors report the same code). Is there any way to either reset _rc
to zero manually, or to test for the existence of a variable in a less ad hoc way that doesn't involve _rc
?
Upvotes: 1
Views: 12084
Reputation: 2694
Here is an example on how to find the first variable name present in the data in a list of potential variable names
sysuse auto
local vlist "var1 var2 mpg var3"
foreach var of local vlist {
capture confirm variable `var'
if !_rc {
local first_present "`var'"
continue, break // <-- stops the loop after the first present variable
}
}
if "`first_present'" != "" {
di as txt "The first present variable name is: " ///
as result "`first_present'"
}
else {
di as txt "the list contained no present variable names"
}
Upvotes: 1
Reputation: 11102
I think there is some misunderstanding on the working of the capture
and _rc
procedure.
1. I quote:
... if you try to use these approaches repeatedly the value of _rc in any given iteration of a loop is only informative of whether there has been at least one error in any prior iteration.
This is not correct. The value of _rc
depends on the result of the command that immediately precedes, so it will be updated until the loop finishes.
2. I quote again:
... whenever the first attempt results in an error there is no way to tell if the second attempt also results in an error ...
Also incorrect. You can tell if the second, third, etc., attempt is an error by looking at _rc
. This is a consequence of (1).
An example can perhaps show this:
clear all
set more off
sysuse auto
gen var1 = 1
gen var2 = 2
local vlist var0 var1 var2 var3 mpg var4
foreach v of local vlist {
capture confirm variable `v'
display "is `v' present ? " (_rc == 0)
}
which results in
is var0 present ? 0
is var1 present ? 1
is var2 present ? 1
is var3 present ? 0
is mpg present ? 1
is var4 present ? 0
The variable var0
which is shown not to exist is followed var1
, which does exist, and so on.
Upvotes: 5
Reputation: 4453
The Stata documentation gives this example under the help page for confirm
. Used in conjunction with capture
, I believe you avoid the problem with resetting _rc
. For your purposes, simply replace the first line in the loop with capture confirm existence `v'
foreach v of local varlist {
capture confirm string variable `v'
if !_rc {
action for string variables
}
else {
action for numeric variables
}
}
}
Upvotes: 1