Announcement

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

  • Error "break" without pressing break

    Why does the following syntax stop with --Break-- r(1) although I don't press Break:
    Code:
    clear 
    cap frame drop original 
    frame create original 
    frame change original 
    clear 
    input int(vp fp fn vn) 
     41  51 43 276 
     31  60 49 269 
     35  70  9 187 
     30  71 30 179 
     10  15 21 236 
    185  27 63  75 
     29  53 53 683 
     27  43  4  99 
    213 881 32 600 
     32  54 62 581 
    end 
    scalar nstudies = _N 
     
    frame change default 
    forvalues i = 1/`=scalar(nstudies)' { 
       frame original: scalar vp = vp[`i'] 
       frame original: scalar fp = fp[`i'] 
       frame original: scalar fn = fn[`i'] 
       frame original: scalar vn = vn[`i'] 
     
       clear 
       input x1 x2 freq 
          1 1 . 
          1 0 . 
          0 1 . 
          0 0 . 
       end 
       replace freq = scalar(vp) if _n==1 
       replace freq = scalar(fp) if _n==2 
       replace freq = scalar(fn) if _n==3 
       replace freq = scalar(vn) if _n==4 
        
       tab2 x1 x2 [fw=freq] 
    }
    Here is a part of the result:
    Code:
    . forvalues i = 1/`=scalar(nstudies)' {
      2.    frame original: scalar vp = vp[`i']
      3.    frame original: scalar fp = fp[`i']
      4.    frame original: scalar fn = fn[`i']
      5.    frame original: scalar vn = vn[`i']
      6. 
    .    clear
      7.    input x1 x2 freq
      8.       1 1 .
      9.       1 0 .
     10.       0 1 .
     11.       0 0 .
     12.    end
    --Break--
    r(1);
    If I move the part starting with -input- outside of -foreach- the syntax runs without break (but then I can't loop through the data of the studies in the frame "original"):
    Code:
    frame change default 
    forvalues i = 1/`=scalar(nstudies)' { 
       frame original: scalar vp = vp[`i'] 
       frame original: scalar fp = fp[`i'] 
       frame original: scalar fn = fn[`i'] 
       frame original: scalar vn = vn[`i'] 
     
       clear 
    }    
       input x1 x2 freq 
          1 1 . 
          1 0 . 
          0 1 . 
          0 0 . 
       end 
       replace freq = scalar(vp) if _n==1 
       replace freq = scalar(fp) if _n==2 
       replace freq = scalar(fn) if _n==3 
       replace freq = scalar(vn) if _n==4 
        
       tab2 x1 x2 [fw=freq]




  • #2
    If you place end inside a loop, Stata will interpret it as the end of the enclosing block (if one exists) rather than as part of the loop. This can make it appear that the loop is being "broken" because Stata stops parsing the current block. As you are repeatedly loading the same dataset across iterations, no need to create the dataset using input within the loop. My approach below uses a tempfile (temporary file).

    Code:
    clear
    cap frame drop original
    frame create original
    frame change original
    clear
    input int(vp fp fn vn)
     41  51 43 276
     31  60 49 269
     35  70  9 187
     30  71 30 179
     10  15 21 236
    185  27 63  75
     29  53 53 683
     27  43  4  99
    213 881 32 600
     32  54 62 581
    end
    scalar nstudies = _N
    
    
    frame change default
    clear
       input x1 x2 freq
          1 1 .
          1 0 .
          0 1 .
          0 0 .
       end 
    tempfile tempdata
    save `tempdata'
    forvalues i = 1/`=scalar(nstudies)' {
       frame original: scalar vp = vp[`i']
       frame original: scalar fp = fp[`i']
       frame original: scalar fn = fn[`i']
       frame original: scalar vn = vn[`i']
       clear
       append using `tempdata'
       replace freq = scalar(vp) if _n==1
       replace freq = scalar(fp) if _n==2
       replace freq = scalar(fn) if _n==3
       replace freq = scalar(vn) if _n==4
        
       tab2 x1 x2 [fw=freq]
    }

    Comment


    • #3
      Excellent solution -- thanks!

      In between I tried to use a local macro named "end_input" containing "end" to end -input- (as it is possible to use a local macro named "end_mata" containing "end" to end -mata-), but that failed with the error message unexpected end of file r(612). Your solution is perfect.

      Comment

      Working...
      X