Announcement

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

  • Trace back output of loops

    Dear Statalist forum,

    I need to understand someone else's code, which includes a long loop containing many local variables and temp-files. I do not completely understand what is done by just looking at the code, so it would certainly help to run each line of code step by step and see what each line of code does with my dataset.

    However, I am struggling to find commands that allow me to do so within a loop, where everything is executed at once. For instance, I was already using
    Code:
    set trace on
    etc., but this just helps me spot the exact line where my error occurs.

    So, I am wondering if you might have any tips for commands you usually use when trying to understand single lines of codes of long loops (if something like that exists)?

    Many thanks in advance.

  • #2
    You can pepper the code with display statements even down to

    Code:
    di 1 
    di 2 
    di 3
    so that you can see where you got to.

    I sometimes use a brutal device of putting say

    Code:
    STOP
    at some point in the code so at that point it fails, given an illegal command. Then you can look at your local macros and your dataset to see where you are. Then move that break to later in the code.

    It is a common view that Stata debugging is not especially easy, but then there are some good reasons for that.

    Comment


    • #3
      and
      Code:
      set traceexpand on
      will show the lines after macro expansion

      Comment


      • #4
        Code:
        help pause
        provides another approach to allowing you to see what your code does with your dataset.

        Comment


        • #5
          Thank you all for the tips, I will try them.

          Comment

          Working...
          X