Hi I have two datasets that I would like to merge by id, but when I do that, I have Dataset B overwriting the diagnosis date, cluster and servdate in Dataset A, how do I prevent this?
Dataset A:
ID | Diagnosis Date | Cluster | Servdate |
1 | Jan 1 1998 | Urban | Aug 3 1998 |
1 | Jan 1 1998 | Urban | Aug 15 1998 |
2 | Feb 3 2000 | Urban | April 9 2000 |
Dataset B:
ID | Diagnosis Date | Cluster | Servdate |
1 | Jan 1 1998 | Rural | Sept 1 1998 |
1 | Jan 1 1998 | Rural | Sept 3 1998 |
2 | Feb 3 2000 | Rural | May 7 2000 |
worked out example using your sample
data a;
infile cards truncover;
input (ID DiagnosisDate Cluster Servdate) (& $15.);
cards;
1 Jan 1 1998 Urban Aug 3 1998
1 Jan 1 1998 Urban Aug 15 1998
2 Feb 3 2000 Urban April 9 2000
;
data b;
infile cards truncover;
input (ID DiagnosisDate Cluster Servdate) (& $15.);
cards;
1 Jan 1 1998 Rural Sept 1 1998
1 Jan 1 1998 Rural Sept 3 1998
2 Feb 3 2000 Rural May 7 2000
;
data want;
set a b;
by id;
run;
Decide which ones you want to keep and drop the rest using dataset options
This is what I need:
ID | Diagnosis Date | Cluster | Servdate |
1 | Jan 1 1998 | Urban | Aug 3 1998 |
1 | Jan 1 1998 | Urban | Aug 15 1998 |
1 | Jan 1 1998 | Rural | Sept 1 1998 |
1 | Jan 1 1998 | Rural | Sept 3 1998 |
2 | Feb 3 2000 | Urban | April 9 2000 |
2 | Feb 3 2000 | Rural | May 7 2000 |
What would you like the result to be?
Seeing what you posted as the result, change the MERGE statement to a SET statement.
Thank you!
data want;
set a b;
by id;
run
What you need is interleave and not a merge
worked out example using your sample
data a;
infile cards truncover;
input (ID DiagnosisDate Cluster Servdate) (& $15.);
cards;
1 Jan 1 1998 Urban Aug 3 1998
1 Jan 1 1998 Urban Aug 15 1998
2 Feb 3 2000 Urban April 9 2000
;
data b;
infile cards truncover;
input (ID DiagnosisDate Cluster Servdate) (& $15.);
cards;
1 Jan 1 1998 Rural Sept 1 1998
1 Jan 1 1998 Rural Sept 3 1998
2 Feb 3 2000 Rural May 7 2000
;
data want;
set a b;
by id;
run;
Or if your datasets are not sorted by id , you could use proc sql
proc sql;
create table want as
select * from a
union all
select * from b
order by id, DiagnosisDate,Servdate;
quit;
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.