Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: mata within the loop does not work


From   Maarten buis <[email protected]>
To   [email protected]
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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index