Announcement

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

  • Way to identify first successful loop iteration?

    Hi Stata,

    I have a loop:

    forv x = 1/100{
    cap{
    [stata_commands]
    [if first successful iteration, execute command x]

    }
    }

    How can I run a command only during the first successful iteration of the loop? Any ideas?

    Thanks in advance.

    -Reese

    p.s. I'm using v 14.2

  • #2
    Sorry to ask, since I ended up figuring it out on my own.

    I'm sure there's a more elegant way, but if anyone's curious, I created a dataset before the loop, which I renamed during the first successful iteration. Then used the existence of the original dataset as condition moving forward.

    save shell.dta, replace

    forv x = 1/100{
    cap{

    [stata_commands]

    cap{
    use shell.dta, clear
    save shell2.dta, replace
    erase shell.dta
    }

    if _rc==601............

    }
    }

    Comment


    • #3
      Perhaps this slightly revised version of your code would would do what you want.
      Code:
      local done no
      
      forv x = 1/100{    
        capture {
          [stata_commands - if any fail the capture block will be exited and the loop incremented] 
          if "`done'"=="no" {
            [one-time stata command]
            local done yes
          }
        }
      }

      Comment


      • #4
        This is all very abstract, and most probably I am not following exactly what is the idea here, however you might look at -help continue-.

        I have found the -continue- construct useful for breaking out of loops.

        Comment


        • #5
          Like others I am in the dark about concrete examples. There are certainly ways of breaking out of loops, but it can be as or more direct to use while -- and looping at most so many times can be part of the condition.

          Code:
          local searching = 1 
          local counter = 1 
          
          while `searching' & `counter' <= 100 { 
              * commands go here 
              * if successful, set -local searching = 0-
              local ++counter 
          } 
          
          if `searching' { 
              * code for failure 
          }
          else { 
              * code for success
          }

          Comment


          • #6
            Let me add to the discussion that, like Nick, I am at a loss as to what is required here and prepared my post #3 by simplifying the code in post #2 to accomplish the same task without creating a dataset that does not seem to serve a purpose.

            For the record, my understanding in post #3 is that the block of code within the loop is to be executed precisely 100 times, possibly failing with a nonzero return code on any given iteration. The objective is that on the first - and only the first - successful run of the block, there is a single Stata command to be run - perhaps something like
            Code:
            display `"Hey! I finally was successful on iteration `x' - now continuing on to iteration 100."'
            Clearly this differs from Nick's understanding, which is more sensible, that actual objective is that the loop need not be run after the first success.

            Comment


            • #7
              First of all, I take responsibility that I didn't communicate my problem clearly. William, your interpretation was correct, however. There is a command I want to execute only once--during the first time the capture block succeeds. That said, I like your simpler solution a lot, and really appreciate the help.

              Nick and Joro, even though I confused you, your tips will surely help me in the future as well. I'll benefit a lot adding -continue- and -while- to my repertoire.

              Comment

              Working...
              X