I have two dataset, I want to do like that:
Data 1
ID | Y |
1 | 10 |
2 | 20 |
Data 2
ID | Y |
1 | 100 |
1 | 200 |
2 | 300 |
3 | 100 |
4 | 500 |
Wanted Data 3 (based on data 1 ID)
ID | Y |
1 | 10 |
1 | 100 |
1 | 200 |
2 | 20 |
2 | 300 |
I used the code:
data data3;
merge data1(in=a) data2;
by id;
if a=1;
run;
But the y of data 1 with same ID will be overwrite, so how should I create data3?
Thank you very much.
It appears you don't want to merge but to interleave the data sets - but then only keep rows with id's that exist in your first source table.
data have1;
input id y$;
datalines;
1 10
2 20
;
data have2;
input id y$;
datalines;
1 100
1 200
2 300
3 100
4 500
;
data want;
set have1 (in=in1) have2;
by id;
retain keep_id;
if first.id then keep_id=in1;
if keep_id=1;
drop keep_id;
run;
proc print data=want;
run;
It appears you don't want to merge but to interleave the data sets - but then only keep rows with id's that exist in your first source table.
data have1;
input id y$;
datalines;
1 10
2 20
;
data have2;
input id y$;
datalines;
1 100
1 200
2 300
3 100
4 500
;
data want;
set have1 (in=in1) have2;
by id;
retain keep_id;
if first.id then keep_id=in1;
if keep_id=1;
drop keep_id;
run;
proc print data=want;
run;
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.