Announcement

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

  • Generate a table convert daily observation to monthly data

    Dear Statalist,
    I have a trouble to generate a table count the number and proportion of people who remained abstinent at 1 to 12 months of life, given days of abstinence from smoking. i.e. 10 people remain abstinent for 30 days, 30 people remain abstinent for 365 days
    month Remain abstinent
    2 10
    12 30
    Code:
    gen monthly_resumed= Day_abs/30
    
    . table monthly_resumed, contents(count Day_abs proportion Day_abs)
    I got this:
    Code:
    proportion invalid
    And I tried without computing proportion, I got this:
    Code:
    ----------------------
    monthly_r |
    esumed    | N(Day_abs)
    ----------+-----------
            0 |         13
     .0333333 |         15
     .0666667 |         20
           .1 |         19
     .1333333 |         10
     .1666667 |          6
           .2 |          6
     .2333333 |         10
     .2666667 |          2
           .3 |          2
     .3333333 |          2
     .3666667 |          3
           .4 |          3
     .4333333 |          4
     .4666667 |         11
           .5 |          5
     .5333334 |          4
     .5666667 |          1
     .6666667 |          3
           .7 |          6
     .8333333 |          2
     .8666667 |          1
     .9666666 |          1
            1 |          3
     1.066667 |          1
          1.1 |          2
     1.166667 |          2
          1.2 |          1
     1.366667 |          1
          1.4 |          1
          1.5 |          3
     1.566667 |          3
          1.8 |          1
     1.833333 |          1
            2 |          2
     2.066667 |          1
          2.1 |          1
    Any help would be appreciated!

  • #2
    Welcome to Statalist.

    As you can see, dividing days by 30 gives you fractions of months. So you need to do something about fractional months.

    If you want 1-30 days to be 1 month, 31-60 days to be 2 months, etc., then
    Code:
    ggenerate monthly_resumed = ceil(Day_abs/30)
    If you want 0-29 days to be 0 months, 30-59 days to be 1 month, etc., then
    Code:
    generate monthly_resumed = floor(Day_abs/30)
    It is not clear to me, however, what numbers you want in your table. I think, however, that what you want is
    Code:
    tabulate monthly_resumed
    Here is sample code run on data that, I think, re-created the 172 observations that went into the tabulation you showed.
    Code:
    . generate monthly_resumed = floor(Day_abs/30)
    
    . 
    . tab monthly_resumed
    
    monthly_res |
           umed |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              0 |        149       86.63       86.63
              1 |         19       11.05       97.67
              2 |          4        2.33      100.00
    ------------+-----------------------------------
          Total |        172      100.00
    
    . tab Day_abs monthly_resumed
    
               |         monthly_resumed
       Day_abs |         0          1          2 |     Total
    -----------+---------------------------------+----------
             0 |        13          0          0 |        13 
             1 |        15          0          0 |        15 
             2 |        20          0          0 |        20 
             3 |        19          0          0 |        19 
             4 |        10          0          0 |        10 
             5 |         6          0          0 |         6 
             6 |         6          0          0 |         6 
             7 |        10          0          0 |        10 
             8 |         2          0          0 |         2 
             9 |         2          0          0 |         2 
            10 |         2          0          0 |         2 
            11 |         3          0          0 |         3 
            12 |         3          0          0 |         3 
            13 |         4          0          0 |         4 
            14 |        11          0          0 |        11 
            15 |         5          0          0 |         5 
            16 |         4          0          0 |         4 
            17 |         1          0          0 |         1 
            20 |         3          0          0 |         3 
            21 |         6          0          0 |         6 
            25 |         2          0          0 |         2 
            26 |         1          0          0 |         1 
            29 |         1          0          0 |         1 
            30 |         0          3          0 |         3 
            32 |         0          1          0 |         1 
            33 |         0          2          0 |         2 
            35 |         0          2          0 |         2 
            36 |         0          1          0 |         1 
            41 |         0          1          0 |         1 
            42 |         0          1          0 |         1 
            45 |         0          3          0 |         3 
            47 |         0          3          0 |         3 
            54 |         0          1          0 |         1 
            55 |         0          1          0 |         1 
            60 |         0          0          2 |         2 
            62 |         0          0          1 |         1 
            63 |         0          0          1 |         1 
    -----------+---------------------------------+----------
         Total |       149         19          4 |       172

    Comment

    Working...
    X