Announcement

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

  • When Stata if command is false, calling on Mata causes errors

    Hi everyone,
    Using Stata 15.1 MP8, I've encountered a variety of errors that seem to be caused by calling on Mata within a Stata if command that is evaluated as false. Try this:

    loc run="no"
    if "`run'"=="yes" {
    mata {
    test=1
    test
    }
    }
    else di "working fine"


    The error returned is:

    } is not a valid command name


    Obviously the brackets are being treated in a specific way, as if the closing Mata bracket is being ignored? Some evidence for this is found with the following code, which works by excluding one of the closing brackets:

    loc run="no"
    if "`run'"=="yes" {
    mata {
    test=1
    test
    }
    else di "working fine"


    Furthermore, when the statement is evaluated as true, everything is fine. This, of course, can be shown by starting the first set of code with: loc run="yes".

    The problem here can be significant, such as in my case. I'm coding a variety of analysis procedures that combine Mata and Stata, and I want to give users the option to skip over large sections of the code. For this, I'm using locals at the start of the .do file that can be set as "yes" or "no" and then evaluated as true or false in order to skip over lots of Mata and Stata code (e.g., if "`run_code'"=="yes" { ... ).

    Any help here is greatly appreciated! Apologies if this issue has been addressed elsewhere. I've been looking around for a few days on-and-off, but couldn't find this issue addressed.

    Mike

  • #2
    There are errors in your code but I don't see that Mata is at fault. Nor does the placing inside an if else branch seem the main point here.

    I was surprised to find that
    Code:
    mata {
    is allowed, but it is, yet once you have done it different rules take over. Once you have entered Mata you need an end statement to get out; hence the error message. Otherwise put, Mata can't be expected to know your thinking that you are inside a pair of Stata braces.

    This is one way to get your code to work and there are others.

    Code:
    loc run = "no"
    if "`run'" == "yes" {
    mata: test = 1 ; test
    }
    else di "working fine"


    For more complicated code that would need to become a function call.

    Comment


    • #3
      Hi Nick,
      Thanks for your help. My style of coding is primarily meant to allow running multiple lines of mata code in a .do file without the "end" statement halting the .do file. Is there a better way to do this that allows embedding multiple lines of Mata code in a .do file?

      More specifically, I still don't understand what the problem is with my original code. If the the initial if statement is evaluated as false, shouldn't everything that follows within the brackets be ignored? Is the mata command not being ignored... or is only some part of it being ignored?

      Thanks again,
      Mike

      Comment


      • #4
        I can't add much. I don't code that way and I certainly don't recommend it. There is perhaps room for small debate on whether Stata/Mata is confused or your code is but the result is the same. The error message shows to me that whatever is parsing your code thinks you're in Mata. If you're in Mata and want to get out, you need to get out. The construct

        Code:
        mata { 
        
        }
        is I think one you're dreaming up as syntax you would like. That's nothing to do with your (or anybody's) programming style. If you can find that documented or working in anybody's code please let me know.

        It seems that the { brace in
        Code:
        mata {
        is being ignored but if that is what you guess then you can't expect the } brace to be recognised as its twin.

        Comment


        • #5
          The " mata { " approach works well in other cases, but obviously not within the brackets of an if command that is evaluated as false. Originally I may have got this mata approach from a Statalist post by Niels Henrik Bruun. The curious case of how it's beings parsed may have to remain a mystery. However, it's a nice way to write mata code in a .do file (at least I thought) because you only need to use the term "mata" once.

          Practically speaking I'm trying to ensure that mata code is not being run when it exists within the brackets of an if command that is evaluated as false. This is important because I'm using locals that are defined within the brackets to create and modify matrices in mata. If mata is being run without the locals properly defined, this can results in errors. Indeed, what led me to figure out the problem I've identified was an error "nothing found where subexp expected," which was related to mata being run when I expected it wouldn't be.

          To ensure this is not happening, it would be a shame to have to insert "mata:" instead of using the brackets as I have been. Perhaps unfortunately, I've got about 2,000 lines of code that rely pretty heavily on the " mata { " construct.

          Thanks again for your input,
          Mike

          Comment


          • #6
            You could open a new thread under Mata. A few people strongly interested in Mata appear to watch that and General less.

            Comment

            Working...
            X