Announcement

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

  • Behavior of Stata's -exit- command, v16-v19

    Dear All,

    I have encountered an unexpected difference in behavior of Stata's -exit- command.

    Here is an illustration:

    Code:
    capture program drop foobar16
    capture program drop foobar19
    
    program define foobar16
        version 16.0
        forvalues i=1/10 {
            display `"`i'"'
            if (`i'>=5) {
                exit
            }
        }
    end
    
    program define foobar19
        version 19.0
        forvalues i=1/10 {
            display `"`i'"'
            if (`i'>=5) {
                exit
            }
        }
    end
    
    foobar16
    foobar19
    
    // END OF FILE
    The output of foobar16 is 10 numbers (1,2,...10), while the output of foobar19 is 5 numbers (1,2,...5).

    I don't see how the version mentioned for compatibility reasons radically changes the behavior of the loop. (And at the same time I have a dejavu that I may have complained about the wrong behavior earlier already).
    A quick conversation with several AIs came out negative - they don't recollect changes in exit behavior between versions 16 and 19, though insist that the only thing that can be causing the difference is the version specification, and thus this difference is due to Stata's changes under the hood.

    Observed in: StataNow/MP 19.5 for Windows (64-bit x86-64), Revision 14 Jan 2026

    What am I missing? Or is it a bug?
    If it is a bug, which versions are affected? (in my trials setting the version to 17 and 18 behaves same as 19, 15 behaves same as 16).

    Thank you, Sergiy

  • #2
    From
    Code:
    help whatsnew16to17
    1.3.15 What's new in programming
    [...]

    5. Existing commands exit and class exit within a forvalues or foreach loop now terminate the loop and, if they are in a program, the program is also terminated.
    Further, class exit will now return any values specified for return.

    Previously, exit and class exit would abort the current iteration of the loop and then proceed with the next iteration of the loop. Iterations of the loop would
    continue until the loop completed or was otherwise terminated. In addition, class exit did not return any values specified for return when encountered within a
    forvalues or foreach loop.

    The previous behavior is preserved under version control.

    Comment


    • #3
      Code:
      help whatsnew16to17
      1.3.15 What's new in programming


      5. Existing commands exit and class exit within a forvalues or foreach loop
      now terminate the loop and, if they are in a program, the program is
      also terminated. Further, class exit will now return any values
      specified for return.

      Previously, exit and class exit would abort the current iteration of the
      loop and then proceed with the next iteration of the loop. Iterations
      of the loop would continue until the loop completed or was otherwise
      terminated. In addition, class exit did not return any values specified
      for return when encountered within a forvalues or foreach loop.

      The previous behavior is preserved under version control.
      Edit: Daniel was quicker.
      https://www.kripfganz.de/stata/

      Comment


      • #4
        Thank you very much Daniel and Sebastian!

        This totally explains it.

        Comment

        Working...
        X