BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Smitha9
Fluorite | Level 6

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

Next code reads the data and sort it to the desired order.

You just need to adapt it to save the right two obs per ID - as in ID=5.

data a;
 infile datalines truncover ;
 informat ID 1. sub $1. date1 mmddyy10. date2 mmddyy10. difference best7.;
 input ID sub $ date1 date2 difference ;
 format date1 date2 mmddyy10.;
datalines;
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
; run;
proc sort data=a out=b; by ID descending difference; run;

 

View solution in original post

1 REPLY 1
Shmuel
Garnet | Level 18

Next code reads the data and sort it to the desired order.

You just need to adapt it to save the right two obs per ID - as in ID=5.

data a;
 infile datalines truncover ;
 informat ID 1. sub $1. date1 mmddyy10. date2 mmddyy10. difference best7.;
 input ID sub $ date1 date2 difference ;
 format date1 date2 mmddyy10.;
datalines;
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
; run;
proc sort data=a out=b; by ID descending difference; run;