Dear Statalist users,
This post is related to earlier posts on how the "exit" command works in foreach of forvalues loops. I have read these posts, and am still stuck with what I believe is a a somewhat different problem. My apologies if the answer already exists somewhere on this forum, I haven't been able to find it.
Here is what I am trying to do:
I am looping over many datasets. Each time a dataset is loaded, I check for the existence of a predefined set of variables (always the same). If one of these variable is missing, I exit the datafile, and load the next one. If not, the program continues.
I want to test the existence of a large number variables. Here is the code that I have, which works perfectly fine (I only show the code where I test the existence of 3 variables).
fs *.dta
foreach file in `r(files)' {
use `file', clear
*
capture confirm variable ccode
if _rc!=0 {
exit
}
capture confirm variable year
if _rc!=0 {
exit
}
capture confirm variable lstatus
if _rc!=0 {
exit
}
dis "the dataset is fine"
}
*
*
This code works just fine: If the first variable "ccode" does not exist in the first dataset then Stata exits this loop and loads the second dataset, etc.
To space space, I wrote a small program to check the existence of a variable:
program define ExistenZ
args var_tocheck
capture confirm variable var_tocheck
if _rc!=0 {
dis in red "the variable `var_tocheck' does not exist: exit the loop"
exit
}
end
*
*
I then call the program in the loop:
fs *.dta
foreach file in `r(files)' {
use `file', clear
*
ExistenZ ccode
ExistenZ year
ExistenZ lstatus
dis "the dataset is fine"
}
The problem is that if any of the variable "ccode" for instance does not exist in the dataset, the code keeps running and tests for the existence of the variable "year", instead of exiting the loop and loading the next dataset. I am quite puzzled by this, as I do not understand why the code does what I want in the first example, and not in the second.
If someone can explain to me what is going, that would be great.
Best regards,
Emmanuel Milet
This post is related to earlier posts on how the "exit" command works in foreach of forvalues loops. I have read these posts, and am still stuck with what I believe is a a somewhat different problem. My apologies if the answer already exists somewhere on this forum, I haven't been able to find it.
Here is what I am trying to do:
I am looping over many datasets. Each time a dataset is loaded, I check for the existence of a predefined set of variables (always the same). If one of these variable is missing, I exit the datafile, and load the next one. If not, the program continues.
I want to test the existence of a large number variables. Here is the code that I have, which works perfectly fine (I only show the code where I test the existence of 3 variables).
fs *.dta
foreach file in `r(files)' {
use `file', clear
*
capture confirm variable ccode
if _rc!=0 {
exit
}
capture confirm variable year
if _rc!=0 {
exit
}
capture confirm variable lstatus
if _rc!=0 {
exit
}
dis "the dataset is fine"
}
*
*
This code works just fine: If the first variable "ccode" does not exist in the first dataset then Stata exits this loop and loads the second dataset, etc.
To space space, I wrote a small program to check the existence of a variable:
program define ExistenZ
args var_tocheck
capture confirm variable var_tocheck
if _rc!=0 {
dis in red "the variable `var_tocheck' does not exist: exit the loop"
exit
}
end
*
*
I then call the program in the loop:
fs *.dta
foreach file in `r(files)' {
use `file', clear
*
ExistenZ ccode
ExistenZ year
ExistenZ lstatus
dis "the dataset is fine"
}
The problem is that if any of the variable "ccode" for instance does not exist in the dataset, the code keeps running and tests for the existence of the variable "year", instead of exiting the loop and loading the next dataset. I am quite puzzled by this, as I do not understand why the code does what I want in the first example, and not in the second.
If someone can explain to me what is going, that would be great.
Best regards,
Emmanuel Milet
Comment