Hi,
How can I merge these two datasets?
id | name |
1 | stallone |
2 | bond |
3 | perry |
4 | gomez |
name | date |
stallone | 2009 |
bond | 2013 |
Another way
data want;
if _N_=1 then do;
dcl hash h(dataset:'two');
h.definekey('id');
h.definedata('date');
h.definedone();
end;
set one;
date=.;
rc=h.find();
run;
One way
data one;
input id name $;
datalines;
1 stallone
2 bond
3 perry
4 gomez
;
data two;
input name $ date;
datalines;
stallone 2009
bond 2013
;
proc sql;
create table want as
select one.*, two.date
from one left join two
on one.name=two.name
order by id;
quit;
Result:
id name date 1 stallone 2009 2 bond 2013 3 perry . 4 gomez .
Another way
data want;
if _N_=1 then do;
dcl hash h(dataset:'two');
h.definekey('id');
h.definedata('date');
h.definedone();
end;
set one;
date=.;
rc=h.find();
run;
Finally, the "classic" data step method:
proc sort data=one;
by name;
run:
proc sort data=two;
by name;
run;
dara want;
merge
one (in=in_one)
two
;
by name;
if in_one;
run;
proc sort data=want;
by id;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.