Announcement

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

  • use of -st_global()-

    Hi -- I am trying to understand the purpose of the mata function -st_global()-. Because it just returns the contents in string form of what is in the named global macro, I do not understand why it is needed. In particular, if we have some macro with name -macName-, can't we just get the same result with "$macName" ? Here is an example. Comment? -- P

    . // looking at retrieving the contents of a global macro in mata
    . global testmac wow. what a fun narrative

    . dis "$testmac"
    wow. what a fun narrative

    . mata
    ------------------------------------------------- mata (type end to exit) -----
    : "$testmac"
    wow. what a fun narrative

    : maccontents=st_global("testmac")

    : maccontents
    wow. what a fun narrative

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


  • #2
    The core idea is that Mata is a compiled language. That means you typically do not use it interactively but write functions that you call later. When writing a function as

    Code:
    void printf_testmac()
    {
        printf("${testmac}\n")
    }
    there are two major problems. First, you probably do not want to hardwire the name testmac into that function. But, say, you did, there is another problem. By the time you compile your function (compile time), it substitutes ${testmac} for whatever the contents of global testmac is then. And, when you call your function later, it will re-display this content, regardless of the contents of global testmac at run time. Here is what that looks like:

    Code:
    . dis "$testmac"
    wow. what a fun narrative
    
    .
    . mata :
    ------------------------------------------------- mata (type end to exit) -----------------------------------------------------------------------------------------------------------------------------
    :
    : void printf_testmac()
    > {
    >     printf("${testmac}\n")
    > }
    
    :
    : end
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    .
    . mata : printf_testmac()
    wow. what a fun narrative
    
    .
    . global testmac "This will not show in Mata"
    
    . display "$testmac"
    This will not show in Mata
    
    .
    . mata : printf_testmac()
    wow. what a fun narrative
    This is almost certainly not what you want.

    Here is the same thing - hardwired name - but using st_global():

    Code:
    . mata :
    ------------------------------------------------- mata (type end to exit) -----------------------------------------------------------------------------------------------------------------------------
    :
    : void printf_testmac()
    > {
    >     printf("%s\n", st_global("testmac"))
    > }
    
    :
    : end
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    .
    . mata : printf_testmac()
    wow. what a fun narrative
    
    .
    . global testmac "This will show in Mata"
    
    . display "$testmac"
    This will show in Mata
    
    .
    . mata : printf_testmac()
    This will show in Mata
    Last edited by daniel klein; 19 Feb 2024, 15:46.

    Comment


    • #3
      st_global() is used to get and set contents of return, ereturn and creturl macros directly. You cannot access r() and e() without it.

      Comment


      • #4
        Daniel -- This is an awesome answer! I think I learned my lesson! I have to go back and look carefully at my use of macros in my mata code! -- P

        Comment


        • #5
          Hi Again -- I am *pretty sure* this is not a related issue, but you now have me thinking about these things. In the attached screenshot from p.307 of the [BAYES] manual, I see that in a -stata- program (not a -mata- function), the authors define local macros `d' and `n' from global macros $MH_yn and $MH_n (in red). Then, they pass the contents of `d' and 'n' to a -mata- function. BUT, is there. any reason to define `d' and `n' here? Could they not just have passed the contents of $MH_yn and $MH_n to the -mata- function?

          Screenshot 2024-02-19 at 6.33.10 PM.png

          Comment


          • #6
            Technically, there is no apparent reason to define these local macros. Perhaps, the authors intended to make the code more readable, which is always a good idea (that I do not follow nearly as often as I should).

            Comment


            • #7
              Hi, this is a MATA Select Reweight do file embedded with reweight file ADO extension. The objective of this code was to reweight the NSS household dataset. This code breaks and says function returned error for Katya call function. The specific error it shows is " katya(): 3001 expected 2 arguments but received 1
              <istmt>: - function returned error
              r(3001);

              Kindly help me fix this error.

              clear mata

              mata:
              void katya(string scalar variables)
              {
              varnames=tokens(st_global(variables))

              x=st_data(., ("c1f00 - c3m70"))
              y=st_data(., "const")

              submat = select(y, rowmissing(y):< $sc_alpha)

              z = select(x',submat:>= $sc_alpha)'
              q = select(submat,submat:>= $sc_alpha)

              colnames=select(varnames',submat:>= $sc_alpha)
              colnames=J(rows(colnames),1,""), colnames

              q0 = select(q ,rowsum(z'):>= $sc_alpha)

              colnames0 = select(colnames,rowsum(z'):>= $sc_alpha)

              qnull = select(q ,rowsum(z'):< $sc_alpha)'
              colnull = select(colnames, rowsum(z'):< $sc_alpha)


              q0_ = q0'
              st_matrix("Q0",q0_)
              st_matrixcolstripe("Q0", colnames0)
              st_matrix("Y", q0)

              }
              end

              Comment

              Working...
              X