Hello,
I have the following data below and I am trying to keep the most recent observation year for each ID that I have. I have tried different methods that did not work. Can someone please help? Thanks!
My goal is to only keep the most recent observation for each ID (ie 2020 for Dog, 2019 for Cat, 2021 for Bird, and 2020 for Mouse). I am trying the code below but it is not working:
As it doesn't allow me to gsort with by. I tried the gsort on the preceding line and my error was that the by function cannot run because it is not sorted. I even tried to create an increasing variable (order) that takes the value 1 for 2021, 2 for 2022 and so on and it also didn't work.
Thanks!
I have the following data below and I am trying to keep the most recent observation year for each ID that I have. I have tried different methods that did not work. Can someone please help? Thanks!
ID | Year | Attribute |
A564 | 2018 | Dog |
A564 | 2019 | Dog |
A564 | 2020 | Dog |
A447 | 2018 | Cat |
A447 | 2019 | Cat |
A125 | 2019 | Bird |
A125 | 2020 | Bird |
A125 | 2021 | Bird |
A478 | 2020 | Mouse |
Code:
by ID, gsort -Year: gen keep = _n == 1
Thanks!
Comment