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: exit from interactive Mata, gracefully?

From   Stas Kolenikov <>
Subject   st: exit from interactive Mata, gracefully?
Date   Wed, 25 May 2011 15:17:36 -0500

Dear statalisters,

what is the appropriate way of handling the following situation? I
have a do-file that uses a mix of Stata and Mata. At some point, due
to some unsurmountable (numeric) difficulties (I actually have a
rather complicated optimization problem), Mata has to break, and the
do-file needs to be exited. So I have

--- the do-file ---

* some Stata code
sysuse auto

// some Mata code

if ( missing(x) ) {
  // something tragic happened; I want to break here and exit the do-file
else {
  // more Mata code
  sum( x )
// even more Mata code
mean( x )

* more Stata code


--- end of do-file ---

So -stata("exit")- does not work there. I can just break it with
-stata("nonsense")- to really cause an error, but my execution would
still continue to run. I cannot put -end- there, as the interpreter
will stop treating this as Mata code after it. Short of rewriting my
code to create flags and branching on them on every occasion, what
else can I do? Essentially, I am looking for something like -break-
without the cycle context. I even thought of using -goto- (God
forbid), but it cannot be used interactively to jump to a label that
is a few lines down the code, so it has not yet been read.

Stas Kolenikov, also found at
Small print: I use this email account for mailing lists only.
*   For searches and help try:

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