Got a data set merging problem:
Data Have1;
a1 | a2 | a3 |
. | 4 | 3 |
1 | . | . |
2 | 3 | 4 |
4 | 9 | 3 |
9 | 5 | 2 |
data have2;
a1 | b1 | b2 |
1 | 6 | 5 |
2 | 4 | . |
9 | 2 | 3 |
12 | 1 | 1 |
I want to merge data Have1 and Have2 in such a way that if the values of the variable a1 in Have1 match with values of a1 in Have2 then keep otherwise drop. For example I want the new set like:
a1 | a2 | a3 | b1 | b2 |
1 | . | . | 6 | 5 |
2 | 3 | 4 | 4 | . |
9 | 5 | 2 | 2 | 3 |
I would appreciate very much if any expert out there help me out to get the result.
Thank you very much.
Sijansap
data want;
merge have1(in=a) have2(in=b);
by a1;
if a and b;
run
Hello,
here is an alternative method :
data want;
if 0 then set have2;
if _n_=1 then do;
declare hash h1(dataset:'have2');
h1.definekey('a1');
h1.definedata(all:'y');
h1.definedone();
end;
set have1;
r1=h1.find(key:a1);
if r1=0;
drop r1;
run;
Thanks 🙂
And the SQL method:
proc sql;
create table want as
select
a.a1,
a.a2,
a.a3,
b.b1,
b.b2
from have1 a, have2 b
where a.a1 = b.a1;
quit;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.