Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: continuing a loop from after it encounters an error

From   Eric Booth <>
To   "<>" <>
Subject   Re: st: continuing a loop from after it encounters an error
Date   Thu, 3 Feb 2011 18:54:53 +0000


I'm still not clear on what the "objects" are.  You mention that are "commands" and then you -display- them. Also, when I run your code, I don't get the r(199) error about it being an unrecognized command -- so there's something important missing from your example.

Take a look at -help confirm- and decide which objects you are trying to confirm exist. 
If these are variables you can:

 set obs 10
 g object1 = 1
 g object3 = 3
foreach i in object1 object2 object3 {
cap confirm variable `i'
   if !_rc { 	            
	display `i'

If these are commands, you can also use -which- and -capture- to check to see if they are installed:
foreach i in  describe object1 object3 list  {
	cap which `i'
	if _rc continue

- Eric
Eric A. Booth
Public Policy Research Institute
Texas A&M University
Office: +979.845.6754

On Feb 3, 2011, at 12:41 PM, Nick Mosely wrote:

> Hello _all,
> I've built a loop that encounters an error periodically due to the
> fact that stata can't find one of the objects specified in the list. I
> don't mind the error, but I would like stata to skip objects in list
> that encounter the error and continue to the next iteration of the
> loop, rather than breaking the loop. I've tried the continue command,
> but am not sure that is what it's meant for.
> Here is a simplified version of the code. In this example, assume that
> objects1 and object3 are valid commands but object2 is not:
> foreach i in object1 object2 object3 {
> display `i'
> continue
> }
>> object1
>> unrecognized command:  object2
>> r(199);
> What can I do get the code to continue running through object3?
> Thanks,
> `Nick Mosely'
> *
> *   For searches and help try:
> *
> *
> *

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index