Statalist


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

st: Automatically generating variable names in mata


From   L B <[email protected]>
To   [email protected]
Subject   st: Automatically generating variable names in mata
Date   Fri, 24 Apr 2009 11:38:28 -0700

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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index