BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
janet0102
Calcite | Level 5

I have a dataset with the ID and key date for each individual. I have another dataset with the ID and multiple event dates (each is an observation) for each individual. I need to find the event date that is closest to (before) or the same as the key date for each individual. Individuals may have as few as two event dates or as many as 50 event dates.

 

Below are ID and key date found in one data set:

 

ID

Key_Dt

15269

01/16/10

18658

07/31/08

23227

06/18/10

24189

10/27/08

24341

03/31/09

 

Here are IDs and event dates, currently residing in a second data set:

 

ID

Evnt_Dt

15269

10/15/10

15269

10/10/10

15269

09/02/10

15269

10/12/10

15269

04/28/10

15269

08/07/10

15269

05/13/10

15269

05/31/10

18658

06/13/08

18658

06/14/08

18658

06/19/08

18658

07/05/08

18658

07/14/08

18658

08/05/08

18658

03/02/09

23227

01/28/10

23227

03/30/10

23227

04/17/10

23227

04/18/10

23227

05/10/10

23227

06/02/10

23227

06/07/10

23227

06/11/10

23227

06/26/10

23227

07/01/10

23227

07/13/10

23227

08/09/10

24189

06/04/08

24189

07/23/08

24189

08/08/08

24189

08/15/08

24189

08/19/08

24189

09/01/08

24189

12/14/08

24189

01/28/09

24341

10/25/08

24341

11/11/08

24341

01/16/09

24341

03/09/09

24671

10/14/08

24671

11/30/08

24671

12/15/08

24671

02/10/09

24671

04/02/09

24671

05/18/09

24671

06/25/09

 

I'm using SAS 9.3. How do I do that? Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

A simple SQL query:

 

data have1;
input ID	Key_d :mmddyy8.;
format key_d yymmdd10.;
datalines;
15269	01/16/10
18658	07/31/08
23227	06/18/10
24189	10/27/08
24341	03/31/09
;
data have2;
input ID	Evnt_d :mmddyy.;
format Evnt_d yymmdd10.;
datalines;
15269	10/15/10
15269	10/10/10
15269	09/02/10
15269	10/12/10
15269	04/28/10
15269	08/07/10
15269	05/13/10
15269	05/31/10
18658	06/13/08
18658	06/14/08
18658	06/19/08
18658	07/05/08
18658	07/14/08
18658	08/05/08
18658	03/02/09
23227	01/28/10
23227	03/30/10
23227	04/17/10
23227	04/18/10
23227	05/10/10
23227	06/02/10
23227	06/07/10
23227	06/11/10
23227	06/26/10
23227	07/01/10
23227	07/13/10
23227	08/09/10
24189	06/04/08
24189	07/23/08
24189	08/08/08
24189	08/15/08
24189	08/19/08
24189	09/01/08
24189	12/14/08
24189	01/28/09
24341	10/25/08
24341	11/11/08
24341	01/16/09
24341	03/09/09
24671	10/14/08
24671	11/30/08
24671	12/15/08
24671	02/10/09
24671	04/02/09
24671	05/18/09
24671	06/25/09
;

proc sql;
create table want as
select a.id, max(evnt_d) as date format=yymmdd10.
from have1 as a left join have2 as b on a.id=b.id and a.key_d >= b.evnt_d
group by a.id;
select * from want; quit;
PG

View solution in original post

1 REPLY 1
PGStats
Opal | Level 21

A simple SQL query:

 

data have1;
input ID	Key_d :mmddyy8.;
format key_d yymmdd10.;
datalines;
15269	01/16/10
18658	07/31/08
23227	06/18/10
24189	10/27/08
24341	03/31/09
;
data have2;
input ID	Evnt_d :mmddyy.;
format Evnt_d yymmdd10.;
datalines;
15269	10/15/10
15269	10/10/10
15269	09/02/10
15269	10/12/10
15269	04/28/10
15269	08/07/10
15269	05/13/10
15269	05/31/10
18658	06/13/08
18658	06/14/08
18658	06/19/08
18658	07/05/08
18658	07/14/08
18658	08/05/08
18658	03/02/09
23227	01/28/10
23227	03/30/10
23227	04/17/10
23227	04/18/10
23227	05/10/10
23227	06/02/10
23227	06/07/10
23227	06/11/10
23227	06/26/10
23227	07/01/10
23227	07/13/10
23227	08/09/10
24189	06/04/08
24189	07/23/08
24189	08/08/08
24189	08/15/08
24189	08/19/08
24189	09/01/08
24189	12/14/08
24189	01/28/09
24341	10/25/08
24341	11/11/08
24341	01/16/09
24341	03/09/09
24671	10/14/08
24671	11/30/08
24671	12/15/08
24671	02/10/09
24671	04/02/09
24671	05/18/09
24671	06/25/09
;

proc sql;
create table want as
select a.id, max(evnt_d) as date format=yymmdd10.
from have1 as a left join have2 as b on a.id=b.id and a.key_d >= b.evnt_d
group by a.id;
select * from want; quit;
PG

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 3478 views
  • 1 like
  • 2 in conversation