Announcement

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

  • "do filename" does not exit when hitting an error from Mata

    I am calling a do file in a loop. On the first iteration, it hits an error in the middle of the file -- r(3200) conformability error from Mata -- and keeps going for
    • the rest of the file and
    • the rest of the loop.
    Is there some way to prevent this behavior, to stop the program when it hits an error?

    The documentation for the nostop option for do says that "normally," do stops upon seeing an error. The natural interpretation is that "normally" refers to the default behavior without the nostop option, but I suppose it is also possible that it means do stops on most but not all errors (in abnormal cases). Anyway, I would like for it to stop for all errors and so have left out the nostop option...to no avail.

  • #2
    Originally posted by Frank Erickson View Post
    I am calling a do file in a loop. On the first iteration, it hits an error in the middle of the file -- r(3200) conformability error from Mata -- and keeps going for
    • the rest of the file and
    • the rest of the loop.
    Is there some way to prevent this behavior, to stop the program when it hits an error?
    The do-file should indeed exit upon error. I suspect there is a capture command somewhere in your code which is causing the error to be captured and execution to continue.

    I set up a simple test case with two do-files to see if I could reproduce the problem. I created test1.do with a loop which calls test2.do. test2.do has Mata code in it which results in a conformability error r(3200). Execution stops as soon as the error occurs.

    Here are the files and the output:

    test1.do:
    Code:
    version 14
    
    forvalues i=1/3 {
            display "iteration `i' begins"
            do test2
            display "iteration `i' ends"
    }

    test2.do:
    Code:
    version 14
    
    display "test2.do begins"
    mata:
    x=1
    y=(2,3)
    z = x - y
    end
    
    display "test2.do ends"

    Output from do test1:

    Code:
    . do test1
    
    . version 14
    
    . 
    . forvalues i=1/3 {
      2.         display "iteration `i' begins"
      3.         do test2
      4.         display "iteration `i' ends"
      5. }
    iteration 1 begins
    
    . version 14
    
    . 
    . display "test2.do begins"
    test2.do begins
    
    . mata:
    ------------------------------------------------- mata (type end to exit) -----
    : x=1
    
    : y=(2,3)
    
    : z = x - y
                     <istmt>:  3200  conformability error
    (0 lines skipped)
    -------------------------------------------------------------------------------
    r(3200);
    
    end of do-file
    r(3200);
    
    end of do-file
    
    r(3200);
    
    .

    Comment


    • #3
      Thanks, I hadn't thought to try making a minimal example. For caller.do:

      Code:
      forvalues i = 1/3 {
        display "iteration `i' begins"
       
        clear mata
        log using "mylog_`i'.smcl", replace
        do "callit.do"
        log close
              
        display "iteration `i' ends"
      }
      and for callit.do:

      Code:
      mata /*start*/
        a = (1,2,3 \ 4,5,6)
        a[,2] + a[1,]
      end
      
      mata: b = "bah"
      mata: printf("%s\n",b)
      It looks as if logging acts like capturing in that it makes the session insensitive to (some?) errors. If that's the case, I wonder if there is a way to disable that behavior.

      Comment


      • #4
        This behavior is a result of how Mata is invoked. It is a very fine point, but there is a difference between starting a block of Mata code with mata and starting that same block of code with mata: (note the colon at the end).

        Consider an interactive Mata session. Because you type interactively, you are bound to make mistakes. You don't want to be bounced back to the Stata dot prompt (as opposed to the Mata colon prompt) just because you made a typo in your interactive session. Thus, when you start Mata by typing mata, you stay in Mata mode even when an error occurs, until you explicitly type end. This does the same thing when you are running in a do-file.

        If you change your do-file to enter Mata with mata:, then Mata will exit and return control to Stata when an error occurs. This is also true if you started a Mata session with mata: interactively, but that's not very useful.

        See [M-3] mata and read the distinction between Syntax 1 and Syntax 2. You want to use Syntax 2.

        Comment


        • #5
          Ok, thanks. That makes sense, though I still wish it were toggle-able by some global option.

          Mata blocks are easier to work with when I'm writing a program and have to deal with control flow (looping, if/else, etc.) based on Mata objects. I can generally move those objects to macros and use Stata control flow, but it's something of a pain and hard to maintain code like st_local("myvar",strofreal(myvar)) all over the place. Anyway, I'll have a look at that section of the documentation in the hope of finding a better way. Thanks again

          Comment


          • #6
            I think I wasn't clear. If you simply start your Mata block with mata: (note the colon) rather than mata with no colon, you'll achieve your desired behavior.

            That is, change your callit.do file to the following, where a colon is added after the mata command in the first block:

            Code:
            mata: /*start*/ // add a colon right after 'mata' here!
              a = (1,2,3 \ 4,5,6)
              a[,2] + a[1,]
            end
            
            mata: b = "bah"
            mata: printf("%s\n",b)

            Comment


            • #7
              Oh, wow, thanks for the clarification! For some reason, I had thought that mata: was only for one-liners.

              Comment

              Working...
              X