Announcement

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

  • Tracing a foreach loop

    Is there any way to trace execution within a -foreach- loop? I have a very long loop that abends with an error message but no indication of which line of code the message refers to. -set trace- does not help. In the past I have sometimes added a -display- statement between each command, but I hope there is a better way.

  • #2
    Hi! When I have such a problem, I stop the loop after each step with a "STOP"-command to see which line causes the error.

    Comment


    • #3
      In which sense did -set trace- not help? Normally it works quite well if you set the correct tracedepth. I.e. usually you'd want something along the lines of -set trace on- -set tracedepth 2-.

      Comment


      • #4
        I guess the problem is that with trace on each line of my code generates hundreds or thousands of lines of trace output as each of the Stata commands I call executes. This is true even at a tracedepth of one. I find it difficult to find my code in the mass of tracing inside the Stata provided commands that are written as ado files, which includes many of the most commonly used commands. . For a moment I thought I could set tracedepth to 0 and get only my program lines in the trace output, but that isn't allowed. The only way I can see to isolate which line of my .do file is responsible for the error message is to set trace on and to filter the resulting log so that only the unindented lines of trace are kept. That would be the lines with a non-blank in column 1. haven't tried it yet, though.

        Comment


        • #5
          Note that the problem is that Stata does not echo the command lines within a foreach loop, and set trace on echoes not only the command but all the commands within any executed .ado files. The default is too little, the trace is too much. Here is a hack I can use:taken from:

          http://www.stata.com/statalist/archive/2013-07/msg00116.html

          Code:
          ** Program pe: Print Execute **
          program define pe
              version 12.0
              if `"`0'"' != "" {
                  display as text `"`0'"'
                  `0'
                  display("")
              }
          end
          One can just prepend pe to each line within the foreach loop.
          It solves the problem of identifying what is executing at any step within the foreach loop and is only a little ugly.

          Comment


          • #6
            Originally posted by [email protected] View Post
            I guess the problem is that with trace on each line of my code generates hundreds or thousands of lines of trace output as each of the Stata commands I call executes. This is true even at a tracedepth of one.
            There is another way.

            On the do file, don't use -set trace- at all. Then, on the command line, type -set trace on-, -set tracedepth 1-, and then -do yourfilename-.

            The first "level" of depth will be spent when calling the do-file, so your trace will only be for the do-file lines and not for lines inside commands called by the do-file.

            Example:

            Code:
            . set trace on
            
            . set tracedepth 1
            
            . do "somedofile"
            
            . sysuse auto, clear
            (1978 Automobile Data)
            
            . 
            . forv i=0/1 {
              2.         qui reghdfe price weight, a(turn)
              3.         if (`i') drop turn
              4.         qui tab turn trunk
              5.         di "done `i'"
              6. }
            - forv i=0/1 {
            - qui reghdfe price weight, a(turn)
            - if (`i') drop turn
            = if (0) drop turn
            - qui tab turn trunk
            - di "done `i'"
            = di "done 0"
            done 0
            - }
            - qui reghdfe price weight, a(turn)
            - if (`i') drop turn
            = if (1) drop turn
            - qui tab turn trunk
            variable turn not found
              di "done `i'"
              }
            r(111);
            
            end of do-file
            
            r(111);
            As you see, the code didn't expand reghdfe (which would be messy) and instead reported the exact line of the problem.

            Comment


            • #7
              Yes, that works for me - thank you. I understand now why I am still a new member, and you are a tenured member, even though we joined in the same month.

              Comment

              Working...
              X