Announcement

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

  • Frequency table for a continuous variable

    Hello,

    I would like to build a frequency table for a continuous variable. Does anyone know how to do this on Stata? How to build class ranges?

    Thanks

  • #2
    Code:
    help recode

    Comment


    • #3
      The -cut- function in -egen- , with the -group- option, might in some circumstances be satisfactory but easier than -recode-.

      Comment


      • #4
        For a fairly systematic discussion see https://www.stata-journal.com/articl...article=dm0095

        But the nub of this matter seems to me to be simple. The bins that are good are those that would be good for a histogram: they have equal width and no ambiguity about which limit is inclusive.

        Here is some simple technique. But note: bins without values will not be tabulated. If that is really important to you, see tabcount (SSC).


        Code:
        . webuse nlswork
        (National Longitudinal Survey.  Young Women 14-26 years of age in 1968)
        
        . su ttl_exp
        
            Variable |        Obs        Mean    Std. Dev.       Min        Max
        -------------+---------------------------------------------------------
             ttl_exp |     28,534    6.215316    4.652117          0   28.88461
        
        . gen ttl_exp_1 = floor(ttl_exp)
        
        . tab ttl_exp_1
        
          ttl_exp_1 |      Freq.     Percent        Cum.
        ------------+-----------------------------------
                  0 |      2,429        8.51        8.51
                  1 |      3,221       11.29       19.80
                  2 |      3,108       10.89       30.69
                  3 |      2,850        9.99       40.68
                  4 |      2,499        8.76       49.44
                  5 |      2,182        7.65       57.09
                  6 |      1,839        6.44       63.53
                  7 |      1,648        5.78       69.31
                  8 |      1,434        5.03       74.33
                  9 |      1,198        4.20       78.53
                 10 |      1,158        4.06       82.59
                 11 |      1,007        3.53       86.12
                 12 |        902        3.16       89.28
                 13 |        796        2.79       92.07
                 14 |        651        2.28       94.35
                 15 |        524        1.84       96.19
                 16 |        403        1.41       97.60
                 17 |        275        0.96       98.56
                 18 |        187        0.66       99.22
                 19 |        104        0.36       99.58
                 20 |         59        0.21       99.79
                 21 |         29        0.10       99.89
                 22 |          8        0.03       99.92
                 23 |          7        0.02       99.94
                 24 |          5        0.02       99.96
                 25 |          5        0.02       99.98
                 26 |          3        0.01       99.99
                 27 |          2        0.01      100.00
                 28 |          1        0.00      100.00
        ------------+-----------------------------------
              Total |     28,534      100.00
        
        . gen ttl_exp_2 = 2 * floor(ttl_exp/2)
        
        . tab ttl_exp_2
        
          ttl_exp_2 |      Freq.     Percent        Cum.
        ------------+-----------------------------------
                  0 |      5,650       19.80       19.80
                  2 |      5,958       20.88       40.68
                  4 |      4,681       16.40       57.09
                  6 |      3,487       12.22       69.31
                  8 |      2,632        9.22       78.53
                 10 |      2,165        7.59       86.12
                 12 |      1,698        5.95       92.07
                 14 |      1,175        4.12       96.19
                 16 |        678        2.38       98.56
                 18 |        291        1.02       99.58
                 20 |         88        0.31       99.89
                 22 |         15        0.05       99.94
                 24 |         10        0.04       99.98
                 26 |          5        0.02      100.00
                 28 |          1        0.00      100.00
        ------------+-----------------------------------
              Total |     28,534      100.00

        Comment

        Working...
        X