Dear all,

I am currently working on a patient consultations database from 2016 to 2021. My purpose is to keep the patients who had a visit in 2019,2020 or 2021. For these patients, I need to keep the history of their visits from before 2019.
In order to see which patients have had a consultation between 2019 and 2021, I have used the following code :

bysort id_data md_rc_wo_consultation_date : gen check_date = "X" if md_rc_wo_consultation_date > date("1/1/2019","MDY")

id_data being the unique identifier per patient and md_rc_wo_consultation_date being the consultation date variable.

In order to mark the previous consultations (between 2016 and 2018 included) I need to expand the "X" value in the check_date variable to all values sharing a same unique identifier. In the example below, in need patient 1 and 4 to have an "X" in the third column in all observations.
id_data md_rc_wo_consultation_date check_date
1 2018
1 2018
1 2019 X
1 2019 X
1 2021 X
2 2016
2 2016
2 2017
3 2021 X
3 2021 X
4 2017
4 2019 X
4 2020 X
I have tried working with bysort and _N but none of my trials returned results.

Thanks for your help !

Victoria