Bookmark and Share

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: Creating new variables in mata.


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Creating new variables in mata.
Date   Wed, 19 Feb 2014 19:33:10 +0000

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
[email protected]


On 19 February 2014 17:40, Phil Schumm <[email protected]> wrote:
> On Feb 19, 2014, at 9:22 AM, Amadou DIALLO <[email protected]> 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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index