Announcement

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

  • Working with TIME variable

    Dear Stata Users

    I want to convert a "string" variable into time. It works for most of the time except for 8 and 9am.

    Code:
    tostring TRD_TM , generate(time)
    generate double time1= clock( time ,"hms#")
    format time1 %tc_HH:MM:SS
    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long TRD_TM str9 time double time1
     83049007 "83049007"         .
     83049007 "83049007"         .
     83049007 "83049007"         .
     92658513 "92658513"         .
     92915849 "92915849"         .
     92924443 "92924443"         .
    100013096 "100013096" 36013000
    100013096 "100013096" 36013000
    100013096 "100013096" 36013000
    155834219 "155834219" 57514000
    171030574 "171030574" 61830000
    end
    format %tc_HH:MM:SS time1
    ------------------ copy up to and including the previous line ------------------

    I would appreciate if anybody can tell me how to convert 8 and 9am into valid time variable.

    Thank you.

  • #2
    Code:
    tostring TRD_TM , generate(time_str) format(%09.0f)
    generate double time2= clock( time_str ,"hms#")
    format time2 %tc_HH:MM:SS
    The clock function is unable to parse your variable time because there is no leading 0 in front of the 8 or 9. k So it thinks you are asking it to calculate the Stata numeric representation for 83:04:90.07 instead of 8:30:49.007. By specifying the -format- option as shown above, you get the needed leading zero.

    Comment


    • #3
      Clyde

      Thank you.

      It works now.

      Comment


      • #4
        This also works:

        Code:
        generate double time2= clock(string(TRD_TM, "%09.0f"),"hms#")
        
        format time2 %tc_HH:MM:SS

        Comment

        Working...
        X