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

# Re: st: Creating new variables in mata.

 From Amadou DIALLO To statalist@hsphsun2.harvard.edu Subject Re: st: Creating new variables in mata. Date Wed, 19 Feb 2014 22:52:16 +0100

```Many thanks, Nick, Phil and Anthony,

I was just trying to understand parsing and compiling in mata vs stata.

In the same spirit, creating incremented names in mata will be:

for (i=1; i<=3; i++) {
a = strofreal(i)
b = "M" + a
b
}
which will give M1 M2 M3.

However, I can't find a way to increment the creation of matrices M1,
M2, M3 (where M1=1, M2=2, etc.):

m:

/*
for (i=1; i<=3; i++) {
a = strofreal(i)
b = "M" + a
b
(void) "M" + a = i
}
*/
for (i=1; i<=3; i++) {
a = "local b " + strofreal(i)
a
//stata(`"a"')
st_local(b, a)
"`b'"
stata("di `b'")
"M`b'"
M`b' = i
M`b'
}
end

I don't understand especially why st_local is returning an empty macro.

Thanks.

2014-02-19 20:33 UTC+01:00, Nick Cox <njcoxstata@gmail.com>:
> I agree with Phil's specific suggestions and general stance here. I
> tend to ask myself, which tasks are easier done in Stata and which in
> Mata? In the absence of other information, I would regard dropping
> variables and creating them as usually easier in Stata.
>
> Naturally, there can be reasons for doing it in Mata. Similarly, there
> are other desiderata. For example, moving back and forth between Stata
> and Mata a great deal doesn't seem ideal.
>
> So, we are left with the simple but vital point that advising well
> would be easier if we knew your overall problem.
>
> A small detail here is that
>
> varname = var + strofreal(i)
>
> is another way to do it.
> Nick
> njcoxstata@gmail.com
>
>
> On 19 February 2014 17:40, Phil Schumm <pschumm@uchicago.edu> wrote:
>> On Feb 19, 2014, at 9:22 AM, Amadou DIALLO <stata.diallo@gmail.com>
>> wrote:
>>> Maybe this has been already answered, but I can't find it.  What is the
>>> equivalent of the stata code below in mata?
>>>
>>> forv i = 1/10 {
>>>   cap drop var`i'
>>>   g var`i' = ...
>>> }
>>>
>>> I've read Bill's "mata matters: macros" and creating new variables, but I
>>> can't find how to parse the "i".
>>
>>
>> A direct translation would look something like this:
>>
>>
>>     for (i=1; i<=10; i++) {
>>         varname = sprintf("var%f", i)
>>         if (_st_varindex(varname)!=.) {
>>             st_dropvar(varname)
>>         }
>>         (void) st_addvar(<type>, varname)
>>         // use st_store() to fill the new variable
>>     }
>>
>>
>> where <type> is the storage type of the new variables you are creating.
>> However, without some more detail about what you are trying to do, it is
>> impossible to say whether this will be useful to you (or even whether it
>> is appropriate to be doing this in Mata).
>>
>>
>> -- Phil
>>
>>
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>> *   http://www.ats.ucla.edu/stat/stata/
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
>

--
Amadou B. DIALLO, PhD.
Senior Economist, AfDB.
a.b.diallo@afdb.org
+21671101789
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```