Bookmark and Share

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


[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 <[email protected]>
To   "<[email protected]>" <[email protected]>
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:

**************************!
clear
 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:
**************************!
clear
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
[email protected]
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:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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