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: trouble with passing a global macro to stata() in a Mata function


From   Matthew White <[email protected]>
To   [email protected]
Subject   Re: st: trouble with passing a global macro to stata() in a Mata function
Date   Thu, 29 Aug 2013 12:21:55 -0400

Hi Limin,

-stata("reshape long $stubnames, i(id) j(alternative)")- is within a
Mata function, so $stubnames is expanded when the function is
compiled: $stubnames does not refer to the run-time value of the
global. To use the run-time value, one option is -st_global()-:

stata(sprintf("reshape long %s, i(id) j(alternative)", st_global("stubnames"))

Or I think you could use the \ escape character:

-stata("reshape long \$stubnames, i(id) j(alternative)")-

For more, see the article "Mata Matters: Macros" in the Stata Journal:
<http://www.stata-journal.com/sjpdf.html?articlenum=pr0040>.

Best,
Matt

On Thu, Aug 29, 2013 at 12:07 PM, Limin Fang
<[email protected]> wrote:
> Dear Statalist users,
>
> I am writing a Stata program that involves automatically generating a list of temporary variable names, which I put in a global macro called "stubnames."   I then need to pass this "stubnames" and a number of matrices to a Mata function that I designed. In the Mata function, I also need to return to Stata environment in order to use its built-in command such as "reshape" and "asclogit."  I use the function "state()" to implement these commands. Since both "reshape" and "asclogit" require the input of variable names that I stored in the global macro "stubnames,"  I want to write something like stata("reshape long $stubnames, i(id) j(alternative)"), but stata() is not recognizing the global macro $stubnames, although once I finish running the program and type in "macro dir," the global macro "stubnames" is listed.
>
> I don't quite understand why state() within my Mata function cannot recognize the global macro "stubnames". I should also add that if I run the program again, state() does pick up the global macro "stubnames" that has already been defined in the first run of the program. Would anyone kindly explain to me why this is happening? Also could anyone help me solve this problem by any chance?
>
> I am including an abridged version of my code if this helps:
>
> program nplsing, rclass
>         version 10
>         syntax varlist [if] [in], NPAR(real) [ITERation(real 5) ID(varname numeric) NChoice(real 2) ///
>                 NUMX(real 2) Bdisc(real 0.99) NAMES(string)]
>         ……
>
>         forvalues i = 1/`npar'{
>                 local stubnames "`stubnames' z`i'_ "
>                 }
>         global stubnames "`stubnames' e"
>         ……
>
>         mata: nplsing("`zevarlist'", "zmat", "pini", "fmat", `bdisc', `iteration', "`touse'")
>         ……
> end
>
> version 10
> mata:
> void nplsing(string scalar tempvarlist, ///
>                                          string scalar zmatname, ///
>                                          string scalar pininame, ///
>                                          string scalar fmatname, ///
>                                          real scalar bdisc, ///
>                                          real scalar iterlim, ///
>                                          string scalar touse)
> {
>         …….
>         while (convcrit >= 1e-6){
>                 ……
>                 stata("preserve")
>                 ……
>                 stata("reshape long $stubnames, i(id) j(alternative)")
>                 ……
>                 stata("asclogit chosen $stubnames, case(id) alternative(alternative) noconstant constraint(1) shownrtolerance tol(1e-10)")
>                 ……
>                 stata("restore")
>                 ……
>                 convcrit = colmax(abs(tetaest1 - tetaest0))
>                 ……
>         }
>
> }
> end
>
> In case you suggest me breaking the mata function into pieces and return back to Stata environment to implement "reshape " and "asclogit," I would say that I'd rather keep this nplsing Mata function as one piece because of the loop "while (convcrit >= 1e-6)." I find it rather tedious to send matrices back and forth between Mata and Stata. More importantly I would have to create a local macro "convcrit" in Stata for the stopping rule in "while"and update it constantly with a value of a scalar that I receive from the Mata function, and I don't quite know how to do that just yet in Stata.
>
> Nevertheless, any advice in helping me solve my problem is welcome. Thank you very much in advance!
>
> Limin Fang
> Department of Economics
> University of Toronto
>
>
>
> *
> *   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/



-- 
Matthew White
Senior Project Associate
Innovations for Poverty Action
101 Whitney Avenue, New Haven, CT 06510 USA
www.poverty-action.org

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