Announcement

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

  • forvalues loop problem

    Dear stata users,

    I have the following code

    Code:
    gen mvq=.
    egen yearmonth = group(year month)
    sum yearmonth
    scalar maxy =r(max)
    local k=maxy
    egen nobs3 =count(mv), by(yearmonth)
    
    forvalues i=1(1) `k' {
        qui xtile xmv=mv if yearmonth==`i' , nq(5) 
        qui replace mvq=xmv if yearmonth==`i'
        qui drop xmv
    }

    where "mv" is the variable for which I create 5 quantile categories and k=433 (the "yearmonth" variable which I loop over has 433 observations).
    I face a problem. There are missing values for some i's (each yearmonth iteration). For instance, `i'=50 and `i'=51 are missing so whenever the program reaches the 49th execution I get this message:
    "nquantiles() must be less than or equal to number of observations plus one",
    which clearly refers to the number of quantiles because I think since there is no yearmonth, there are also no quantiles in the 50th execution.
    so..
    1. Is there a way to skip the missing values in the loop so that it continues to let's say the 52nd execution?
    2. Is there a way to take into account the fact that there might be less than 6 observations so as to skip the xtile command and leave mvq=. ?

    I tried
    Code:
    qui xtile xmv=mv if yearmonth==`i' & nobs3 >=6, nq(5)
    but I it doesn't work.

    Thank you,
    Stylia

  • #2
    This can be slimmed down. Getting r(max), putting it in a scalar and then putting that in a local and then taking it out again is round-about.

    The more crucial problem is trapping the instances of missings.

    Code:
    gen mvq=.
    egen yearmonth = group(year month)
    sum yearmonth, meanonly  
    qui forvalues i=1/`r(max)' {    
         capture xtile xmv=mv if yearmonth==`i' , nq(5)      
         if _rc == 0 {          
               replace mvq=xmv if yearmonth==`i'          
               drop xmv    
         }
    }
    That said, if you have separate variables for year and month, you're well advised to use ym() to make a Stata monthly date out of them. But that requires the year variable to hold numeric values like 2017 and the month variable to hold 1 to 12.

    And the loop isn't needed, any way, as xtile() from egenmore will do this for you.

    So, my guess is that this reduces further to

    Code:
    * once only
    ssc inst egenmore
    
    gen mdate = ym(year month)
    format mdate %tm
    
    egen mvq = xtile(mv), by(mdate)  nq(5)

    Comment


    • #3
      both work perfectly! thank you very much for your help!

      Comment

      Working...
      X