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]

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

From   Nick Cox <>
To   "''" <>
Subject   st: RE: continuing a loop from after it encounters an error
Date   Thu, 3 Feb 2011 18:52:16 +0000

Depending on what the "objects" are, there may be a better solution to your problem that allows you to avoid it altogether, but in terms of your question, -capture- is what you seek. 

-capture- eats errors. 

Look also at -capture noisily- and -noisily capture-. Here's the difference: 

. sysuse auto
(1978 Automobile Data)

. di frog[1]
frog not found

. capture noisily di frog[1]
frog not found

. noisily capture di frog[1]

. noisily capture di mpg[1]

. capture di mpg[1]

. capture noisily di mpg[1]


Nick Mosely

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'
>unrecognized command:  object2

What can I do get the code to continue running through object3?

*   For searches and help try:

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