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: Interactive MATA and DO Files - Foreach Loops

From   Maarten Buis <[email protected]>
To   [email protected]
Subject   Re: st: Interactive MATA and DO Files - Foreach Loops
Date   Thu, 26 Jul 2012 08:33:57 +0200

The safest way to use Mata inside a Stata loop is to, outside the
Stata loop, put your Mata stuff inside a Mata function, and call that
that Mata function inside the Stata loop:


Hope this helps,

On Thu, Jul 26, 2012 at 6:51 AM, Matthew McKay wrote:
> I have increasingly started to use MATA in an interactive manner within my
> Stata Do Files (mainly for easy matrix functions).
> The overall structure of my program is:
> local YearList = "1995 1996 1997 1998 1999 2000"
> local END = "end"
> foreach year of local YearList {
>     {some STATA items}
>     MATA
>     {some MATA items}
>     `END'
>     {some STATA items}
> }
> display("OUT")
> To get around the parsing of the do file script issue with using "end" I
> have replaced that with a local which substitutes "end" on runtime to end
> the mata environment. This method has worked very well for me until recently
> where the foreach loop runs and everything is computed, except the program
> stops when exciting the foreach loop with the error: "program error:
> matching close brace not found".
> I have checked and rechecked and the brackets are balanced.
> Is there something syntactically wrong with using MATA in this way? Should I
> be using MATA functions instead?

Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
*   For searches and help try:

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