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   Phil Schumm <pschumm@uchicago.edu>
To   Statalist Statalist <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Creating new variables in mata.
Date   Wed, 19 Feb 2014 11:40:28 -0600

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/


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