Bookmark and Share

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


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

Re: st: mata programming - input type


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: mata programming - input type
Date   Thu, 21 Mar 2013 10:32:55 +0000

It's just not true that Mata cannot be processed within a Stata loop,
but that does not seem to be the problem here.

The lack of macros in Mata poses challenges that are more evident than
real to many programmers who know a lot of Stata. The question was
addressed in

SJ-11-2 pr0052  . . . . Stata tip 100: Mata and the case of the missing macros
        . . . . . . . . . . . . . . . . . . . . . . . . W. Gould and N. J. Cox
        Q2/11   SJ 11(2):323--324                                (no commands)
        tip showing how to do the equivalent of Stata's macro
        substitution in Mata

and the gnomic answer is to solve the problem by avoiding it.

In the Tip above the main contribution of the junior author (meaning,
younger by just about 2 months) was to add a Perry Mason-type title to
the work of the senior author.

One good method is to pass macros once to Mata and thereafter to use
Mata's string operators and functions.

Try this:

levelsof time_period, local(period_id)

foreach period of local period_id {
       mata
       //import STATA matrices
       period = st_local("period")
       EPSILON = st_matrix("EPSILON_M" + period)
       PRICE   = st_matrix("PRICE_M" + period)
       ...
       end
}

So,

1. -period- in Mata is just a string scalar containing the same same
contents as a Stata local macro.

2. Adding that string scalar to given text is quite enough technique
for what you want.

3. More complicated manipulations just need e.g. string functions in Mata.

Nick

On Wed, Mar 20, 2013 at 10:16 PM, Tomáš Houška <[email protected]> wrote:

> I am trying to write this loop (here I give an example in "stata
> language", but I am aware that this will never work as mata cannot be
> processed within Stata loop):
>
> Example:
>
> levelsof time_period, local(period_id)
>
> foreach period of local period_id {
> mata
>         //import STATA matrices
>         EPSILON = st_matrix("EPSILON_M`period'")
>         PRICE   = st_matrix("PRICE_M`period'")
>         SHARE   = st_matrix("SHARE_M`period'")
>
>         BETA = EPSILON :* ( (SHARE *(1:/PRICE)') )
>
>        st_matrix("BETA`period'",BETA)
>
> end
> }
>
> Which means I have several matrices, each marked by period number and
> I am trying to perform some operations on them (here I give a
> simplified example of my code). I would like to create a mata function
> that would take "period_id" as an input (period_id is always in
> integer) and process the mata code, resulting in creating new matrix
> BETA`period_id'.
>
> So far I have been able to come up with this:
> Example 2:
>
> mata
> mata clear
> void mc_estim(string scalar period) {
>         //import STATA matrices
>         EPSILON = st_matrix("EPSILON_M`period'")
>         PRICE   = st_matrix("PRICE_M`period'")
>         SHARE   = st_matrix("SHARE_M`period'")
>
>         BETA = EPSILON :* ( (SHARE *(1:/PRICE)') )
>
>         st_matrix("BETA`period'",BETA)
>         }
> end
> * run mata function with a selected input
> mata mc_estim(534)
>
> But I am not sure how to use the "input" in the mata function code (I
> expect that the notation is different from Statas local macros....) I
> would appreciate any help on this.

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


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