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;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.