Announcement

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

  • Calculating a sum of total cases and total deaths (case fatality ratio)

    Hi Stata Community,

    I am getting stuck on something I feel has a more succinct way of coding than what I am currently doing. I would like to calculate case fatality ratios. My time variables are discharge year and discharge quarter. So far, I've calculated the total # cases and total #deaths by repeating the code below. However, the egen total function scores the result in all of the observations meeting the condition--that's not quite what I want. Is there a way I can store the total number of deaths in just one cell? I would like to generate a case fatality ratio for each unique year/quarter by dividing the cases variable/death variable. Thank you in advance!


    sort discharge_year discharge_qtr
    egen deaths_2016_q1=total(expired) if discharge_year==2016 &discharge_qtr==1
    gen cases_2016_q1=_n if discharge_year==2016 &discharge_qtr==1


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int encounter_id long(discharge_year discharge_qtr) byte expired
     187 2016 1 0
     998 2016 1 0
     128 2016 1 1
      19 2016 1 0
      83 2016 1 0
     586 2016 1 1
     127 2016 1 1
    1170 2016 1 0
     141 2016 1 0
    1236 2016 1 0
     359 2016 1 0
    1537 2016 1 0
    1203 2016 1 0
    1045 2016 1 0
     314 2016 1 0
     866 2016 1 0
     582 2016 1 0
     433 2016 1 0
     981 2016 1 0
    1008 2016 1 1
     730 2016 1 0
     158 2016 1 0
     853 2016 1 0
    1137 2016 1 0
    1069 2016 1 0
    1197 2016 1 0
    1230 2016 1 0
    1173 2016 1 0
    1144 2016 1 1
     400 2016 1 0
    1051 2016 1 1
    1174 2016 1 0
     784 2016 1 0
     825 2016 1 0
     915 2016 1 1
    1181 2016 1 0
     392 2016 1 0
    1585 2016 1 0
    1289 2016 1 0
    1207 2016 1 0
     919 2016 1 0
     113 2016 1 0
     758 2016 1 0
     883 2016 1 1
     928 2016 1 0
     278 2016 1 0
     788 2016 1 1
    1642 2016 1 0
    1504 2016 1 0
    1446 2016 1 0
    1531 2016 1 0
    1740 2016 1 0
     123 2016 1 0
     994 2016 1 0
     467 2016 1 0
      40 2016 1 0
    1350 2016 1 0
      71 2016 1 0
      10 2016 1 0
    1560 2016 1 0
    1141 2016 1 0
     877 2016 1 1
    1416 2016 1 0
     703 2016 1 0
    1284 2016 1 0
    1739 2016 1 0
    1227 2016 1 0
      13 2016 1 0
     639 2016 1 0
     805 2016 1 0
    1211 2016 1 0
     234 2016 1 0
    1534 2016 1 0
    1118 2016 1 0
    1399 2016 1 0
     414 2016 1 0
     504 2016 1 0
     197 2016 1 0
    1021 2016 1 0
     213 2016 1 0
     472 2016 1 0
    1044 2016 1 0
     742 2016 1 0
    1728 2016 1 0
     257 2016 1 0
     117 2016 1 1
    1722 2016 1 0
    1025 2016 1 0
     252 2016 1 0
    1557 2016 1 0
     383 2016 1 0
     707 2016 1 0
    1123 2016 1 0
     203 2016 1 0
     355 2016 2 0
     403 2016 2 0
      55 2016 2 0
    1039 2016 2 0
    1704 2016 2 0
    1409 2016 2 1
    end

  • #2
    How about just aggregating over them?
    Code:
    collapse (count) case = expired (sum) expired = expired, by(discharge_year discharge_qtr)
    gen cf_ratio = expired / case * 100
    Also, regarding
    I would like to generate a case fatality ratio for each unique year/quarter by dividing the cases variable/death variable
    I think it's the other way, death / cases.

    Comment


    • #3
      Ken Chui oh whoops! Yes, definitely meant death/cases.

      Comment


      • #4
        Thank you so much Ken Chui ! That worked beautifully

        Comment


        • #5
          Originally posted by Julia Silver View Post
          Thank you so much Ken Chui ! That worked beautifully
          You are welcome. Glad that it worked out fine.

          Comment

          Working...
          X