I've the datasets as follows.
data have1; input Segment $ Expenses RC; datalines; 7183_AS1 1234.50 453.97 7183_AS2 654.78 325.98 ; run; data have2; input Segment $ Country $ Currency $; datalines; 7183_AS1 USA Dollar 7183_AS1 USA Dollar 7183_AS1 USA Dollar 7183_AS2 USA Dollar 7183_AS2 USA Dollar ; run;
Now I want to get the results as below. I tried with right join and inner join but it didn't worked either.
Segment Country Currency Expenses RC 7183_AS1 USA Dollar 1234.50 453.97 7183_AS1 USA Dollar 1234.50 453.97 7183_AS1 USA Dollar 1234.50 453.97 7183_AS2 USA Dollar 654.78 325.98 7183_AS2 USA Dollar 654.78 325.98
In summary I want all records from 'have2' plus the matching values from 'Expenses' and 'RC' variables from 'have1'. Any guidance?
If SQL,
data have1;
input Segment $ Expenses RC;
datalines;
7183_AS1 1234.50 453.97
7183_AS2 654.78 325.98
;
run;
data have2;
input Segment $ Country $ Currency $;
datalines;
7183_AS1 USA Dollar
7183_AS1 USA Dollar
7183_AS1 USA Dollar
7183_AS2 USA Dollar
7183_AS2 USA Dollar
;
run;
proc sql;
create table want as
select a.*,expenses,rc
from have2 a left join have1 b
on a.segment=b.segment;
quit;
If SQL,
data have1;
input Segment $ Expenses RC;
datalines;
7183_AS1 1234.50 453.97
7183_AS2 654.78 325.98
;
run;
data have2;
input Segment $ Country $ Currency $;
datalines;
7183_AS1 USA Dollar
7183_AS1 USA Dollar
7183_AS1 USA Dollar
7183_AS2 USA Dollar
7183_AS2 USA Dollar
;
run;
proc sql;
create table want as
select a.*,expenses,rc
from have2 a left join have1 b
on a.segment=b.segment;
quit;
What if one of the datasets has no matches? And are the segments unique in have1?
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.