I have a dataset that is a panel data, that is many ID's for many moments in time. It looks like this:
ID | Date | Start date
1 | 01/01/2021 | 01/01/2021
1 | 02/01/2021 | 01/01/2021
1 | 03/01/2021 | 01/01/2021
1 | 20/01/2021 | 20/01/2021
1 | 21/01/2021 | 20/01/2021
2 | 05/01/2021 | 05/01/2021
2 | 06/01/2021 | 05/01/2021
2 | 07/01/2021 | 05/01/2021
3 | 10/01/2021 | 10/01/2021
3 | 11/01/2021 | 10/01/2021
3 | 25/01/2021 | 25/01/2021
Where basically, Date
denotes which days the ID appeared on the table, and Start date
the start date of some thing happening (doesn't really matter what in this case).
I want to obtain a table like this:
ID | Start date | End date
1 | 01/01/2021 | 03/01/2021
1 | 20/01/2021 | 21/01/2021
2 | 05/01/2021 | 07/01/2021
3 | 10/01/2021 | 11/01/2021
3 | 25/01/2021 | 25/01/2021
Where there is one line per "start date" per ID, and the "end date" is added according to the last date in which the person had that "start date".
This is my proposed code but it's clearly missing something, I think mainly how to specify that the "if" should be done for each Start_date.
DATA ArrearsPeriods;
SET data;
BY ID Start_date Date;
RETAIN ID Start_date END_DATE;
IF last.DateTHEN END_DATE = Date;
RUN;
If there's a better way of doing this in proc sql, be my guest!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.