Announcement

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

  • Difference between two dates in days

    Hi all,

    I have a date of admission (ex. 02mar2018 00:00:00) and date of intervention (ex. 05mar2018 00:00:00) variables in DMYhm long format:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(dt_adm dt)
     1.835568e+12 1.8358272e+12
    1785974460000 1785974520000
    1767570840000 1.7678736e+12
    1827425160000 1827662940000
    1797352980000  1.797408e+12
    1.8025632e+12  1.803168e+12
    1807650360000 1.8080457e+12
    1840039080000 1.8400929e+12
    1.8462816e+12  1.846368e+12
    1.7840133e+12 1.7840232e+12
    1838927460000 1.8389844e+12
    1792784280000 1.7936991e+12
    1854378720000  1.854789e+12
    1.8186336e+12 1.8186336e+12
    1833068460000 1.8334404e+12
    1.7916576e+12  1.791711e+12
    1856194740000 1.8562563e+12
    1.7901468e+12 1.7901507e+12
    1857663240000 1857728520000
    1833613140000 1.8336201e+12
    1.8280512e+12 1.8280512e+12
    1.8570816e+12 1.8570816e+12
    1819474620000 1819630320000
     1.796472e+12 1.7967336e+12
    1835940060000 1.8359478e+12
    1770856140000 1.7709204e+12
    1811700660000 1.8119322e+12
    1810109940000 1.8101169e+12
    1.8334368e+12 1.8334638e+12
    1.8230103e+12 1.8230184e+12
    end
    format %tc dt_adm
    format %tc dt
    How do I find the difference between two dates in terms of days (an integer)?

  • #2
    Lana, try

    Code:
    gen wanted = dofc(dt) - dofc(dt_adm)

    Comment


    • #3
      Thank you! That worked perfectly!

      Comment


      • #4
        There is another approach to this:
        Code:
        gen wanted = clockdiff(dt_adm, dt, "d")
        These two approaches calculate slightly different things, however. Look, for example, at the third observation in your example data, where dt_adm is 04jan2016 23:54:00 and dt is 08jan2016 12:00:00. The solution in #2 considers just the dates, disregarding the times, so it subtracts 04jan2016 from 08jan2016, and gets a difference of 4 days. However, the approach shown here notices that the time on 08jan2016 is earlier than the time on 04jan2016, so that four days have not actually elapsed, and this method reports a 3 day difference.

        Which approach is appropriate for your purposes depends on how you are using the results. For example, if you are using this data for administrative purposes, where the calendar date is highly salient, then the approach in #2 is better. If you are using this data for biomedical purposes, where actual elapsed time matters more than calendar markings, then the approach here is better.

        Added: Actually, if you are using this for biomedical purposes, you might be better off still with the clockdiff_frac() function, which does not truncate the result to an integer.
        Last edited by Clyde Schechter; 21 Jul 2022, 09:35.

        Comment


        • #5
          Also, understanding that Stata time variables are in milliseconds, you can calculate how many milliseconds there are in a day and divide the difference by this. 1000 milliseconds in a second, 60 seconds in a minute, 60 minutes in an hour and 24 hours in a day.

          Code:
          gen wanted= (dt-dt_adm)/(1000*60*60*24)
          This will give you the number of days including the fractional part.

          Comment


          • #6
            Also the (1000*60*60*24) part can be abbreviated by the somewhat more transparent msofhours(24).

            Comment

            Working...
            X