Announcement

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

  • How to calculate days and hours between two dates

    Hi,

    I am calculating days and hours between two dates (admission date/time and discharge date/time), thanks for any suggestions.

    Example attached:

    clear

    input str9 admi_date str9 admi_time str9 dischage_date str9 dischage_time
    1 11jan2019 1154 12jan2019 0716
    2 15feb2019 0217 08oct2018 0934
    3 01dec2019 2314 09feb2020 0817
    end

    The final results should have two new variables, one denotes the total days between two dates, and another one indicates the total hours between two dates.

    Best,
    Zichun

  • #2
    This looks like dataex output, but won't run correctly without surgery. I've ignored the typo "dischage".

    Stata has no special support for hours. I've calculated the difference in ms and then in hours. Note that #2 was discharged some months before admission.


    Code:
    clear
    
    input id str9 admi_date str9 admi_time str9 disc_date str9 disc_time
    1 11jan2019 1154 12jan2019 0716
    2 15feb2019 0217 08oct2018 0934
    3 01dec2019 2314 09feb2020 0817
    end
    
    gen double wanted_ms = clock(disc_date + disc_time, "DMY hm") - clock(admi_date + admi_time, "DMY hm") 
    
    gen wanted_hr = wanted_ms / (60 * 60 * 1000)
    
    list 
    
    
        +---------------------------------------------------------------------------+
         | id   admi_date   admi_t~e   disc_date   disc_t~e    wanted_ms   wanted_hr |
         |---------------------------------------------------------------------------|
      1. |  1   11jan2019       1154   12jan2019       0716     69720000    19.36667 |
      2. |  2   15feb2019       0217   08oct2018       0934   -1.121e+10   -3112.717 |
      3. |  3   01dec2019       2314   09feb2020       0817    5.994e+09     1665.05 |
         +---------------------------------------------------------------------------+

    Comment


    • #3
      Thank you so much! Sorry for the typo.

      Comment

      Working...
      X