Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

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

st: mata in stata loop

From   Tomáš Houška <>
Subject   st: mata in stata loop
Date   Fri, 5 Apr 2013 20:25:25 +0200

Dear Statalist,

I know that this has been covered in other posts several times, but I
would nevertheless appreciate your help... I have gone through help
and over all the other posts, but I cannot get a working solution.

I have data for markets and time periods and for each combination I am
doing long list of computations that I in the end save using
-postfile- command.

In one section of all my computations, I have a mata code that loops
over all combinations of markets and time periods in mata loop - the
input are previously created matrices, and the aim is to obtain matrix
of costs (see code below).

I would like to embed this into a wider Stata loop, that changes the
input matrices, so for every iteration of the Stata loop I end up with
different elements inside the cost matrix that is being calculated.
The aim is to see how the results of long list of calculations depend
on the input parameters.

For this I need to embed my mata loops inside the stata loop. I have
tried rewriting it with mata -void- function, but I cannot get it
working. Any pointers are appreciated....

Eventually, my Stata function should be usable in this manner:

levelsof market_name, local(market_id)
levelsof period_list, local(period_id)

mata premerger(market_id, period_id)

...creating a cost matrix

My current void function code is (all the matrices that the function
uses are already created in Stata memory with previous calculations,
mata just has to get them and calculate cost matrix for each
market/period combination):

void premerger(string period_id, string market_id) {

period = tokens(period_id)
market = tokens(market_id)

for(i = 1; i<=length(period); i++) {
for(s = 1; s<=length(market); s++) {

//import STATA matrices
EPSILON = st_matrix("EM_m"+market[s]+"_p"+period[i])
PRICE   = st_matrix("PRICEM_m"+market[s]+"_p"+period[i])
SHARE   = st_matrix("SHAREM_m"+market[s]+"_p"+period[i])
OWNER   = st_matrix("OM_m"+market[s]+"_p"+period[i])

BETA = EPSILON :* ( (SHARE *(1:/PRICE)') )
COST = PRICE + luinv(OB) * SHARE

// saving results into Stata


Thank you for any tips!
*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index