Announcement

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

  • Call arguments from forvalues command

    Hello-

    I'm trying to write a simple program to display a status bar during a forvalues loop. Right now, I'm manually entering the minimum and maximum of the range stated in the forvalues command, but it's not a very useful command if you have to re-type the min/max values every time. I'd like to automatically create local variables for the #1 and #2 arguments in the forvalues command. The following code creates what I'm looking for in a .do file:

    Code:
    forvalues i = 1/50 {
    
         * Display status of loop replications
            local pct = abs((`i'/50)*100)
            if `i' == 1 dis "Loop running: 0%" _continue
            if mod(`pct',10) == 0 & `i' != 50 dis "...`pct'%" _continue
            if `i' == 50 dis "...100%. DONE." _newline(1)
            
        sleep 250
    }
    The sleep command is just for testing purposes. I'd like to replace the 1 and the 50 with local variables captured from the forvalues command. If I could do that, it would give me a nice one-word program to create a status bar!

    Thanks!
    -Marc

  • #2
    I do not believe that the values you want are accessible to the user within the loop. Could you give an example where that would be useful? I would guess that you usually know the start and end values of the loops you are running.

    Best
    Daniel

    Comment


    • #3
      A statusbar in Stata is typically displayed using: https://www.stata-journal.com/articl...article=pr0030
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment

      Working...
      X