Announcement

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

  • replace, depending on a time variable

    Hi everyone,

    I want to make a replace in a variable (interlude), if another variable is within a range (start_time and end_time).

    Basically, I want to replace interlude = 0 if start_time[_n] and end_time[_n-1] for each individual and trip (ID_VIAJE) are within 8 p.m. and 9 a.m.

    Could anyone give me a solution to that?
    Here is a dataex:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str9 individ_ID byte ID_VIAJE double(start_time end_time) float interlude
    "1000154_1" 1 59400000 60300000   .
    "1000154_1" 2 79200000 81000000 315
    "1000154_2" 1 64800000 66600000   .
    "1000154_2" 2 79200000 81000000   .
    "1000289_1" 1 28800000 30600000   .
    "1000289_1" 2 36000000 37800000   .
    "1000289_3" 1 35400000 36000000   .
    "1000289_3" 2 52200000 54000000   .
    "1000289_3" 3 61200000 62400000   .
    "1000289_3" 4 75600000 76800000   .
    "1000370_1" 1 41400000 45000000   .
    "1000370_1" 2 52200000 55800000 120
    "1000370_1" 3 63000000 66600000 120
    "1000370_1" 4 75600000 79200000 150
    "1000370_2" 1  9000000 10800000   .
    "1000370_2" 2 45060000 46800000 571
    "1000457_1" 1 37800000 39600000   .
    "1000457_1" 2 43200000 46800000   .
    "1000457_2" 1 36000000 39600000   .
    "1000457_2" 2 50400000 54000000   .
    "1000457_4" 1 50400000 54000000   .
    "1000457_4" 2 75600000 79200000   .
    "1000660_1" 1 36000000 37800000   .
    "1000660_1" 2 68400000 70200000   .
    "1001299_1" 1 57600000 58800000   .
    "1001299_1" 2 72000000 73800000   .
    "1001530_1" 1 31560000 32400000   .
    "1001530_1" 2 38700000 39600000   .
    "1001530_1" 3 70620000 70800000   .
    "1001530_1" 4 73620000 73800000   .
    "1001728_1" 1 61200000 61800000   .
    "1001728_1" 2 64200000 64800000   .
    "1001728_2" 1 61200000 61800000   .
    "1001728_2" 2 64200000 64800000   .
    "1001942_1" 1 34920000 35100000   .
    "1001942_1" 2 42180000 42360000   .
    "1001942_2" 1 34200000 35100000   .
    "1001942_2" 2 36900000 37800000   .
    "1002132_1" 1 28800000 32400000   .
    "1002132_1" 2 68400000 72000000   .
    "1002132_2" 1 43200000 50400000   .
    "1002132_2" 2 79200000 86400000   .
    "1002179_1" 1 32400000 34200000   .
    "1002179_1" 2 39600000 41400000   .
    "1002291_1" 1 35580000 36300000   .
    "1002291_1" 2 40980000 41700000   .
    "1002291_1" 3 61200000 61500000   .
    "1002291_1" 4 66600000 66900000   .
    "1002291_2" 1 48240000 48600000   .
    "1002291_2" 2 48600000 48900000   .
    "1002315_1" 1 45000000 45600000   .
    "1002315_1" 2 52200000 53100000   .
    "1002400_1" 1 28800000 29100000   .
    "1002400_1" 2 29100000 29400000   .
    "1002400_1" 3 48600000 48900000   .
    "1002400_1" 4 48900000 49200000   .
    "1002400_1" 5 75600000 75900000   .
    "1002400_1" 6 75900000 76200000   .
    "1002400_2" 1 28740000 28800000   .
    "1002400_2" 2 53940000 54000000   .
    "1002400_2" 3 59340000 59400000   .
    "1002400_2" 4 79140000 79200000   .
    "1002400_3" 1 55800000 57000000   .
    "1002400_3" 2 75600000 76800000   .
    "1002741_1" 1 29700000 31800000   .
    "1002741_2" 1 27900000 28800000   .
    "1002741_3" 1 30600000 32400000   .
    "1002741_3" 2 63000000 64800000   .
    "1002741_4" 1 27000000 30600000   .
    "1002741_4" 2 50400000 52200000   .
    "1002741_5" 1 25200000 28800000   .
    "1002741_5" 2 50400000 54000000   .
    "1002741_6" 1 30600000 31800000   .
    "1002809_1" 1 30600000 31800000   .
    "1002809_1" 2 31800000 32160000   .
    "1002809_1" 3 71640000 72000000 658
    "1002809_2" 1 27000000 28800000   .
    "1002809_2" 2 57600000 59400000 480
    "1002809_2" 3 59400000 61200000   .
    "1002956_2" 1 36360000 37800000   .
    "1002956_2" 2 61800000 63000000   .
    "1003400_1" 1 55800000 57600000   .
    "1003400_1" 2 64800000 67200000   .
    "1003400_2" 1 64800000 65700000   .
    "1003400_2" 2 71100000 72000000   .
    "1003589_1" 1 30300000 31200000   .
    "1003589_1" 2 64500000 65400000   .
    "1003589_2" 1 30300000 31200000   .
    "1003589_2" 2 31200000 32100000   .
    "1003589_2" 3 32100000 33600000   .
    "1003589_2" 4 50400000 51900000   .
    "1003589_2" 5 63900000 64500000   .
    "1003589_2" 6 64500000 65400000   .
    "1003589_2" 7 65400000 66600000   .
    "1003589_2" 8 82800000 84300000   .
    "1003988_1" 1 25200000 27000000   .
    "1003988_1" 2 63000000 64800000 600
    "1003988_2" 1 54540000 55800000   .
    "1003988_2" 2 75240000 76500000 324
    "1003988_2" 3 82800000 83700000 105
    end
    format %tC start_time
    format %tC end_time
    I don't know how to know when it is 8 p.m. or not with this type of variable.
    Thank you in advance for your help.

    Best,

    Michael

  • #2
    Code:
    replace interlude = 0 if inrange(start_time, tc(8:00PM), tc(9:00PM))   ///
        & inrange(end_time, tc(8:00PM), tc(9:00PM))
    will work with this data because start_time and end_time are all dated 1jan1960 (i.e. they are the closest one can get in Stata to a "pure time" variable.)

    In more general data where the start_time and end_time variables also had dates involved, it would be a bit different:
    Code:
    replace interlude = 0 if inrange(start_time-cofd(dofc(start_time)), tc(8:00PM), tc(9:00PM))   ///
        & inrange(end_time-cofd(dofc(end_time)), tc(8:00PM), tc(9:00PM))

    Comment


    • #3
      Hi Clyde Schechter,

      Thank you for your post #2.
      I didn't know that

      Code:
       replace interlude = 0 if inrange(start_time, tc(8:00PM), tc(9:00PM))   ///    
           & inrange(end_time, tc(8:00PM), tc(9:00PM))
      works in this case.
      Thank you so much for your time.

      Best,
      Michael

      Comment


      • #4
        Sorry,

        I meant between 8 p.m. and 9 a.m. in #1.
        • Does the code provided in #2 works then?
        Because I obtain something weird:

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str9 individ_ID byte ID_VIAJE double(start_time end_time) float interlude
        "1001530_1" 3 70620000 70800000 517
        "1001530_1" 4 73620000 73800000   0
        end
        format %tC start_time
        format %tC end_time
        start_time[_n] end_time[_n-1] interlude
        forget about that 01jan1960 19:40:00
        01jan1960 20:27:00 forget about that 0
        It should rather be 0 if end_time[_n-1] begins after 20:00:00. So interlude should be 20. Maybe I explain myself badly.
        If it is the case, I apologize for this.

        Thank you.
        Michael
        Last edited by Michael Duarte Goncalves; 24 Oct 2023, 10:00.

        Comment


        • #5
          No, it doesn't work, because between 8PM and 9AM would otherwise cross a day-boundary, whereas your start_time and end_time variables do not "flip the date." So you have to treat the time between 8PM and midnight, and midnight to 9AM separately.

          Code:
          replace interlude = 0 if ///
          (inrange(start_time, tc(8:00PM), tc(11:59:59PM)) | inrange(start_time, tc(12:00AM), tc(9:00AM))) ///
          & (inrange(end_time, tc(8:00PM), tc(11:59:59PM)) | inrange(end_time, tc(12:00AM), tc(9:00AM)))

          Comment


          • #6
            Ok nice! This part was tricky for me.

            Thank you for your tips.
            Michael

            Comment

            Working...
            X