Announcement

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

  • How can a funtion get an input from a local

    I am struggling with defining a function that will accept a Stata macro and then tokenize that macro into two elements. I have tried the following but did not succeed. The following code is just an example, I am trying to learn how one can transfer contents of a local macro to Mata function.
    Code:
    local anything "2 6"
    mata
    function fasm (string scalar anything)
    {
                            t = tokeninitstata()
                           tokenset(t, st_local("anything"))
                            formation = tokenget(t)
                            testing = tokenget(t)
                            st_global(FRM, formation)
                            st_global(TST, testing)
    }
    end
    
    . mata: fasm("`anything'")
                   st_global():  3254  nonstring found where string required
                      fasm():     -  function returned error
                     <istmt>:     -  function returned error
    r(3254);
    Last edited by Saeed Sardar; 18 Jan 2017, 06:17.

  • #2
    I'm assuming you have a good reason for using tokeninitstata() and related low-level functions instead of just calling tokens(). Besides that, two problems:
    1. You need to use st_global("FRM", formation) , with FRM in quotes. Otherwise, it will be treated as a Mata variable name.
    2. You are doing st_local("anything") which loads the corresponding local variable. If you are already hard-coding the name to be that, why are you accepting an argument, i.e. string scalar anything? Either pass the string as an argument and don't call st_local, or hard-code the name of the local and have no args in the mata function

    Comment


    • #3
      Sergio Correia Thanks for your reply, that was helpful. You mentioned that we can use tokens(), can you show how can we get elements of the the macro anything to two separate variables; say the first element of the local is equal to token1=2, and the second element is equal to token2=6

      Comment


      • #4
        I'm not sure if I follow, but is this what you are thinking about?

        Code:
        . local anything 2 6
        
        . mata
        ------------------------------------------------- mata (type end to exit) ----------------------------------------------------
        : list = tokens(st_local("anything"))
        
        : list
               1   2
            +---------+
          1 |  2   6  |
            +---------+
        
        : token1 = list[1]
        
        : token2 = list[2]
        
        : token1
          2
        
        : token2
          6
        
        : end
        --------
        I just index the result of tokens to extract the first and second element.

        Comment


        • #5
          That was really helpful. I appreciate your time.

          Comment

          Working...
          X