Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: break when using mata within a loop


From   "Maarten Buis" <[email protected]>
To   <[email protected]>
Subject   st: RE: break when using mata within a loop
Date   Fri, 27 Jul 2007 13:31:25 +0200

--- Carlos Eduardo Hern�ndez Castillo wrote:
> I am trying to use mata within a forvalues loop. However Stata stops 
> the execution of the code when arriving to the "mata:" line. Could 
> you please tell me how to overcome this problem? This is the Stata 
> output:
>
> . forvalues contvar = 1(1)3 {
>   2.         mata:
>   3.                 a=1
>   4.                 a
>   5.         end
> --Break--
> r(1);
>
> end of do-file
> --Break--
> r(1);

You can define outside the loop a mata function that does the 
computations, and than call that function within the loop.

*------------ begin example -----------
mata:
void testtest(real scalar i) {
	a = i
	printf("i=%g\n", a)
}
end

forvalues i = 1/3 {
	mata: testtest(`i')
}
*---------- end example ----------------

Hope this helps,
Maarten


-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology 
Vrije Universiteit Amsterdam 
Boelelaan 1081 
1081 HV Amsterdam 
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434 

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------



*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index