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