I am attempting to pass a value within a mata function to a stata temporary variable in an ado file.
The function test() below stores the name of a stata temporary variable in x (eg __00000R), assigns the value 1.2 to the temporary variable, and creates a tempvar named tmpname to reference the name of the temporary variable. I attempt to display the value of tmpname within the function and outside the function. The result is that tmpname evaluates to 1.2 only outside of the function:
Output:
Inside fnc, tmpname:
Outside fnc, tmpname: 1.2
How can I access tmpname within the function? The end goal is to turn test() into a objective function for optimize() that takes in an initial scalar value, passes it to a stata command, retrieves the scalar result, and computes the difference between the initial and new values.
The function test() below stores the name of a stata temporary variable in x (eg __00000R), assigns the value 1.2 to the temporary variable, and creates a tempvar named tmpname to reference the name of the temporary variable. I attempt to display the value of tmpname within the function and outside the function. The result is that tmpname evaluates to 1.2 only outside of the function:
Code:
mata void test(){ string scalar x x = st_tempname() st_numscalar(x,1.2) // x is string scalar that contains 1.2. Saved to stata scalar under a tempvar name st_local("tmpname",x) // tempvar `tmpname' in stata contains the tempvar name (eg __00000R) stata(`"di "Inside fnc, tmpname: " `tmpname'"') } test() stata(`"di "Outside fnc, tmpname: " `tmpname'"') end
Inside fnc, tmpname:
Outside fnc, tmpname: 1.2
How can I access tmpname within the function? The end goal is to turn test() into a objective function for optimize() that takes in an initial scalar value, passes it to a stata command, retrieves the scalar result, and computes the difference between the initial and new values.
Comment