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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.