Announcement

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

  • continue, break in a while loop

    I'm working with house price indices, so I have a big loop with other loops inside. This is because I have information from online listings for each city in a particular month of a particular year.
    In the following code, i stands for city, m stands for month and y stands for year. The loop is much longer, but in this first part I calculate the amount of observations in the treated group and in the control group. I'm trying to omit the months where I have less than 50 observations either in the treated or in the control group and for that I'm using "continue, break". The problem is that these lines continue the loop in the next year, not in the next month. For example, if I didn't have enough observations in city 1, may, 2018, instead of jumping to city 1, june 2018, it jumps to city 1, january 2019. So whenever I have a month that "breaks" the loop, it breaks it to the next year and I miss all of the other months that come next.
    Does anybody have a better idea of how to solve this issue?
    The example of the code that I'm using is the following:
    (preciouf is the price variable, and NUMB_T stands for number treated, and NUMB_N for the non treated


    Code:
    local i=1
    while `i'<14{    
        local y=2017
        while `y'<2021{
            local m=1
            while `m'<5{
    
                
                insheet using "${pathData}`i'_`y'_`m'.csv", comma clear
    
                * getting number of control and treated before matching
                tabstat     preciouf, stat(count) save, if treated==1
                matrix         NumbB_T=r(StatTotal)
                tabstat     preciouf, stat(count) save, if treated==0
                matrix         NumbB_NT =r(StatTotal)'
                matrix         NumbB=(NumbB_T, NumbB_NT)
                mat         rown NumbB = "`y' `m'"
                mat         coln NumbB = Before
                local         c=NumbB_T[1,1]
                local         z=NumbB_NT[1,1]
                mat         rown NumbB_T = "i_`y'_`m'"
                mat         rown NumbB_NT = "i_`y'_`m'"
                mat2txt , m(NumbB_T) sav("${pathRes}NumbB_T.txt") append
                if (`c'<50) continue, break
                if (`z'<50) continue, break
                
                *** everything else
                
                
                local m=`m'+1
            }
            local y=`y'+1
        }
        local i=`i'+1
    }

  • #2
    Loops aren't the solution to your problem. I realize you invested a lit of time and effort in that, but I am afraid you will end up throwing it all away. Instead this sounds like a problem for the by prefix in combination with _N. We can give you more detailed advice when you give us an example of your data. See the statalist FAQ for how to do that.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Maarten is likely right that this is better done with -by:- or, depending on what "everything else" is, with -runby- (from SSC). Nevertheless, if you want to work with your existing code, you can overcome your current difficulty by conditioning "everything else" on there being adequate numbers of T and NT, rather than conditioning the break on there not being such. So:

      Code:
      if `c' >= 50 & `z' >= 50 {
          // everything else
      }
      local m = `m' + 1

      Comment


      • #4
        Seems to me that if you just delete the "break", and leave only the continue, it should work.
        continue skips the rest of the given round of the loop, and jumps to the next round of the same loop.
        continue, break breaks out of the whole loop, and proceeds to what is next after the loop. In your case what is next is the outer loop over years.

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          Maarten is likely right that this is better done with -by:- or, depending on what "everything else" is, with -runby- (from SSC). Nevertheless, if you want to work with your existing code, you can overcome your current difficulty by conditioning "everything else" on there being adequate numbers of T and NT, rather than conditioning the break on there not being such. So:

          Code:
          if `c' >= 50 & `z' >= 50 {
          // everything else
          }
          local m = `m' + 1
          This worked!!! Thank you very much Clyde Schechter

          Joro Kolev I tried what you suggested before, but the continue function starts again in the begginning of the loop, so in my case, without the break it kept processing over and over again that month that didn't have more than 50 observations (from the begginning until the continue)

          I couldn't share the data because it was confidential. And where I said "everything else" is just a bunch of matching and regressions I have to create the indices. But Clyde solution worked just fine! Thank you so much!!!

          Comment

          Working...
          X