Announcement

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

  • Calculating difference between two times in minutes

    I have two variables - bed time and wake time. bed time is formulated like: 21_30 and wake time is formulated like 06_30.

    Can you advise how I can calculate the duration between those times in stata in minutes? i.e in that example the calculation should be 540 minutes.

  • #2
    Welcome to Statalist. Please take the time to read through the FAQs, especially on how to present data examples using the dataex command. Stata has excellent capabilities that will enable you to handle date and time variables, you can take a look at

    Code:
    help datetime
    The following assumes that you have a string variable and that bed time is either on the day before wake time (before midnight) or on the same day as wake time (after midnight). Otherwise, you need a date variable. Run the code in a do file as it contains multiple line breaks.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str7(bed_time wake_time)
    "21_30" "06_30"
    "01_30" "10_00"
    end
    
    gen time_diff= cond(real(substr(bed_time, 1, 2)) >real(substr(wake_time, 1, 2)), ///
    ((24-real(substr(bed_time, 1, 2)))*60) -(real(substr(bed_time, 4, 2))) ///
    + (real(substr(wake_time, 1, 2))*60) +real(substr(wake_time, 4, 2)),  ///
    ((real(substr(wake_time, 1, 2))*60)+real(substr(wake_time, 4, 2)))- ///
    ((real(substr(bed_time, 1, 2))*60)+real(substr(bed_time, 4, 2))))
    or more easily using Stata's datetime tools

    Code:
    gen double sleep = clock(bed_time, "hm")
    gen double wake = clock(wake_time, "hm")
    gen time_diff2= cond(sleep>wake,  (wake+86400000-sleep)/60000, (wake-sleep)/60000)

    Res.:

    Code:
    . l
    
         +--------------------------------+
         | bed_time   wake_t~e   time_d~2 |
         |--------------------------------|
      1. |    21_30      06_30        540 |
      2. |    01_30      10_00        510 |
         +--------------------------------+
    Last edited by Andrew Musau; 24 Sep 2019, 17:21.

    Comment


    • #3
      Thank you Andrew - all sorted

      Comment


      • #4
        Thanks for this. What if the two variables (in your example, sleep and wake) are numeric and not string?
        Dapel

        Comment


        • #5
          If you are using a recent version of Stata (17 or later), and if, by numeric, you mean Stata interanl format clock variables, you can use the -clockdiff()- or -clockdiff_frac()- function. -help clockdiff()-.

          Added: If your Stata is older than that:
          Code:
          gen diff_in_hours = (time2 - time1)/msofhours(1)
          and then you can truncate or round to an integer, or leave it as is.
          Last edited by Clyde Schechter; 22 Jul 2024, 11:32.

          Comment


          • #6
            That worked. Thank you very much!

            Dapel

            Comment

            Working...
            X