Announcement

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

  • egen xtile

    Hey! I am trying to do the following,

    egen tier=xtile(diff), nq(3) by(id yq)

    yq is the year and quarter.

    It says

    too many values
    r(134);

    There are 1.56 millions of observations and the dataset is 18MB. How to solve it?

    Here are the sample data. Great Thanks!

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(diff id yq)
     .18 7001 216
     .25 7001 216
     .23 7001 216
     .11 7001 216
     .29 7001 216
     .46 7001 216
    -.05 7001 216
    -.04 7001 216
    -.02 7001 216
    -.05 7001 216
    -.15 7001 217
    -.01 7001 218
     .03 7001 218
     .02 7001 218
       0 7001 218
    -.09 7001 218
       0 7001 219
     .02 7001 219
     .03 7001 219
     .01 7001 219
       0 7001 219
     .05 7001 220
     .04 7001 220
     .05 7001 220
     .06 7001 220
     .08 7001 220
     .08 7001 220
    -.03 7001 221
    -.01 7001 221
    -.01 7001 221
    -.05 7001 222
    -.03 7001 222
    -.04 7001 222
    -.02 7001 222
    -.03 7001 222
    -.04 7001 222
    -.01 7001 222
    -.13 7001 223
    -.14 7001 223
    -.14 7001 223
    -.14 7001 223
    -.14 7001 223
    -.13 7001 223
    -.28 7001 223
    -.29 7001 223
    -.13 7001 223
     -.1 7001 223
    -.09 7001 223
       0 7001 224
       0 7001 224
     .03 7001 225
     .04 7001 225
    -.13 2422 215
     .03 2422 215
     .14 2422 215
       0 2422 215
     .14 2422 215
     .47 2422 215
     .01 2422 216
    -.13 2422 216
    -.03 2422 216
     .01 2422 216
    -.04 2422 216
    -.04 2422 216
    -.01 2422 216
     .02 2422 216
     .05 2422 216
    -.05 2422 216
    -.08 2422 216
    -.01 2422 216
    -.01 2422 216
    -.02 2422 216
    -.06 2422 216
    -.01 2422 216
    -.05 2422 216
    -.02 2422 216
    -.05 2422 216
     .08 2422 216
    -.02 2422 216
    -.01 2422 216
    -.01 2422 216
    -.05 2422 216
    -.01 2422 216
       0 2422 216
    -.06 2422 216
    -.04 2422 217
    -.05 2422 217
    -.05 2422 217
     .01 2422 217
     .02 2422 217
    -.01 2422 217
     .03 2422 217
       0 2422 217
    -.04 2422 217
    -.05 2422 217
       0 2422 217
    -.03 2422 217
       0 2422 217
    -.08 2422 217
    -.04 2422 217
    end

  • #2
    Try this one
    Code:
    . egen groups = group(id yq)
    
    . summ groups, meanonly
    
    . gen mahtier = .
    (100 missing values generated)
    
    . forvalues l = 1/`r(max)' {
      2. xtile temptier = diff if groups==`l', nquantiles(3)
      3. replace mahtier = temptier if groups==`l'
      4. drop temptier
      5. }
    (6 real changes made)
    (27 real changes made)
    (15 real changes made)
    (10 real changes made)
    nquantiles() must be less than or equal to number of observations plus one
    r(198);
    It generates an error because in the example data there are even singletons by group, and I am asking Stata to compute 3 quantiles for such data which is impossible.

    Comment


    • #3
      Or better yet replace the loop above with the loop below. (You still might have a problem because some by groups are too small, but at least the loop below will not break at the first occurrence of such a group with too few members.)

      Code:
      . forvalues l = 1/`r(max)' {
        2. cap xtile temptier = diff if groups==`l', nquantiles(3)
        3. cap replace mahtier = temptier if groups==`l'
        4. cap drop temptier
        5. }

      Comment


      • #4
        You can use astile as well. astile is extremely fast, even with larger datasets.
        Code:
        ssc install astile
        bys id yq : astile  tier = diff, nq(3)
        Regards
        --------------------------------------------------
        Attaullah Shah, PhD.
        Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
        FinTechProfessor.com
        https://asdocx.com
        Check out my asdoc program, which sends outputs to MS Word.
        For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

        Comment


        • #5
          Thank you both~ It's solved )

          Comment

          Working...
          X