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: generate stata variables in mata and set the names from matrices
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: generate stata variables in mata and set the names from matrices
Date
Fri, 29 Apr 2011 12:37:36 +0100
You must do more than change that one line! The references to `local'
need changing too.
But a better strategy is just to avoid using locals or globals
altogether and to do it the Mata way.
e.g.
clear
set obs 100
mata
x = J(100, 3, 42)
for (j=1; j<=cols(x); j++) {
yname = "y" + strofreal(j)
st_addvar("float", yname)
st_store(., yname, x[,j])
}
end
See also Bill Gould at
<http://www.hsph.harvard.edu/cgi-bin/lwgate/STATALIST/archives/statalist.1101/Author/article-871.html>
and in due course Stata Tip 100 (Stata Journal in press).
Nick
On Fri, Apr 29, 2011 at 12:11 PM, Cyrus Levy <[email protected]> wrote:
> I changed the line below
>
> stata(`"local namer=radian[1,rd]"')
>
> to
>
> stata(`"global namer=radian[1,rd]"')
>
> but it still doesn't work as I want... Line by line entry works this way as
> well.
> (by the way, there must be a shorter way to code what I want like 3 lines
> shorter but how)
>
>
> On 4/29/2011 5:36 AM, Nick Cox wrote:
>>
>> Evidently a local macro defined in one place is not visible in another.
>>
>> See a posting from Tuesday:
>>
>> http://www.stata.com/statalist/archive/2011-04/msg01227.html
>>
>> Nick
>>
>> 2011/4/29 Cyrus Levy<[email protected]>:
>>>
>>> I want to generate stata variables in mata, and set the names of those
>>> variables using a mata matrix. I use the code below which is supposed to
>>> work -it works when I enter the lines line by line-, but then when I run
>>> it
>>> in a do file, it doesn't work. It doesn't generate a HHI_R50 variable. It
>>> generates a HHI_R variable with no extension referring to the
>>> corresponding
>>> matrix value. What's wrong?
>>>
>>> ...
>>> mata
>>> ...
>>> radian=(50,75,200)
>>> for (rd=1; rd<=cols(radian); rd++) {
>>> ...
>>> st_matrix("radian", radian)
>>> st_numscalar("rd", rd)
>>> stata(`"local namer=radian[1,rd]"')
>>> stata("gen HHI_R`namer'=.")
>>> st_store(., "HHI_R`namer'", HHI_R)
>>> }
>>> end
>>> ...
>>>
*
* 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/