capture and nostop

In some instance the standard option that STATA stops whenever an error occurs is a (minor) annoyance. In one of my projects I was running the same set of regressions over several groups with loops. However, whenever STATA found a group that could not run the regressions it would stop, stating the error no observations. Similar things can happen when you select (sub)groups to run commands like summarize, tabulate etc.

One solution is to “capture” the command, so that any error that is returned does not stop the do-file. My favourite example for capture is the following statement that can be found in almost all of my do-files…:


capture log close
log using mylog, replace

The capture log close before my log statement allows me to open a log, even if a log file has been open. It closes a running log-file. However, if no log-file is open, STATA would react with an error on the statement log close and exit. With a capture in front, this does not happen.

If you want to run the entire do-file without stopping, you could also state:


do my_do_file.do, nostop

Leave a Reply