Bookmark and Share

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

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

Re: st: Creating new variables in mata.

From   Phil Schumm <>
To   Statalist Statalist <>
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 <> 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)!=.) {
        (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:

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