Announcement

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

  • How to skip the r(2000) error and continue with code in Stata

    Dear all,

    My search on this topic has not been satisfactory:


    https://www.statalist.org/forums/for...stfile-command
    https://www.stata.com/statalist/arch.../msg01072.html
    https://www.stata.com/statalist/arch.../msg00382.html


    So, my question is: How can I successfully run the code below and skip the instances in which Stata returns r(2000)? E.g., all observations failed (event=1 for all items) or did not fail (event=0 for all items). In my do file, that simple simulation stops at step 2.


    Code:
    *! step 1
    clear
    set obs 100
    gene trt = runiform()<0.5
    gene event = runiform()<0.2
    logit event trt
    
    *! step 2
    cap drop trt event
    set obs 100
    gene trt = runiform()<0.5
    gene event = runiform()<0.001
    logit event trt
    
    *! step 3
    cap drop trt event
    set obs 100
    gene trt = runiform()<0.5
    gene event = runiform()<0.999
    logit event trt
    
    *! step 4
    cap drop trt event
    set obs 100
    gene trt = runiform()<0.5
    gene event = runiform()<0.2
    logit event trt
    All the best,

    Tiago

  • #2
    Code:
    help capture
    Same way as you have

    Code:
    cap drop
    to suppress the error message. You can first check whether the logit runs and then rerun it if it is successful.

    Code:
    cap logit event trt
    if _rc==0 {
          logit event trt
    }

    Comment


    • #3
      The following should also work, and has the advantages that logit is only run once each time, and that the output will show whether or not the r(2000) error was encountered:

      Code:
      capture noisily logit event trt

      Comment


      • #4
        Dear Andrew and David,

        As always, thank you so much for your help and tips. Always extremely helpful.

        All the best,

        Tiago

        Comment

        Working...
        X