Announcement

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

  • Problem with a time variable: How can I extract values in a certain range please?

    Hi everyone, I have a variable of type double %tC, and I want to create a new variable that takes only values from 9 p.m. to 6:59 a.m.
    Then, I want to do -replace- condition with this new variable, but only for observations that have a "start-time" between 7:00 a.m. to 8:59 p.m.

    Could anyone help me please:

    Here's one of my code attempts:

    Code:
    gen taxi_trips_costs = .
    label var taxi_trips_costs "Cost of a Taxi Trip, in €"
    
    replace taxi_trips_costs = 30 if (origin_barrio == "Aeropuerto" & destination_madridcentral == 1) | (origin_madridcentral == 1 & destination_barrio == "Aeropuerto")
    
    replace taxi_trips_costs = 2.4 + 1.05 * DISTANCIA_VIAJE if (origin_barrio != "Aeropuerto" & origin_madridcentral == 0) | (destination_barrio!= "Aeropuerto" & destination_madridcentral == 0) & start_hour >= 7 & start_hour <= 21
    
    replace taxi_trips_costs = 2.9 + 1.2 * DISTANCIA_VIAJE if (origin_barrio != "Aeropuerto" & origin_madridcentral == 0) | (destination_barrio != "Aeropuerto" & destination == 0) & (start_hour >= 21 & start_min >=0 | start_hour < 7)
    Here is a dataex example:

    [CODE]

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str47 origin_barrio long origin_madridcentral str47 destination_barrio long destination_madridcentral byte(start_hour start_minute) double start_time
    "Valverde"             0 "Valverde"                       0 15  5 54300000
    "Aluche"               0 "Aluche"                         0  8 15 29700000
    "Legazpi"              0 "Delicias"                       0 12  0 43200000
    "Almenara"             0 "Almenara"                       0  9  0 32400000
    "Entrevías"           0 "Entrevías"                     0  9  0 32400000
    "Aluche"               0 "Aluche"                         0 20 30 73800000
    "Opañel"              0 "Abrantes"                       0 20 30 73800000
    "Entrevías"           0 "Entrevías"                     0 11 30 41400000
    "Pilar"                0 "Pilar"                          0 17  0 61200000
    "Pilar"                0 "Valverde"                       0 16  0 57600000
    "Los Ángeles"         0 "Villaverde Alto"                0  7 30 27000000
    "El Viso"              0 "Recoletos"                      0  8 45 31500000
    "Apostol Santiago"     0 "Apostol Santiago"               0 14 45 53100000
    "Moscardó"            0 "Imperial"                       0 18  0 64800000
    "Recoletos"            0 "Palomeras Bajas"                0 14  0 50400000
    "Atocha"               0 "Palos de Moguer"                0 10 30 37800000
    "Buenavista"           0 "Cuatro Vientos"                 0  8 53 31980000
    "Argüelles"           0 "Numancia"                       0 16  0 57600000
    "Embajadores"          1 "Cortes"                         1 10 25 37500000
    "Quintana"             0 "Hispanoamérica"                0  8 10 29400000
    "Palacio"              1 "Argüelles"                     0 11 40 42000000
    "Embajadores"          1 "Embajadores"                    1 22 30 81000000
    "Numancia"             0 "Portazgo"                       0 17  2 61320000
    "Opañel"              0 "Comillas"                       0 14 20 51600000
    "Moscardó"            0 "Vista Alegre"                   0 12  0 43200000
    "Puerta del Ángel"    0 "Lucero"                         0 14  0 50400000
    "Rosas"                0 "Arcos"                          0 19 54 71640000
    "Piovera"              0 "San Pascual"                    0 18 35 66900000
    "Ensanche de Vallecas" 0 "Ensanche de Vallecas"           0  9  0 32400000
    "El Goloso"            0 "Palos de Moguer"                0 17  0 61200000
    "Castilla"             0 "Ciudad Jardín"                 0 17 30 63000000
    "Castellana"           0 "Lista"                          0 16 55 60900000
    "Aravaca"              0 "Almagro"                        0 12  0 43200000
    "Villaverde Alto"      0 "Villaverde Alto"                0  9 38 34680000
    "Águilas"             0 "Águilas"                       0  9 40 34800000
    "Legazpi"              0 "Pacífico"                      0 16  0 57600000
    "Puerta Bonita"        0 "Universidad"                    1 12  0 43200000
    "Valdemarín"          0 "Valdemarín"                    0  9  9 32940000
    "Delicias"             0 "Embajadores"                    1 17 31 63060000
    "Guindalera"           0 "Guindalera"                     0 11  0 39600000
    "Rosas"                0 "Arcos"                          0 16 45 60300000
    "San Juan Bautista"    0 "Almagro"                        1 10 30 37800000
    "El Viso"              0 "Timón"                         0 16 40 60000000
    "Guindalera"           0 "Pueblo Nuevo"                   0 14 15 51300000
    "Ciudad Jardín"       0 "Cortes"                         1 20 45 74700000
    "Concepción"          0 "Concepción"                    0 16 45 60300000
    "La Paz"               0 "Pilar"                          0 20 55 75300000
    "Guindalera"           0 "Guindalera"                     0 13 20 48000000
    "Valderrivas"          0 "Casco Histórico de Vicálvaro" 0  9 30 34200000
    "Cuatro Caminos"       0 "Cuatro Caminos"                 0 16 30 59400000
    "Puerta Bonita"        0 "Rios Rosas"                     0 15  0 54000000
    "Orcasitas"            0 "Orcasitas"                      0 12 57 46620000
    "San Cristóbal"       0 "Acacias"                        0  8 15 29700000
    "Mirasierra"           0 "Peñagrande"                    0 22  0 79200000
    "Legazpi"              0 "Butarque"                       0 19  0 68400000
    "Media Legua"          0 "Castellana"                     0 10 50 39000000
    "Palomeras Sureste"    0 "Palomeras Sureste"              0 19 30 70200000
    "San Juan Bautista"    0 "Valdefuentes"                   0  8 55 32100000
    "Justicia"             1 "Trafalgar"                      0 17  0 61200000
    "Universidad"          1 "Valverde"                       0 22  0 79200000
    "Mirasierra"           0 "Mirasierra"                     0  8 55 32100000
    "Costillares"          0 "Apostol Santiago"               0 17  6 61560000
    "El Viso"              0 "Arcos"                          0 15  0 54000000
    "Cuatro Vientos"       0 "Cuatro Vientos"                 0 17 20 62400000
    "Numancia"             0 "Numancia"                       0 18 30 66600000
    "Goya"                 0 "Goya"                           0 12 30 45000000
    "Portazgo"             0 "Palomeras Bajas"                0  7 30 27000000
    "Ibiza"                0 "Palacio"                        1  8 41 31260000
    "Ibiza"                0 "Ibiza"                          0 19 32 70320000
    "Horcajo"              0 "Valdebernardo"                  0 19 15 69300000
    "Pacífico"            0 "Casco Histórico de Vallecas"   0 17  0 61200000
    "Puerta del Ángel"    0 "Puerta del Ángel"              0  8 50 31800000
    "Numancia"             0 "Numancia"                       0  8  0 28800000
    "Palomeras Bajas"      0 "Portazgo"                       0 15  0 54000000
    "Vinateros"            0 "Vinateros"                      0 11  0 39600000
    "Vista Alegre"         0 "Legazpi"                        0 18 30 66600000
    "Valverde"             0 "Salvador"                       0  9 50 35400000
    "Arapiles"             0 "Cortes"                         1 19 15 69300000
    "Bellas Vistas"        0 "Vallehermoso"                   0 13 40 49200000
    "Los Ángeles"         0 "San Fermín"                    0 16 30 59400000
    "Rosas"                0 "Rosas"                          0  8  0 28800000
    "Almenara"             0 "Adelfas"                        0 17  0 61200000
    "Palos de Moguer"      0 "Delicias"                       0 19 45 71100000
    "Cuatro Caminos"       0 "Valverde"                       0 18 50 67800000
    "Cármenes"            0 "Lucero"                         0  8 30 30600000
    "Fuente del Berro"     0 "Fuente del Berro"               0 20  0 72000000
    "Valverde"             0 "Valdebernardo"                  0 16  0 57600000
    "Sol"                  1 "Delicias"                       0 21 30 77400000
    "San Isidro"           0 "San Isidro"                     0 13 45 49500000
    "Ciudad Universitaria" 0 "Puerta Bonita"                  0 15  0 54000000
    "Numancia"             0 "Numancia"                       0 21 20 76800000
    "Alameda de Osuna"     0 "Alameda de Osuna"               0 13 40 49200000
    "Los Rosales"          0 "Vallehermoso"                   0 18  0 64800000
    "Ensanche de Vallecas" 0 "Marroquina"                     0 20 30 73800000
    "Numancia"             0 "Casco Histórico de Vallecas"   0 14 40 52800000
    "Ciudad Universitaria" 0 "Peñagrande"                    0 16 38 59880000
    "Palacio"              1 "Almenara"                       0 19  0 68400000
    "Nueva España"        0 "Hispanoamérica"                0 19  0 68400000
    "Imperial"             0 "Opañel"                        0 12 30 45000000
    "Guindalera"           0 "Lista"                          0 18  0 64800000
    end
    format %tC start_time
    label values origin_madridcentral madcent
    label values destination_madridcentral madcent
    label def madcent 0 "Rest of Madrid", modify
    label def madcent 1 "Madrid Central", modify

    The part of "start_time" variable 01jan1960 is a mistake. We can just drop it please.

    Thank you in advance.

    Best,

    Michael
    Last edited by Michael Duarte Goncalves; 05 Oct 2023, 03:47.

  • #2
    The part of "start_time" variable 01jan1960 is a mistake. We can just drop it please.
    No, it's not a mistake; you can't drop it and shouldn't try to. There is no pure time variable in Stata. There are datetime variables. To represent a time of day, you must include a date as well. The default date is 01jan1960. If it displeases you to see 01jan1960 there, you can suppress its appearance with
    Code:
    format start_time %tCHH:MM
    But this does not actually change the value of start_time from Stata's perspective. (And, again, you shouldn't. The variable is correctly constructed as is.)

    With regard to your taxi_trips_cost variable, the code you show fails with your data example because it refers to a variable, DISTANCIA_VIAJE, which does not exist, and because the final-replace refers to a variable destination--and Stata cannot figureout whether you mean destination_madridcentral or destination_barrio. But once you fix those problems, it all seems to run without errors and produces reasonable appearing results. So what exactly is the question?

    Comment


    • #3
      Dear Clyde Schechter:

      Thank you very much for your suggestion. I didn't know I should not drop it.

      Thanks for your suggestion, I clean up some data, and all works well.

      Thank you for your time.

      Best regards,

      Michael

      Comment

      Working...
      X