Announcement

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

  • Elapsed hours

    Hi,

    I am trying to calculate the total number of hours spent on certain activities (no AM and PM codes are provided in the data) with the following data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str5(slot_1_start slot_1_end)
    ""      ""     
    ""      ""     
    ""      ""     
    "05:00" "05:30"
    ""      ""     
    ""      ""     
    ""      ""     
    "05:00" "05:30"
    ""      ""     
    ""      ""     
    ""      ""     
    "06:00" "07:00"
    ""      ""     
    ""      ""     
    ""      ""     
    "06:30" "07:00"
    ""      ""     
    ""      ""     
    ""      ""     
    "06:00" "07:00"
    ""      ""     
    ""      ""     
    ""      ""     
    "05:00" "06:30"
    ""      ""     
    ""      ""     
    ""      ""     
    "06:30" "08:30"
    ""      ""     
    ""      ""     
    ""      ""     
    "06:30" "08:00"
    ""      ""     
    ""      ""     
    ""      ""     
    "05:30" "06:00"
    ""      ""     
    ""      ""     
    ""      ""     
    "06:00" "06:40"
    ""      ""     
    ""      ""     
    ""      ""     
    "06:00" "07:00"
    ""      ""     
    ""      ""     
    ""      ""     
    "06:00" "06:30"
    ""      ""     
    ""      ""     
    ""      ""     
    "06:00" "07:00"
    ""      ""     
    ""      ""     
    ""      ""     
    "07:00" "08:30"
    ""      ""     
    ""      ""     
    ""      ""     
    "06:00" "06:20"
    ""      ""     
    ""      ""     
    ""      ""     
    "05:00" "06:00"
    ""      ""     
    ""      ""     
    ""      ""     
    "05:30" "06:30"
    ""      ""     
    ""      ""     
    ""      ""     
    "05:00" "06:00"
    ""      ""     
    ""      ""     
    ""      ""     
    "06:00" "07:00"
    ""      ""     
    ""      ""     
    ""      ""     
    "06:00" "08:00"
    ""      ""     
    ""      ""     
    ""      ""     
    "06:00" "06:40"
    ""      ""     
    ""      ""     
    ""      ""     
    "06:00" "07:00"
    ""      ""     
    ""      ""     
    ""      ""     
    "05:00" "06:00"
    ""      ""     
    ""      ""     
    ""      ""     
    "06:00" "06:45"
    ""      ""     
    ""      ""     
    ""      ""     
    ""      ""     
    end

    The variables, as you can see, are in string format. When I try to encode, the format is as follows:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long(slot1start slot1end)
     .  .
     .  .
     .  .
     5  5
     .  .
     .  .
     .  .
     5  5
     .  .
     .  .
     .  .
    10 17
     .  .
     .  .
     .  .
    12 17
     .  .
     .  .
     .  .
    10 17
     .  .
     .  .
     .  .
     5 13
     .  .
     .  .
     .  .
    12 21
     .  .
     .  .
     .  .
    12 20
     .  .
     .  .
     .  .
     7  9
     .  .
     .  .
     .  .
    10 14
     .  .
     .  .
     .  .
    10 17
     .  .
     .  .
     .  .
    10 13
     .  .
     .  .
     .  .
    10 17
     .  .
     .  .
     .  .
    15 21
     .  .
     .  .
     .  .
    10 12
     .  .
     .  .
     .  .
     5  9
     .  .
     .  .
     .  .
     7 13
     .  .
     .  .
     .  .
     5  9
     .  .
     .  .
     .  .
    10 17
     .  .
     .  .
     .  .
    10 20
     .  .
     .  .
     .  .
    10 14
     .  .
     .  .
     .  .
    10 17
     .  .
     .  .
     .  .
     5  9
     .  .
     .  .
     .  .
    10 15
     .  .
     .  .
     .  .
     .  .
    end
    format %td slot1start
    label values slot1start slot1start
    label def slot1start 5 "05:00", modify
    label def slot1start 7 "05:30", modify
    label def slot1start 10 "06:00", modify
    label def slot1start 12 "06:30", modify
    label def slot1start 15 "07:00", modify
    label values slot1end slot1end
    label def slot1end 5 "05:30", modify
    label def slot1end 9 "06:00", modify
    label def slot1end 12 "06:20", modify
    label def slot1end 13 "06:30", modify
    label def slot1end 14 "06:40", modify
    label def slot1end 15 "06:45", modify
    label def slot1end 17 "07:00", modify
    label def slot1end 20 "08:00", modify
    label def slot1end 21 "08:30", modify

    I simply want the number of hours but most of the examples I have read about refer to elapsed time with days etc. Could someone point me in the right direction please as to what is the correct format needed to obtain elapsed time in this context?

    Thank you.


  • #2
    You should not be using encode to translate dates and times from string to numeric. You should never use encode to translate numbers stored as strings into numbers stored as numbers - destring is the command for that. But for dates and times stored as strings, a different approach is needed.

    Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

    All Stata manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

    With that said, here is some sample code that may, with the understanding you gain from the recommended reading, point you in a useful direction.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str20(stime1 stime2)
    "5:00"  "6:45"
    "12:30" "1:10"
    end
    generate double ntime1 = clock(stime1,"hm")
    generate double ntime2 = clock(stime2,"hm")
    replace ntime2 = ntime2 + 12*60*60*1000 if ntime2 < ntime1
    format ntime1 ntime2 %tcHH:MM
    generate diff_min = (ntime2 - ntime1)/(60*1000)
    list, noobs
    Code:
    . list, noobs
    
      +----------------------------------------------+
      | stime1   stime2   ntime1   ntime2   diff_min |
      |----------------------------------------------|
      |   5:00     6:45    05:00    06:45        105 |
      |  12:30     1:10    12:30    13:10         40 |
      +----------------------------------------------+

    Comment

    Working...
    X