ID Sub Date1 Date2 Difference
1 A 11/02/1998 08/04/2022 -8676
2 A 06/24/1999 01/12/2022 -8238
2 B 09/16/2019 01/12/2022 -849
3 A 03/11/2019 05/17/2021 -798
4 A 07/11/2005 01/15/2022 -6032
4 B 05/11/2018 01/15/2022 -1345
5 A 09/19/2007 04/09/2020 -4586
5 B 09/19/2007 04/09/2020 -4586
5 C 02/09/2010 04/09/2020 -3712
I want the dataset Date1 near to the Date2.
I want only maximum 2 rows per ID and the first would be the nearest and the second would be the next nearest/same difference if Sub is different.
ID Sub Date1 Date2 Difference
1 A 11/02/1998 08/04/2022 -8676 ( for ID=1 only one row)
2 B 09/16/2019 01/12/2022 -849 (for ID=2 849 is nearest so this comes first)
2 A 06/24/1999 01/12/2022 -8238 (for ID=2 8238 is second nearest)
3 A 03/11/2019 05/17/2021 -798 (for ID=3 only one row)
4 B 05/11/2018 01/15/2022 -1345 (for ID=4 -1345 is nearest to date2)
4 A 07/11/2005 01/15/2022 -6032 ( for ID=4 next nearest to date2)
5 A 09/19/2007 04/09/2020 -4586 ( for ID=5 first nearest to date2)
5 B 09/19/2007 04/09/2020 -4586 ( for ID=5 same difference -4586 but different Sub
so comes second row)
can I do this in SAS?
thanks in advance.