Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mata: Retrieving (string) names of objects

    Hello,

    I found a way of retrieving (string) names of objects in Mata. My solution uses pointers. I did not find a post on this task before, so I am posting my solution here for the sake of other users.

    Assume we are interested in the name of the object testvar. The name of the object testvar can be stored in the string namevar by using the function nameexternal:

    Code:
    namevar=nameexternal(&testvar)
    In the following application, a variable is imported from Stata to Mata and stored in a vector with the same name as the original variable:

    Code:
    version 13
    clear all
    
    set obs 2
    gen testvar=1
    mata
        function vartomata2 (real vector var) {
            var=st_data(., nameexternal(&var))
        }
        mata describe
        testvar=J(st_nobs(),1,.)    
        vartomata2(testvar)
        mata describe    
        testvar
    end
Working...
X