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: dynamic line execution in mata


From   Phil Schumm <[email protected]>
To   Statalist Statalist <[email protected]>
Subject   Re: st: dynamic line execution in mata
Date   Mon, 10 Feb 2014 12:05:42 -0600

On Feb 10, 2014, at 11:38 AM, Andrew Maurer <[email protected]> wrote:
> I am trying to find Mata's equivalent of Stata's macro expansion functionality.


You may use Stata's macros inside of Mata code, however they are evaluated only once at compile time, so their utility is limited.  For example, they can represent a useful way of referring to constants within a program, but in general, you're better off avoiding them.  See http://www.stata-journal.com/article.html?article=pr0040 for more information.


> In the below example, I first define an object thisvar as the string "date" and I define the object date as the column vector 1 \ 2 \ 3 \ 4 \ 5. How can I return the contents of the "date" object by only referencing "thisvar"?

<snip>

> i = 1
> date = 1 \ 2 \ 3 \ 4 \ 5
> varnames = "date", "price"
> thisvar = varnames[1,i]
> rows( thisvar ) // output: 1
> rows( date ) // output: 5


It's unclear what you're trying to do here, but if you're trying to use a string like "date" to refer to an existing object, one option is to use an associative array.  For example:

    i = 1
    mydict = asarray_create()
    asarray(mydict, "date", (1 \ 2 \ 3 \ 4 \ 5))
    varnames = "date", "price"
    thisvar = varnames[1,i]
    rows(asarray(mydict, thisvar))


-- 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