Announcement

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

  • Mata calling Stata's -syntax- command

    Greetings,
    I am fairly new to Mata, so I wonder if I'm making the following too complicated. I have a Mata program that reads in a spreadsheet and parses it in various ways. One thing it needs to do is process a string from the spreadsheet that contains Stata-style options; it needs to split one option from the rest. If this were pure Stata, this is easy:

    Code:
    local 0 , `option_string'
    syntax , [ MYopt(numlist) * ]
    local option_string `options'
    
    di "I just parsed numlist: (`myopt')"
    But I need to do this in the middle of a bunch of Mata code. Here's what I've come up with. This works, but feels like it's more circuitous than need be?

    Code:
    program pull_option_stata
        args option_string opt_to_pull result_local
    
        local 0 , `option_string'
        syntax , [ `opt_to_pull' * ]
    
        * Mata can only reach into namespace of caller, so:
        c_local options `options'
        c_local `result_local' ``result_local''
    end
    
    mata
    string scalar pull_option(string scalar options, string scalar opt_to_pull, string scalar result_local) {
    
        stata(`"pull_option_stata ""'+options+`"" ""'+opt_to_pull+`"" ""'+result_local+`"""')
        display("Remaining options: ["+st_local("options")+"]")
        display("and now "+result_local+"==[" + st_local(result_local) + "]")
    
        options = st_local("options")
        return(st_local(result_local))
    }
    
    the_options = "this that myopt(1/3)"
    myopt = pull_option(the_options,"MYopt(numlist)","myopt")
    
    display("after pull:")
    the_options
    myopt

  • #2
    Well, a simple version would read

    Code:
    string scalar pull_option(
        
        string scalar options,
        string scalar opt_to_pull,
        string scalar name_of_opt_to_pull
        
        )
    {
        st_local("0", ","+options)
        
        stata("syntax [ ,"+opt_to_pull+" * ]")
        
        options = st_local("options")
        
        return(st_local(name_of_opt_to_pull))
    }
    Depending on who is calling this and from where you may want to be a bit more careful with overwriting Stata locals

    Code:
    string scalar pull_option(
        
        string scalar options,
        string scalar opt_to_pull,
        string scalar name_of_opt_to_pull
        
        )
    {
        string rowvector cc
        real   scalar    breakintr
        real   scalar    rc
        string scalar    result
        
        
        cc = (st_local("0"),st_local("options"),st_local(name_of_opt_to_pull))
        
        breakintr = setbreakintr(0)
        
        st_local("0", ","+options)
        
        rc = _stata("syntax [ ,"+opt_to_pull+" * ]")
        
        if ( !rc ) {
            
            options = st_local("options")
            result  = st_local(name_of_opt_to_pull)
            
        }
        
        st_local("0",cc[1])
        st_local("options",cc[2])
        st_local(name_of_opt_to_pull,cc[3])
        
        (void) setbreakintr( breakintr )
        
        if ( rc )
            exit(rc)
        
        return( result )
    }
    By the way, Mata can reach into the caller's caller namespace; but that is unnecessary here.

    Comment


    • #3
      Originally posted by daniel klein View Post
      By the way, Mata can reach into the caller's caller namespace . . .
      Okay, I'll bite, how does it do that?

      Comment


      • #4
        Originally posted by Joseph Coveney View Post
        Okay, I'll bite, how does it do that?
        OK, found it.

        Now, that's really undocumented, sort of like c_local, itself.

        (I'd already looked through help undocumented to no avail on this.)

        Comment


        • #5
          Thank you Daniel and Joseph! That's really helpful, and it's nice to know about st_c_local.

          Comment

          Working...
          X