Announcement

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

  • STATA command to execute code and return error message

    Hello,

    I have written a script to automatically run several do files in a folder and print either success or fail based on whether it returns _rc == 0 or not. Now I would like to add to that: if it failed, I want it to print the error message and the last 3-5 lines of syntax for why it failed. Is STATA capable of doing that? Any suggestions on how to do that?






  • #2
    The only way I know of how to see why things failed, is to -set trace on- and to look at exactly which command is breaking the code.

    I do not see how this process can be automated, and therefore I think what you are asking would be impossible.

    Comment


    • #3
      Thank you for answering my question!

      Sorry for the confusion, what I mean is when I run the script it can tell which .do file in my folder run successfully and which ones failed.

      For -set trace on-, can I save the printed error message and export it to a txt file, or is it only printed in the STATA command line?

      Comment


      • #4

        It sounds as if you have at least one do file running other do files. There are many things you can do.

        One is have a verbose mode.

        If you put

        Code:
        local verbose  *
        early in a do-file then a statement such as


        Code:
        `verbose'  di "Looking at file for this place"
        will be interpreted as a comment and never executed. Posiiively, you need to see more output when things are failing sometimes.

        Other way round

        Code:
        * local verbose *
        converts that definition itself into a comment and whenever Stata sees `verbose' it sees a local macro not defined, meaning empty, and that doesn't stop the rest of the statement being executed.

        Another thing you can do is put capture in front of anything that might go wrong and if it fails asks to see the return code.
        Code:
        capture END
        if _rc di _rc


        Code:
        set trace on 
        is in turn often the best way to debug, but the trade-off is delicate as commands call other commands and do-files call other do-files, and getting just enough to see the problem and not too much can be hard to optimise.

        Comment


        • #5
          Is it possible to make di _rc to a new variable? Or to write them in a txt file?

          Comment


          • #6
            And also write the lines that cause error into the txt file

            Comment


            • #7
              #5 You can copy _rc to a new variable with say


              Code:
              gen rc = _rc 
              but I am not clear how that would help any debugging purpose. You would just be copying for the same constant into every observation in a variable.

              #6 This is essentially the same question as before. Stata doesn't so far as I know even internally have a buffer of recent commands, and indeed as commands can call commands, do files can call do-files, ado code can call other ado or Mata code, Mata code can call other Mata code, ado and Mata code can call compiled internal code, and so on, it's not obvious what that would be, or what would help.

              There are programming languages where more debugging is possible. Indeed, as I understand it, the closer you are to machine code, the more that is easy, relatively speaking. Here in Stata you are often a long, long way from machine code, and that is usually what you would want, but here is a downside. Usually you need to add more code to show what is what at each stage.
              Last edited by Nick Cox; 07 Feb 2023, 07:31.

              Comment

              Working...
              X