Announcement

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

  • Incorrect results when subtracting time

    Dear all

    I am trying to compute call duration from subtracting two time periods in Stata but I get resultant incorrect time difference between the two variables. I have looked online but was unable to fix the issue. The "CallDuration" variable that I have has constant 12 hours added to it. I am trying to subtract 12 hours (the variable for this is "twlhour") from my "CallDuration" variable but the answer seems to be incorrect and unintuitive. Please tell me how to fix this problem.

    This is my sample data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int(CalledNumber Date) double(Time CallDuration) float(twlhour call_dur)
    15000 21304      -1893384240000      -1893455984000 4.32e+07 -1.893499e+12
    15000 21304      -1893384240000      -1893455925000 4.32e+07 -1.893499e+12
    15000 21304 -1893384300000.0002      -1893455891000 4.32e+07 -1.893499e+12
    15000 21304 -1893384300000.0002 -1893455950999.9998 4.32e+07 -1.893499e+12
    15000 21304      -1893384360000 -1893455985999.9998 4.32e+07 -1.893499e+12
    15000 21304      -1893384660000      -1893455914000 4.32e+07 -1.893499e+12
    15000 21304      -1893384660000 -1893455677999.9998 4.32e+07 -1.893499e+12
    15000 21304      -1893384720000      -1893455813000 4.32e+07 -1.893499e+12
    15000 21304      -1893384720000 -1893455954999.9998 4.32e+07 -1.893499e+12
    15000 21304      -1893384840000 -1893455768000.0002 4.32e+07 -1.893499e+12
    end
    format %tdnn/dd/CCYY Date
    format %tchh:MM_AM Time
    format %tchh:MM:SS CallDuration
    format %tcHH:MM:SS twlhour
    format %tcMM:SS call_dur
    I am running the following Code :

    gen twlhour = 60*60*1000*12 // making a 12 hour variable
    format twlhour %tcHH:MM:SS // formating
    gen call_dur = CallDuration - twlhour //
    format call_dur %tcMM:SS

    Thanks anyways



  • #2
    Always use double for any variable measured in milliseconds, So

    Code:
    gen double call_dur = CallDuration - msofhours(12)
    There is no point to defining a variable containing a constant msofhours(12) or 60*60*1000*12. That value is repeated in every observation if you do that, It's not like a variable in a common or garden programming language, That's an entire variable (column) in your dataset.

    See

    Code:
    help datetime
    where the advice to use doubles is repeated often,

    Comment


    • #3
      Thank you very much. This works! I will use doubles moving forward

      Comment

      Working...
      X