Announcement

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

  • How to create a progress bar for loops

    Hi there,

    I have been looking online for ways to create a progress bar that shows the completion of a loop. I found one way to do it (https://acarril.github.io/posts/progess-bar) but the "progress" makes vertical dots, instead of simply horizontal dots.

    Is there a simple way to do it in Stata? Ideally it would allow you to specify a heading (e.g., running n loops) and then provide a series of horizontal dots (or any another symbol) to indicate the completion of each loop.

    Any advice is much appreciated!

  • #2
    No, the progress bar suggested in the link you provided shows the dots horizontally (and by necessity it will continue on a new line after a certain number of dots in a row).

    Comment


    • #3
      Thanks for your response.

      Trying this simple example, the code within the loop is between the "heading" (i.e., "Loop running") and the dots...

      Code:
      _dots 0, title(Loop running) reps(10)
      forvalues i = 1/10 {
       
        sleep 1000
       
        _dots `i' 0
      }
      Any reason why this is happening?

      Comment


      • #4
        I don't know why this is happening -- the "Stata tip 41" by David Harrison (see https://journals.sagepub.com/doi/pdf...867X0700700111) is using virtually the same commands you are using. By the way: He also explains that -_dots- is an undocumented Stata command.

        However, the following "work around" will do what you want:
        Code:
        local reps 70
        forvalues i = 1/`reps' {
          sleep 100
          if `i'==1 _dots 0, title(Loop running) reps(`reps')
          _dots `i' 0
        }

        Comment


        • #5
          Wow. Not only is _dots undocumented, it's so undocumented that
          Code:
          help undocumented
          does not include it.

          The designed use case for _dots is apparently to be used within a program.
          Code:
          . capture program drop gnxl
          
          . program define gnxl
            1. local reps 70
            2. _dots 0, title(Loop running) reps(`reps')
            3. forvalues i = 1/`reps' {
            4.   sleep 10
            5.   _dots `i' 0
            6. }
            7. display "Done!"
            8. end
          
          . gnxl
          Loop running (70)
          ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5 
          ..................................................    50
          ....................Done!
          
          .

          Comment


          • #6
            Ah! I see. Many thanks, Dirk and William.

            Would be good if Stata invested in a documented way to do progress bar. I'll put it in the WIshlist for Stata 18

            Comment


            • #7
              Though it is not a general solution to O.P.'s question, I will just point out that if the loop is a loop over the values of one or more variables, the loop can be replaced with the -runby- command. -runby- has a -status- option which gives a periodic progress report showing how many -by- groups have been processed and the estimated time remaining. It's not the same as a progress bar, but serves the same purpose.

              -runby- was written by Robert Picard and me, and is available from SSC.

              Comment


              • #8
                Thanks, Clyde! I will also look into this

                Comment

                Working...
                X