BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8

 

data fruit;
  input up down sub_product fav $;
datalines;
2 5	apple	applepie
3 2 lemon	lemontart
4 0 melon	melonmilk
7 11 lemon	lemontart
19 33 apple	applejuice
run;

data box;
input up down anyy okok $;
3 6	app	appl
5 9 lem	lemo
4 0 mel	melilk
7 11 lem	lemotrt
21 33 appl	appleju


data new;
merge fruit box;
by up down;

I am merging two data sets by two variables how can I write it in terms of sql? should I use full join?   

8 REPLIES 8
Kurt_Bremser
Super User

I had to fix BOTH of your DATA steps. Please test your code before posting it, so we don't have to do it for you. See it as basic courtesy with regards to those who are intended to help you.

data fruit;
input up down sub_product $ fav $;
datalines;
2 5 apple applepie
3 2 lemon lemontart
4 0 melon melonmilk
7 11 lemon lemontart
19 33 apple applejuice
;

data box;
input up down anyy $ okok $;
datalines;
3 6 app appl
5 9 lem lemo
4 0 mel melilk
7 11 lem lemotrt
21 33 appl appleju
;

proc sql;
create table want as
  select
    coalesce(t1.up,t2.up) as up,
    coalesce(t1.down,t2.down) as down,
    t1.sub_product,
    t1.fav,
    t2.anyy,
    t2.okok
  from fruit t1 full join box t2
  on t1.up = t2.up and t1.down = t2.down
;
quit;

But always keep in mind that SQL behaves differently from a data step when you have a many-to-many relationship.

HeatherNewton
Quartz | Level 8

thanks, I tried but they are not merging..

anyway what does it mean to merge by two variables?

HeatherNewton
Quartz | Level 8

HeatherNewton_0-1647070120259.png

thanks I now get it the above in results, but what I get from output is the below which is a mixture of two tables if we look at the what is circled.. and also why the table is displayed in output, supposed all data set will show in output?

 

HeatherNewton_1-1647070278979.png

 

Kurt_Bremser
Super User

Your second picture shows the table view of the fruit dataset. The first looks like a PROC PRINT of dataset want, which is shown in the Results tab.

You must select the WANT dataset in the table viewer (since all three datasets were created during a single submit, all will show up there).

HeatherNewton
Quartz | Level 8

HeatherNewton_0-1647064818378.png

 

 

seem missing something.. fav column is showing okok

HeatherNewton
Quartz | Level 8
data fruit;
input up down sub_product $ fav $;
datalines;
2 5 apple applepie
3 2 lemon lemontart
4 0 melon melonmilk
7 11 lemon lemontart
19 33 apple applejuice
;

proc sort data=fruit;
run;

data box;
input up down anyy $ okok $;
datalines;
3 6 app appl
5 9 lem lemo
4 0 mel melilk
7 11 lem lemotrt
21 33 appl appleju
;

proc sort data=box;
run;
proc sql;
create table want as
  select
    coalesce(t1.up,t2.up) as up,
    coalesce(t1.down,t2.down) as down,
    t1.sub_product,
    t1.fav,
    t2.anyy,
    t2.okok
  from fruit t1 full join box t2
  on t1.up = t2.up and t1.down = t2.down
;
quit;

results:

HeatherNewton_0-1647067477216.png

here you go...

Kurt_Bremser
Super User

Your picture shows incoming dataset fruit, not the resulting table want.

 

PS to view the correct dataset, use the drop-down of the Output tab.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 726 views
  • 0 likes
  • 2 in conversation