Announcement

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

  • Is there a Mata function for inputting to and outputting from local macros in the calling segment?

    Fellow Statalisters (especially StataCorp):

    Are there any Mata functions to input values from, and to output values to, a macro in the calling segment? I was thinking of a Mata equivalent of the undocumented c_local command, which allows the user to reset the values of macros in the calling segment. Ideally, I would like to be able to copy values, in both directions, between a local macro in the calling segment and a variable or dataset characteristic. And, ideally, I would like to be able to do this even if the value cannot be enclosed in compound quotes (as will be the case if the value is an unpaired right compound quote).

    Is there a way to do this?

    Best wishes

    Roger

  • #2
    In Mata, the "calling segment" might be another Mata function or a Stata instance, e.g., a do-file, or ado-file. Since local macros are not a part of Mata, you are obviously referring to the latter situation.

    When you call Mata from Stata, then, technically, I would argue the caller is the Stata instance (do-file, ado-file) that calls Mata. Both share the same namespace for local macros. You can access local macros in that namespace via st_local().

    Code:
    local foo bar
    mata : st_local("foo")
    mata : st_local("foo", "42")
    display "`foo'"
    Because this is well documented, I believe you want something else. If I had to guess, I would say you might be thinking about an ado-file

    Code:
    program stata_mynamespace
        ...
        mata : mata_mynamespace(...)
    end
    
    mata :
    
    void mata_mynamespace()
    {
    ...
    set_local_in_caller_of_stata_mynamespace()
    }
    end
    that is called as

    Code:
    stata_mynamespace ...
    and supposed to set/get local macros in the namespace that is calling stata_mynamespace.

    You have already mentioned the respective non-documented Stata command to do this. Obviously, you can call this (and other) Stata commands directly from Mata using, e.g., stata(...).

    As far as I know, there is no way that users can obtain contents from local macros that are defined outside the current namespace.

    Best
    Daniel
    Last edited by daniel klein; 19 May 2019, 00:19.

    Comment


    • #3
      Thanks Daniel. What I actually had n mind WAS an ado-ffile, or more accurately a pair of ado-files challed char2local and local2char, such that

      char2local namelist

      would copy a list of dataset characteristics to local macros of the same names, and

      local2char namelist

      would copy a list of local macros nto dataset characteristcs of the same names. And I was thinkng of giving both of these ado-files a var(varname) option, to copy between locals and variable characteristics instead of between locals and dataset characteristics.

      It would probably be the best idea just to use a loop, as in

      foreach X in namelist {
      local `X' `"`_dta[`X']'"'
      }

      but I was hoping to do this as a one-liner (as I seem to do t so often these days).

      All the best
      Roger



      }

      Comment


      • #4
        Originally posted by Roger Newson View Post
        [...]

        char2local namelist

        would copy a list of dataset characteristics to local macros of the same names, and
        I think this easily done using your suggested approach; substitute local with c_local. If you are worried about weird unpaired quotes, Mata might be superior. However, I wonder how useful preserving things such as unpaired right quotes really is for anyone who wants to work with this stuff outside of your programmed environment; that is a different topic, though.

        The real problem is with you second idea.

        Originally posted by Roger Newson View Post
        [...]

        local2char namelist

        would copy a list of local macros nto dataset characteristcs of the same names.
        I do not think this can be done. If you wanted a one-liner, you could set up a Mata function, then directly call

        Code:
        mata : local2char()
        from the namespace where the locals are defined. But I doubt it gets better than this.

        Edit:

        By the way,

        Code:
        help mata ado_fromlchar()
        seems like a good place to start.

        Best
        Daniel
        Last edited by daniel klein; 19 May 2019, 11:16.

        Comment


        • #5
          Thanks for the tp about ado_fromlchar. I'll check it out.
          All the best
          Roger

          Comment


          • #6
            Originally posted by Roger Newson View Post
            Fellow Statalisters (especially StataCorp):

            Are there any Mata functions to input values from, and to output values to, a macro in the calling segment? I was thinking of a Mata equivalent of the undocumented c_local command, which allows the user to reset the values of macros in the calling segment. Ideally, I would like to be able to copy values, in both directions, between a local macro in the calling segment and a variable or dataset characteristic. And, ideally, I would like to be able to do this even if the value cannot be enclosed in compound quotes (as will be the case if the value is an unpaired right compound quote).

            Is there a way to do this?
            Now there is, in Stata 16. Stata 16 has a new, undocumented, Mata function st_c_local. It works just like the documented function st_local() except it works in the caller's macro name space rather than the current macro name space. Examples:

            Code:
            val_of_callers_x = st_c_local("x")
            st_c_local("x", "new_val_of_callers_x")

            Comment

            Working...
            X