Bookmark and Share

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

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

st: using MATA FOOBAR with LOOPs

From   Jason Park <>
Subject   st: using MATA FOOBAR with LOOPs
Date   Mon, 9 Dec 2013 02:31:16 +0800

Dear users,

Thanks to one of the previous threads
(, I've
come to understand that, in a do file, mata should be moved outside of
a loop to before the loop so that the loop can call it.
However, the thread only shows the case where input is a scalar.
Instead, I want the name of a matrix that mata returns as a result,
say O, to be the parameter of the mata function. As shown below, the
mata function I want my loops to call is a matrix presentation of some
detailed calculation of the outputs of VAR results.

* beginning of the do file
clear all
void foobar(O) {
L = ("aa"\"bb")
M = J(2, 1, .)
M[1,1] = `aa'
M[2,1] = `bb'
O = L,M

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:
<istmt>:  3499  O not found

Could anyone provide some tips?

Thanks in advance,
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index