Announcement

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

  • Deleting mismatched observations for each panel id

    Hello everyone,
    I am cleaning my dataset and having a problem with one of the variables. So I have data that looks somewhat like this. The variable "trip/km" shows the "trip since the last fueling". This implies that for the first date mentioned in the table, I do not need 395 as it does not have the corresponding data.
    And for this I also need to further correspond the data correctly with each observation, so now my task is to show that 416 is the trip traveled from 28l of fuel and delete all irrelevant observations. I need to do this for each vehicle-id. I am not sure how to go about it.
    Adding to that I might have to change the label for trip/km - trip/km traveled from fuelling.
    Can someone please help me out with this?
    vehicle-id refueling-date quantity trip/km
    13 21-03-2021 28 395
    13 13-09-2021 35 416
    13 23-04-2021 23 333
    20 22-04-2018 80.59 599
    20 17-09-2018 87.43 657

  • #2
    Please present data examples using the dataex command as recommended in FAQ Advice #12. Assuming that "refueling_date" is a proper Stata date variable and no refueling dates are missing (not recorded), you want:

    Code:
    bys vehicle_id (refueling_date): gen wanted= trip[_n+1]
    drop if missing(wanted)
    drop trip
    list, sepby(vehicle_id)

    Comment

    Working...
    X