Announcement

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

  • Mata, Loops and Stata Locals do not work

    Dear all
    I'm having a problem when I run a Stata command constructed in Mata. Here is an example (that is not my original code, but that uses the same logic).

    ************************
    mata
    var=("x1","x2","x3")
    for (v=1;v<=cols(var);v++) {
    cmd="reg y "+var[1,v]
    st_local("cmd",cmd)
    stata("`cmd'")
    stata("di `cmd'")
    }
    end
    ***********************

    This should work, but it didn't. Actually this works when I do by myself, but it didn't when I use a loop.
    The "local" just is not transferred to Stata.
    Why? Can someone help-me to make it work.
    Thanks in advance

  • #2
    Hi
    Look at
    Code:
    cls
    sysuse auto, clear
    
    mata
    var=("mpg","weight","length")
    for (v=1;v<=cols(var);v++) {
        cmd = sprintf("reg price %s", var[v])
        stata(cmd)
        cmd
    }
    end
    There are several changes to your code.
    Some you may like, use what you want
    Kind regards

    nhb

    Comment


    • #3
      What is the error message you get? In the mean time this should work

      Code:
      sysuse auto , clear
      
      mata
      
      var =("trunk","mpg")
      for (v=1;v<=cols(var);v++) {
      cmd="reg price "+var[1,v]
      stata(cmd)
      
      }
      end
      and your last statement within the loop has quotes missing and should be
      Code:
      stata("di "+char(34)+"`cmd'" +char(34))
      in order to work. If you did not get an error message it means, like you mention it, that the local cmd was empty.

      Comment


      • #4

        Thank you guys,
        Yes Christophe the local was empty. Sorry to not specify the error.
        And thanks for the code Niels. That works just fine.

        Comment


        • #5
          And Christophe the last statement was just to check if there was something in the "local".

          Comment


          • #6
            See http://www.stata-journal.com/sjpdf.h...iclenum=pr0040 for an overview here.

            Comment


            • #7
              I understood that it was for testing. My point was that if your local hadn't been empty and cmd was a string then your code would have triggered an error message, therefore you have to add the quotes. Besides it is strange that `cmd' is empty. It seems that the local cmd changes value only once you have returned to Stata.

              Comment


              • #8
                I think it is more important to say that you only should use macro's inside Mata in very rare cases.
                Both Christophe and I builds the Stata command purely in a Mata string and insert this inside the -stata- function.

                Also instead of using the Mata function -stata- and the Stata command -display- then do the whole thing using the Mata function -printf- instead.
                Kind regards

                nhb

                Comment


                • #9
                  Also if one has defined a map function in Mata as done below one only needs 2 lines of code to get the job done:
                  Code:
                  cls
                  sysuse auto, clear
                  
                  mata
                      mata drop map() do_regression()
                      
                      function map(f, row)
                      {
                          if (cols(row) == 1){
                              return((*f)(row[1]))
                          } else {
                              return((*f)(row[1]), map(&(*f), row[2::cols(row)]))
                          }
                      }
                  
                      function do_regression(v) stata(sprintf("reg price %s", v))
                      map(&do_regression(), ("mpg", "weight", "length"))
                  end
                  With a bit of training the map function becomes quite valuable when coding
                  Kind regards

                  nhb

                  Comment

                  Working...
                  X