Announcement

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

  • egen(cut)

    Hi there

    I want to group a continuous variable (var). My code syntax is as follows:

    egen vargrp = cut(var), at(10,20,30)

    However, I want to cut into groups of 10 way beyond 30 (up to about a thousand). Is there any quick way I can do this without having to list 10,20,30,40,50,60.... etc?

    Thanks


  • #2
    This is quite often asked here. Personally I dislike that function as you have to look each time at the code to see exactly what happens at boundaries (the documentation doesn't seem lucid to me) and its conventions for values beyond specified limits are puzzling.

    But there are wonderful solutions for what you want. You just need your own generalisations of floor() which rounds down and ceil() which rounds up.

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . gen length2 = 10 * ceil(length/10)
    
    . gen length3 = 10 * floor(length/10)
    
    . tabdisp length if length2 != length3, c(length?)
    
    ----------------------------------
    Length    |
    (in.)     |    length2     length3
    ----------+-----------------------
          142 |        150         140
          147 |        150         140
          149 |        150         140
          154 |        160         150
          155 |        160         150
          156 |        160         150
          157 |        160         150
          161 |        170         160
          163 |        170         160
          164 |        170         160
          165 |        170         160
          168 |        170         160
          169 |        170         160
          172 |        180         170
          173 |        180         170
          174 |        180         170
          175 |        180         170
          177 |        180         170
          179 |        180         170
          182 |        190         180
          184 |        190         180
          186 |        190         180
          189 |        190         180
          192 |        200         190
          193 |        200         190
          195 |        200         190
          196 |        200         190
          197 |        200         190
          198 |        200         190
          199 |        200         190
          201 |        210         200
          203 |        210         200
          204 |        210         200
          206 |        210         200
          207 |        210         200
          212 |        220         210
          214 |        220         210
          217 |        220         210
          218 |        220         210
          221 |        230         220
          222 |        230         220
          233 |        240         230
    ----------------------------------
    In short both solutions round, but one returns lower class limits and the other returns upper class limits.

    See also http://www.stata-journal.com/sjpdf.h...iclenum=dm0002

    I note without approving that many people like int() and round().

    Comment


    • #3
      perfect - thank you

      Comment

      Working...
      X