Announcement

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

  • How do I get time difference (gap) in second?

    I have a dataset like this
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str19 time_org float id
    "2009-01-03 18:15:05" 0
    "2009-01-09 02:54:25" 1
    "2009-01-09 02:55:44" 2
    "2009-01-09 03:02:53" 3
    "2009-01-09 03:16:28" 4
    "2009-01-09 03:23:48" 5
    "2009-01-09 03:29:49" 6
    "2009-01-09 03:39:29" 7
    "2009-01-09 03:45:43" 8
    "2009-01-09 03:54:39" 9
    end
    The original time_org variable is a string one, so I converted it then generate a new variable, time
    Code:
    gen year = substr(time_org,1,4)
    gen month = substr(time_org,6,2)
    gen day = substr(time_org,9,2)
    
    gen hour = substr(time_org,12,2)
    gen min = substr(time_org,15,2)
    gen second = substr(time_org,18,2)
    
    foreach x of varlist year month day hour min second {
        destring `x', replace
        }
    *
    gen time = mdyhms(month,day,year,hour,min,second)
    Next, I create a lag1 variable and make a calculate on time difference between time and lag1.
    Code:
    gen lag1 = time[_n-1]
    gen period = (time-lag1)/1000
    list in 1/10
    I have 2 questions and need your help:

    1) It seems the results for time difference in seconds are incorrect.
    2) If I set the datetime format with the code below, the time_org and time are different. How to fix it?
    Code:
    format time %tc
    I tried with code below and results are incorrect too.
    Code:
    gen time = Cmdyhms(month,day,year,hour,min,second)
    format time %tC
    Thank you so much.

  • #2
    Time variables should always be stored as double, otherwise you risk running into the kinds of problem that you report.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str19 time_org float id
    "2009-01-03 18:15:05" 0
    "2009-01-09 02:54:25" 1
    "2009-01-09 02:55:44" 2
    "2009-01-09 03:02:53" 3
    "2009-01-09 03:16:28" 4
    "2009-01-09 03:23:48" 5
    "2009-01-09 03:29:49" 6
    "2009-01-09 03:39:29" 7
    "2009-01-09 03:45:43" 8
    "2009-01-09 03:54:39" 9
    end
    
    gen double time= clock(time_org,"YMDhms")
    format time %tc
    gen wanted= (time-time[_n-1])/1000
    Res.:

    Code:
    . l, sep(10)
    
         +--------------------------------------------------------+
         |            time_org   id                 time   wanted |
         |--------------------------------------------------------|
      1. | 2009-01-03 18:15:05    0   03jan2009 18:15:05        . |
      2. | 2009-01-09 02:54:25    1   09jan2009 02:54:25   463160 |
      3. | 2009-01-09 02:55:44    2   09jan2009 02:55:44       79 |
      4. | 2009-01-09 03:02:53    3   09jan2009 03:02:53      429 |
      5. | 2009-01-09 03:16:28    4   09jan2009 03:16:28      815 |
      6. | 2009-01-09 03:23:48    5   09jan2009 03:23:48      440 |
      7. | 2009-01-09 03:29:49    6   09jan2009 03:29:49      361 |
      8. | 2009-01-09 03:39:29    7   09jan2009 03:39:29      580 |
      9. | 2009-01-09 03:45:43    8   09jan2009 03:45:43      374 |
     10. | 2009-01-09 03:54:39    9   09jan2009 03:54:39      536 |
         +--------------------------------------------------------+

    Comment


    • #3
      It works. I appreciate for your help to fix it, Andrew.

      Comment

      Working...
      X