Statalist


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

Re: st: Automatically generating variable names in mata


From   "Glenn Goldsmith" <glenn.goldsmith@gmail.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Automatically generating variable names in mata
Date   Sat, 25 Apr 2009 13:32:13 +0100

<>

If you want, you can define the locals `haircharname', `weightcharname' etc.
in stata so that e.g. `haircharname' evaluates to charvector[1],
`weightcharname' evaluates to charvector[2] etc. Then, typing `haircharname'
in mata will return the value of `hairchar'.

This isn't quite what you asked for, but may do the job you need, depending
on how you intend to use the mata variables afterwards.

**************************************************************
// Initialize locals
local hairchar = 1
local weightchar = 2
local heightchar = 3

// Define xcharname, ycharname ... locals 
local characteristics hairchar weightchar heightchar
forvalues i = 1/`: word count `characteristics'' {
  local `: word `i' of `characteristics''name charvector[`i']
}

mata:
charnames=tokens(st_local("characteristics"))
charvector = J(1,3,.)

for (i=1; i<=cols(charvector); i++) {
    charvector[i] = strtoreal(st_local(charnames[i]))
}

`haircharname'
`weightcharname'
`heightcharname'

end
**************************************************************

HTH

Glenn

L B <luli.econ@gmail.com> wrote:

I'm new to mata so I apologize if this is an obvious question. I have
a set of locals (although they could also be scalars I guess) I need
to pass on to mata. They're a lot, and so instead of typing out line
by line, say

hairchar=st_local("hairchar")
heightchar=st_local("heightchar")
etc

I want to do something that will create all the mata variables using a
list of which locals to bring into mata, and also to dynamically
create the variable names to match the local names, i.e. I want to
create a mata variable called "hairchar" with the contents of the
local "hairchar". This is typically something you'd do with macros in
Stata so I'm not sure how to do this in Mata. I want to do something
"like":
local characteristics hairchar weightchar heightchar ...

mata:
charvector=tokens(st_local(characteristics))

for (i=1; 1=cols(charvector), i++) {
    `charvector[i]' = st_local(charvector[i])
}

In mata I use each of these at separate points in my code so I'd
rather keep each as a separate variable with a distinct name instead
of setting them up as variables in stata, passing them to mata as a
one row vector containing all the locals, eg.  charvector = [hairchar
weightchar heightchar etc] and then having to remember that when I
want heightchar I should use charvector[3] and when I want
manicurechar I should use charvectr[17]

 Someone asked a similar question like this in the past
(http://www.stata.com/statalist/archive/2008-03/msg00861.html)but the
solution proposed doesn't apply to this example.

Any ideas would be much appreciated!

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



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index