BookmarkSubscribeRSS Feed
femiajumobi1
Quartz | Level 8

I wish to merge two datasets: Data_A and Data_B using ID. I am only interested in observations in B with dates variable FDS_B that are within 14 days of each of the dates in A (FDS_A). The date format is MM/DD/YYYY.  How do I improve the code below if I were to include FDS_A and FDS_B?

 

Data want;
Merge  Data_A Data_B;
by ID;
run;

 

 

5 REPLIES 5
ballardw
Super User

Maybe something along the lines of

 

Proc sql;
   create table want a
   select a,*, b.fds_b
   from data_a as a
           left join
           data_b as b
           on a.id=b.id
   where abs(a.fds_a-b.fds_b) le 14
  ;
quit;

Maybe. If your Fds variables are actual SAS date variables.

You do not mention any other variables from Data_b. You would have to explicitly list them using the b.variable syntax. If the variable is the same name as one in Data_A do you expect it to be a different variable or replace the value of the variable in the Data_a.

 

I am a bit concerned about exactly what you mean by "each of the dates in A". That could be taken different ways.

If the above suggestion doesn't come close then show some example data from each of the two sets and what you expect for the result.

femiajumobi1
Quartz | Level 8

I really appreciate your prompt response. Thanks. I had a detour on the approach and will get back for further clarifications. 

mkeintz
PROC Star

 

  1. What do you want your output dataset to look like?
  2. Will it have any unmatched obs from a or b?
  3. What if multiple b's match a single a, or vice versa?
  4. Or worse yet, what if multiple b's match multiple a's?

Some of these questions will be simplified if we knew (for instance) that either a or b was limited to one obs per id.  Is that the case/

 

If you provided some sample data in the form of a working data step, I think you will get some suggested code to address the request.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
femiajumobi1
Quartz | Level 8
I really appreciate your prompt response. Thanks. I had a detour on the approach and will get back for further clarifications. 
tarheel13
Rhodochrosite | Level 12

proc sql with inexact matching.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 489 views
  • 2 likes
  • 4 in conversation