Hi Experts,
I want to left join two tables (table A and table B).
Table A
Date_ID | ID | Flag |
20190202 | 14715 | Y |
20190203 | 14715 | Y |
20190204 | 14715 | Y |
20190202 | 888 | Y |
20190203 | 888 | Y |
Table B
Date_ID | ID | CID |
20190202 | 14715 | 222 |
20190203 | 14715 | 222 |
20190204 | 14715 | 222 |
20190202 | 14715 | 333 |
20190203 | 14715 | 333 |
20190204 | 14715 | 333 |
20190202 | 1295128 | 444 |
20190203 | 1295128 | 444 |
20190204 | 1295128 | 444 |
Code I'm using
data w(drop=rc);
if _N_ = 1 then do;
dcl hash h(dataset : "a",hashexp : 20);
h.definekey("ID","DATE_ID");
h.definedata("ID","Flag");
h.definedone();
end;
set b;
if 0 then set a;
rc = h.find(key : ID, key : DATE_ID);
run;
Table W (What I am getting)
Date_ID | ID | CID | Flag |
20190202 | 14715 | 222 | Y |
20190203 | 14715 | 222 | Y |
20190204 | 14715 | 222 | Y |
20190202 | 14715 | 333 | Y |
20190203 | 14715 | 333 | Y |
20190204 | 14715 | 333 | Y |
20190202 | 1295128 | 444 | Y |
20190203 | 1295128 | 444 | Y |
20190204 | 1295128 | 444 | Y |
Table W (what I am expecting)
Date_ID | ID | CID | Flag |
20190202 | 14715 | 222 | Y |
20190203 | 14715 | 222 | Y |
20190204 | 14715 | 222 | Y |
20190202 | 14715 | 333 | Y |
20190203 | 14715 | 333 | Y |
20190204 | 14715 | 333 | Y |
20190202 | 1295128 | 444 | |
20190203 | 1295128 | 444 | |
20190204 | 1295128 | 444 |
Please help me correct error in my code.
Thanks in advance.
Try this
data w(drop=rc);
if _N_ = 1 then do;
dcl hash h(dataset : "a",hashexp : 20);
h.definekey("ID","DATE_ID");
h.definedata("ID","Flag");
h.definedone();
end;
set b;
if 0 then set a;
call missing(flag);
rc = h.find(key : ID, key : DATE_ID);
run;
Try this
data w(drop=rc);
if _N_ = 1 then do;
dcl hash h(dataset : "a",hashexp : 20);
h.definekey("ID","DATE_ID");
h.definedata("ID","Flag");
h.definedone();
end;
set b;
if 0 then set a;
call missing(flag);
rc = h.find(key : ID, key : DATE_ID);
run;
Anytime
Instead of
if 0 then set a;
set the required attributes (length of flag) in the _n_ = 1 block.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.