Announcement

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

  • Extracting hours (including decimal part) from string variable in hh:mm:ss

    Hi all. I have a dataset with information on the time (i.e., hh:mm:ss) spent with national broadcasting by the president of Venezuela. The string variable storing time is “cadenas_time”. Please see part of the data below.

    I need to extract the hours from the "cadenas_time" variable (including decimal points).

    For instance, 2:10:13 would be converted to 2.17 (2 hours + 10/60 = 0.17)

    Could someone help?

    Thank you very much.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int year byte month int dm str9 country str8 cadenas_time
    2012  1 624 "Venezuela" "20:42:57"
    2012  2 625 "Venezuela" "26:24:27"
    2012  3 626 "Venezuela" "11:59:00"
    2012  4 627 "Venezuela" "6:49:20" 
    2012  5 628 "Venezuela" "7:41:59" 
    2012  6 629 "Venezuela" "18:22:52"
    2012  7 630 "Venezuela" "15:57:50"
    2012  8 631 "Venezuela" "19:33:40"
    2012  9 632 "Venezuela" "9:22:41" 
    2012 10 633 "Venezuela" "2:39:28" 
    end
    format %tm dm

  • #2
    Well, if you did not have times exceeding 24 hours, it could be a one liner. But, you do, so it's a little harder:

    Code:
    split cadenas_time, parse(":") gen(hrs) destring
    gen elapsed_hours = hrs1 + hrs2/60 + hrs3/(60*60)

    Comment


    • #3
      Thanks a lot, Clyde.

      Comment

      Working...
      X