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?
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.