Announcement

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

  • Calculating the difference between times (not dates) for data read as a string

    Hello,

    I am wanting to calculate the time in minutes between pairs of selected variables. The first pair of variables (starttime endtime) were read in as strings. I tried to destring these variables but received an error stating they contained non-numeric characters:

    . destring starttime, gen (start)
    starttime contains nonnumeric characters; no generate


    For some reason, the other set of variables I entered were accepted as numeric characters. When I deducted Break12 - Break11 I obtained two values: 5400000 & 7260000

    My two questions are:

    1) what is required to substract the difference between starttime and endtime
    2) what is the format of the output obtained by deducting Break12 - Break11? Looking at the raw data it should be 90 minutes: 20:12 - 18:42 (which is how the data was entered)

    Any assistance is appreciated.

    Bob

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int id str5(starttime endtime) double(Break11 Break12 Break21 Break22 Break31 Break32)
     3 "11:52" " 3:15"              .              .              .                   .              .              .
     4 " 5:13" "10:37" -1893388680000 -1893383280000 -1893378060000 -1893376979999.9998              .              .
     5 " 8:38" " 8:57"              .              .              .                   .              .              .
     7 " "     "12:10"              .              .              .                   .              .              .
    14 " 2:46" " 8:31"              .              .              .                   .              .              .
    15 " 1:45" "12:16" -1893449280000 -1893442020000 -1.8934347e+12 -1893433380000.0002 -1.8934293e+12 -1893412980000
    end
    format %tchh:MM Break11
    format %tchh:MM Break12
    format %tchh:MM Break21
    format %tchh:MM Break22
    format %tchh:MM Break31
    format %tchh:MM Break32

  • #2
    You should almost never want to destring dates or times. The only likely exception might be string years such as "2015" "2016". If you destring "23:00" and "22:59" (which is possible by ignoring the colons) then arithmetic will give you a nonsense difference of 2300 - 2259 = 41, not 1.

    But you have a good answers, like 5400000.

    You just need to see why you get those. As you're telling us you have date-times with display formats %tc and the units are milliseconds.

    If you want to use results in minutes you just divide by 60000.

    All documented e.g. at help datetime

    Comment


    • #3
      Nick,

      Many thanks for clarifying how to calculate minutes for the variables with the double format. I just saw another post about using format %tc. I'll see if this works on the str5 variables (starttime endtime) .
      Last edited by Bob Green; 11 Jun 2017, 14:45.

      Comment


      • #4
        For the conversion from string to a time variable it looks like what I needed is: gen double starttime2 = clock(starttime, "hm")

        Comment

        Working...
        X