Announcement

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

  • Break loop if it takes too long

    Suppose I am running a foreach loop in Stata, generally like
    Code:
    foreach unit in units { do something }
    For some units, this "something" runs fast, but for others, it is taking too long.

    I would like to tell Stata to ignore steps that are taking too long based on some time cutoff.

    For example, suppose we have four units A, B, C and D to loop over. Unit A runs in one minute, unit B runs in three minutes, unit C takes half an hour and unit D runs in two minutes. But I don't have that information before running.

    I would like to impose a cutoff like 10 minutes for example, so that A and B would run normally and C would be running for 10 minutes and then would break, and the loop would jump to D, that would also run normally.

    Is there a way to do that in Stata?

  • #2
    I'm not aware of any general way to do this in Stata. If somebody else is, I hope they will also respond as I would like to learn, too.

    That said, if the content of the loop whose duration concerns you is one of the many regression models that is fit by maximum likelihood, while you cannot, as far as I know, set a time limit, you can accomplish something along those lines by specifying -iterate(#)- (replacing # by the maximum number of iterations you will allow). If the analysis does not converge within that number of iterations, the command stops. The output of the command will include a message stating that the analysis did not converge, and r(converged) will be set to 0 (so you can take appropriate actions depending on when things converged and when they didn't).

    Comment


    • #3
      Originally posted by Wagner Oliveira View Post
      Suppose I am running a foreach loop in Stata, generally like
      Code:
      foreach unit in units { do something }
      For some units, this "something" runs fast, but for others, it is taking too long.

      I would like to tell Stata to ignore steps that are taking too long based on some time cutoff.

      For example, suppose we have four units A, B, C and D to loop over. Unit A runs in one minute, unit B runs in three minutes, unit C takes half an hour and unit D runs in two minutes. But I don't have that information before running.

      I would like to impose a cutoff like 10 minutes for example, so that A and B would run normally and C would be running for 10 minutes and then would break, and the loop would jump to D, that would also run normally.

      Is there a way to do that in Stata?
      I just read this thread and I have the same problem. Did you find any solution?

      Best regards,

      Jesper Eriksson

      Comment


      • #4
        Possible approach as sketch. May not work for what you want (breaking within a command). Iterate seems a sensible solution.

        Code:
        clear all
        version 18
        
        set obs 100000
        g x = rnormal()
        
        timer clear 1
        timer on 1
        forv i = 1/100 {
            qui timer list 1
            if r(t1)>10 {
                di as error "*** Break Due to Time ***"
                continue, break
            }
            timer on 1
            bootstrap mean = r(mean), reps(100): summ x
            timer off 1
        }
        Last edited by George Ford; 01 Dec 2024, 15:36.

        Comment

        Working...
        X