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

# st: RE: looping in MATA

 From "Nick Cox" To Subject st: RE: looping in MATA Date Thu, 29 Apr 2010 15:08:33 +0100

```The key principle here is that local macro references are not part of
Mata. So, they must be interpreted before Mata sees them. Calling Mata
line by line is a little clumsy, but will work, e.g.

mata: x`y' = st_data(., (" g`y'", " g`y+1'", " g`y+2'"," g`y+3'", "
g`y+4'"), 0)

However, you must get the local macro syntax right too. `y+1', etc.
won't work as you intend. You need `=`y'+1', etc.

Nick
n.j.cox@durham.ac.uk

Olga Gorbachev

I am a beginner in Mata language, but I need to do something, that to
me sounds complicated:

I need to create a rolling loop in stata mata language, I am using
some simple matrix algebra and it is easier to do in Mata than in
matrix functions.

I need to do something like this (within a do file):
*****************************************
mata

foreach y in 1970/2000 {

x`y' = st_data(., (" g`y'", " g`y+1'", " g`y+2'"," g`y+3'", " g`y+4'"),
0)

r=rows(x`y')
vc=x`y'*x`y''
vc=vc/25
var`y'=sqrt(trace(vc)/rows(vc))
tr=vc-diag(vc)
cov`y'=sum(tr)
cov`y'=sqrt(cov`y'/(r^2-r)/2)
}

end
*************************

Ideally I would also like to save these cov`y' and var`y' data into a
metrix.

*
*   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/
```