Announcement

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

  • Repeat the command until it works in a do-file

    Hello,

    I have a rather special request.
    I have a do-file with a thousand similar commands. I'm using sdmxuse, a module to import data from statistical agencies using the sdmx standard.

    Sometimes I get an error because the statistical agency's server does not respond to my request with the standard sdmx.
    If i redo the command, it works perfectly.

    So I would like to find a way to repeat the command until it works.

    Thank you in advance

  • #2
    I am taking the problem as described without questions. Here is how you run any command until it works, i.e., until it sets the return code to 0.

    Code:
    capture noisily cmd
    while ( _rc ) {
        capture noisily cmd
    }
    Be careful with this code; it will keep trying until there is no error, which might never be the case. A slightly modified, more reasonable approach would be the following program

    Code:
    capture program drop tryit
    program tryit
        version 15
        
        gettoken max   0 : 0 , parse(":")
        gettoken colon 0 : 0 , parse(":")
        assert (`max' > 0)
        assert ("`colon'" == ":")
        
        local try 0
        capture noisily `0'
        while ( _rc & (`++try' < `max') ) {
            sleep 1000
            capture noisily `0'
        }
        
        exit _rc
    end
    You would define this program (just include the code near the top of your do-file), then type something like

    Code:
    tryit 10 : cmd
    The code above would repeatedly execute the command, cmd, every second but would stop after 10 unsuccessful trials.

    Best
    Daniel
    Last edited by daniel klein; 18 Oct 2019, 07:06.

    Comment


    • #3
      Thank you Daniel

      Comment

      Working...
      X