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

# st: Need help on using external global in st_view to avoid recompiling in Mata

 From <[email protected]> To <[email protected]> Subject st: Need help on using external global in st_view to avoid recompiling in Mata Date Fri, 30 Mar 2012 11:32:06 -0400

```Hello all,

I have written a Mata function to use with -moptimize- or -ml- to estimate
a discrete labour supply model.  The simplified example below works for data where
I would have 3 distinct hours choice (e.g. 0 hours, 20 hours and 40 hours) with
income for each choice.

----

version 11.2

mata:

function LSmodel_d0(transmorphic scalar M, real scalar todo, real rowvector b, real scalar lnf, real rowvector g, real matrix H)
{
real colvector beta_y, beta_h

real colvector U
real colvector sum_eU
real colvector y_chosen
real colvector h_chosen

real colvector L_1

real matrix y_i
real matrix h_i

beta_y		= moptimize_util_xb(M,b,1)
beta_h		= moptimize_util_xb(M,b,2)

st_view(y=.,.,"y_chosen")
st_view(h=.,.,"h_chosen")

st_view(y_i=.,.,("y0","y20","y40"))
st_view(h_i=.,.,("h0","h20","h40"))

U  = beta_y:*y_chosen :+ beta_h:*h_chosen

U_i  = beta_y:*y_i :+ beta_h:*h_i

L_1 = (U :- ln(sum_eU))

lnf = moptimize_util_sum(M,L_1)
}

mata mosave mata_LS_singles_quad_d2(), dir(PERSONAL) replace

end

----

Since I might use different hours choices for different group of the population (as done
in the economic literature), I would like to avoid having to recompile the code if
I want to run my estimator on, for example, 5 distinct hours choice (e.g. 0 hours,
10 hours, 20 hours, 30 hours and 40 hours) so that:

st_view(y_i=.,.,("y0","y10","y20","y30","y40"))
st_view(h_i=.,.,("h0","h10","h20","h30","h40"))

I was thinking of defining a global macro that could vary depending on the data I use.
For example, I could use:

global h_i = `""h0","h20","h40""'

or

global h_i = `""h0","h10","h20","h30","h40""'

depending on the data used.

Is it possible?

My attempts to use pointers and global macro (-findexternal()- or -valofexternal()-)
have not worked so far.

Regards,
Jean-François Bertrand

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

• Follow-Ups: