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

# Re: : st: using MATA FOOBAR with LOOPs

 From Christopher Baum To "statalist@hsphsun2.harvard.edu" Subject Re: : st: using MATA FOOBAR with LOOPs Date Mon, 9 Dec 2013 13:18:11 +0000

```<>
On Dec 9, 2013, at 2:33 AM, Jason wrote:

>
> * beginning of the do file
> clear all
> mata
> void foobar(O) {
> L = ("aa"\"bb")
> M = J(2, 1, .)
> M[1,1] = `aa'
> M[2,1] = `bb'
> O = L,M
> }
> end
>
> foreach case in "date<=10" "date>10&date<=20" "date>20" {
> ma drop aa
> ma drop bb
> local aa = 0
> local bb = 0
> forvalues i = 1/10
> var a b if id == `i' & `case', lags(1/2)
> ma drop B
> ma B = e(b)
> local aa = `aa' + B[1,1]
> local bb = `bb'+ B[1,6]
> mata foobar(O)
> }
> * end of the do file
>
> This code returns:
> r(3499);
>
> Could anyone provide some tips?

In addition to Nick Cox’s comments about Mata, it appears that you are trying to create a Mata matrix containing string content (in L) and numeric content (in M). That will not work. Mata supports both string and numeric matrices, but not in the same object.

Nick pointed out that your use of local macros aa and bb within Mata will not work. Quite so. You could use st_local() to retrieve the current values of those locals within Mata.

If you want to construct a proper matrix in Mata and make it accessible to Stata, use st_matrix(). That function can either retrieve a Stata matrix into Mata or push a Mata matrix into a Stata matrix.

Kit Baum
Professor of Economics and Social Work, Boston College, Chestnut Hill MA, USA
DIW Research Professor, Department of Macroeconomics, DIW Berlin, Berlin, Germany
baum@bc.edu  |  http://ideas.repec.org/e/pba1.html

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```