Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Mata r() strings


From   [email protected] (William Gould, StataCorp LP)
To   [email protected]
Subject   Re: st: Mata r() strings
Date   Mon, 27 Aug 2007 09:43:03 -0500

Sergiy Radyakin <[email protected]> reports, 

> I want to return a string among the r() values from a Mata function.
> 
> According to the manual, the following function is to be used in this case:
> > st_strscalar(string scalar name, string value)
> [...]
>
> It does not seem to work however. The following is what happened:
> **-------------1--------------------------------------------------
>         : mata clear
>         : void function my_test() {
>         > st_strscalar("r(my_name)" ,"my_value")
>         > }
>         : my_test()
>                   st_strscalar():  3300  argument out of range
>                        my_test():     -  function returned error
>                          <istmt>:     -  function returned error
>         r(3300);
> 
> As an alternative, I tried to store my string value to a local via st_local:
> **-------------2--------------------------------------------------
>         : mata clear
>         : void function my_test() {
>         > st_local("r(my_name)" ,"my_value")
>         > }
>         : my_test()
>         _r(my_name) invalid name
>                       st_local():  3300  argument out of range
>                        my_test():     -  function returned error
>                          <istmt>:     -  function returned error
>         r(3300);

First, "string scalars" are not currently allowed in r(), and that is why 
Mata gave an error when Sergiy coded

            st_strscalar("r(my_name)" ,"my_value")

What he coded would be correct syntax if Stata had string scalars.

Stata expects strings in r() to be returned in macros. Sergiy tried, 


            st_local("r(my_name)" ,"my_value")

but that did not work.  What would have worked is

            st_global("r(my_name)" ,"my_value")

and here's a demonstration:

        . mata
        ----------------------------------------- mata (type end to exit) -----
        : st_rclear()

        : st_global("r(myname)", "a string value")

        : end
        -----------------------------------------------------------------------

        . ret list 

        macros:
                     r(myname) : "a string value"

        . _

It's a fine point, but things like r(myname) are considered to be global 
macros and not local macros because those macros are exposed outside of 
the program that set them.

-- Bill
[email protected]
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index