Thank you, this makes a lot of sense! I have another follow-up question - what if we were to have 4 years of data per individual, but we only wanted to see if they had that disease within the last 2 years? My original dataset subject year disease1 disease2 disease 3 a 2019 1 1 1 a 2020 0 0 0 a 2021 0 0 0 a 2022 0 0 0 b 2019 0 1 1 b 2020 1 0 0 b 2021 1 0 0 b 2022 0 0 1 My desired output is below. For example - for subject a in 2021, since they had all 3 diseases in the prior year of 2019, they should also have flags for all those diseases in 2020 and 2021. But since 2022 is after the 2 year mark, and they did not have the disease in 2020 and 2021, they will not have flags for the year 2022. My desired output subject year disease1 disease2 disease 3 a 2019 1 1 1 a 2020 1 1 1 a 2021 1 1 1 a 2022 0 0 0 b 2019 0 1 1 b 2020 1 1 1 b 2021 1 1 1 b 2022 1 0 1
... View more