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

Re: st: Automatically generating variable names in mata

From   "Glenn Goldsmith" <>
To   <>
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']

charvector = J(1,3,.)

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





L B <> 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


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
local characteristics hairchar weightchar heightchar ...


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
( the
solution proposed doesn't apply to this example.

Any ideas would be much appreciated!

*   For searches and help try:

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