Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: mata within the loop does not work


From   Maarten buis <maartenbuis@yahoo.co.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: mata within the loop does not work
Date   Fri, 14 Jan 2011 10:14:13 +0000 (GMT)

--- On Fri, 14/1/11, Oleksandr Shepotylo wrote:
> I have the following code to run 500 Monte Carlo
> iterations
> 
> forvalues i=1(1)500  {
>     use data, clear
>     ...
>     mata:
>     ...
>     end
>     ...
> }
> 
> The loop never starts and I get the error r(1). 

The trick is to put whatever you want to do in Mata
in a Mata function, and call that Mata function 
within your Stata loop. Below is a silly example:

*----------- begin example -----------
clear all
mata
void foobar(real scalar a) {
    "hello world " + strofreal(a)
}
end

mata foobar(1)

forvalues i= 1/10 {
	mata foobar(`i')
}

*------------ end example --------------
(For more on examples I sent to the Statalist see: 
http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany

http://www.maartenbuis.nl
--------------------------


      

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index