Announcement

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

  • Calculating difference between time variables

    Hi everyone,

    I need to calculate a time difference, between two variables: start_time and end_time[_n-1].

    But with a special condition:
    • If end_time[_n-1] for individual i begins before 9 p.m. and start_time continues after 9p.m. up to 9a.m, I should create a new variable that takes into account the total amount of time elapsed since -end_time[_n-1] (trip 1) and start_time (trip 2), and then substract from this amount the time in which parking spots are free (to give a little context, parking is free from 9 p.m. until 9 a.m.). I have already a variable (interlude) that is the time elapsed since end_time[_n-1] and start_time. But I need to create a new variable that substracts the time between the free scheme range (the fraction of time between 9 p.m. and 9 a.m.,) from the -interlude- variable. Could you give me some insights, please?
    I mention before that I need a creation of a new variable, but it could perfectly just to replace the interlude variable by what I mentioned above


    dataex:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str9 individ_ID double(start_time end_time) float interlude
    "1000154_1" 59400000 60300000   .
    "1000154_1" 79200000 81000000 315
    "1000154_2" 64800000 66600000   .
    "1000154_2" 79200000 81000000 210
    "1000289_1" 28800000 30600000   .
    "1000289_1" 36000000 37800000  90
    "1000289_3" 35400000 36000000   .
    "1000289_3" 52200000 54000000 270
    "1000289_3" 61200000 62400000 120
    "1000289_3" 75600000 76800000 220
    end
    format %tC start_time
    format %tC end_time
    -interlude-'s creation
    Code:
    by individ_ID (start_time), sort: gen interlude = ///
        clockdiff(end_time[_n-1], start_time, "m") if _n > 1 & (end_time[_n-1] < start_time | date[_n-1] < date)
    Thank you in advance.
    Best,
    --
    Michael
    Last edited by Michael Duarte Goncalves; 24 Oct 2023, 09:34.

  • #2
    Hi everyone,

    Could anyone give me some insights please?
    Thank you in advance.

    Best regards,
    Michael

    Comment


    • #3
      I have tried this code, but not sure it is optimal...

      Code:
      gen interlude_time_diff = .
      gen nine_pm = clock("21:00:00", "hms")
      format %tC nine_pm
      
      sort individ_ID start_time
      quietly by individ_ID: gen actual_start_time = start_time if _n > 1
      format %tC actual_start_time
      
      forval i = 2/`=_N' {
          if actual_start_time[`i'] > nine_pm & end_time[`i'-1] < nine_pm {
              replace interlude_time_diff = clockdiff(nine_pm, actual_start_time [`i'], "m") in `i'
          }
      }
      don't hesitate to give me your feedback or suggestions for improvement, please.

      Thank you a lot in advance.

      Michael
      Last edited by Michael Duarte Goncalves; 25 Oct 2023, 04:14.

      Comment

      Working...
      X