Announcement

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

  • hours and dates

    hello, i am trying to substract 2 dates and times to obtain a duration. the 2 dates and the 2 times of day are separate. Before doing the substraction, which should be straightforward, how do i fuse date and time? thanks
    here under are the 4 variables
    Thank you!
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int DAT1 double HOUR1 int DAT2 double HOUR2
    23034 -1.8933732e+12 23034      -1893429120000
    23031 -1.8934281e+12 23034 -1893412080000.0002
    23034 -1.8934107e+12 23034 -1893406379999.9998
    23087  -1.893447e+12 23087      -1.8934335e+12
        .              . 23076 -1893439559999.9998
    23041 -1.8934026e+12 23042      -1893416940000
    23054 -1.8934452e+12 23054 -1893400799999.9998
    23035  -1.893411e+12 23036      -1.8934542e+12
    23032 -1.8934488e+12 23035      -1893400440000
    23037 -1.8933786e+12 23038      -1.8934539e+12
    end
    format %tdnn/dd/CCYY DATRUPT
    format %tchh:MM HEURRUPT
    format %tdnn/dd/CCYY DATNAISS
    format %tchh:MM HRNAISS

  • #2
    Code:
    forvalues i = 1/2 {
        gen double date_time`i' = cofd(DAT`i') + HOUR`i'
        format date_time`i' %tc
    }
    As for calculating the difference, you may find the -clockdiff()- or -clockdiff_frac()- function more useful than subtraction, in that you can get an answer that is denominated in an appropriate time unit rather than another clock variable. (That's assuming you are using a relatively recent version of Stata that has those functions.)

    Comment


    • #3
      thanks a lot!

      Comment

      Working...
      X