BookmarkSubscribeRSS Feed
jerrylshen
Obsidian | Level 7

Hi, I'm trying to merge/join multiple tables together into, but retaining essentially all the info. I'm using SAS 9 and I'm open to any SAS/Proc SQL advice

 

Table 1

 

╔════════════════╦═════════╦═════════╦════════╦═══════════╗
║     status     ║ Message ║ Subject ║ Field1 ║   Forms   ║
╠════════════════╬═════════╬═════════╬════════╬═══════════╣
║       1        ║ aba     ║     001 ║ one    ║ "01 & 02" ║
║       1        ║ bab     ║     002 ║ one    ║ "01 & 02" ║
╚════════════════╩═════════╩═════════╩════════╩═══════════╝

Table 2

 

 

╔════════════════╦═════════╦═════════╦════════╦═══════════╗
║     status     ║ Message ║ Subject ║ Field2 ║   Forms   ║
╠════════════════╬═════════╬═════════╬════════╬═══════════╣
║       1        ║ aaa     ║     001 ║ two    ║ "01 & 02" ║
║       1        ║ vvv     ║     002 ║ two    ║ "01 & 02" ║
╚════════════════╩═════════╩═════════╩════════╩═══════════╝

Expected final output:

╔════════╦══════════╦═════════╦════════╦════════╦═══════════╗
║ status ║ Message  ║ Subject ║ Field1 ║ Field2 ║   Forms   ║
╠════════╬══════════╬═════════╬════════╬════════╬═══════════╣
║      1 ║ aba, aaa ║     001 ║ one    ║ two    ║ "01 & 02" ║
║      1 ║ bab, vvv ║     002 ║ one    ║ two    ║ "01 & 02" ║
╚════════╩══════════╩═════════╩════════╩════════╩═══════════╝

I've tried proc sql full join and right/left join, but my results end up being something like

 

proc sql;

CREATE TABLE t1 AS
SELECT *
FROM table1 full join table2 on table1.Subject = table2.Subject
            full join table3 on table1.Subject = table3.Subject
;
quit;

Result's status would only be based on table1's status
╔════════════════╦═════════╦═════════╦════════╦════════╦═══════════╗ ║ status ║ Message ║ Subject ║ Field1 ║ Field2 ║ Forms ║ ╠════════════════╬═════════╬═════════╬════════╬════════╬═══════════╣ ║ 1 ║ aba ║ 001 ║ one ║ ║ "01 & 02" ║ ║ 1 ║ bab ║ 002 ║ one ║ ║ "01 & 02" ║ ║ 1 ║ aaa ║ 001 ║ ║ two ║ "01 & 02" ║ ║ 1 ║ vvv ║ 002 ║ ║ two ║ "01 & 02" ║ ╚════════════════╩═════════╩═════════╩════════╩════════╩═══════════╝

Where am I going wrong with the joins here? Or how can I get the expected output?

 

Thanks!   

 

EDIT: fixed the Expected Final Output table. So, Message should concatenate while also merging. This is probably a lot more complicated now:/

 

6 REPLIES 6
novinosrin
Tourmaline | Level 20

It appears you want to APPEND rather than JOIN

 


data want;

set one two;

run;

 

jerrylshen
Obsidian | Level 7
Shoot sorry, I just edited my question, it's a bit different now where this answer wouldn't work 100%.
novinosrin
Tourmaline | Level 20

Hello @jerrylshen 

 


data one;
infile cards ;
input (status    Message  Subject  Field1    Forms  ) ( &:$10.); 
cards;
 1        aba          001  one     "01 & 02" 
 1        bab          002  one     "01 & 02" 
 ;


data two;
infile cards ;
input (status    Message  Subject  Field2    Forms  ) (& :$10.); 
cards;
1         aaa         001  two     "01 & 02" 
1        vvv          002  two    "01 & 02"
;


data want;
merge one two(rename=message=m);
by status;
message=catx(',',message,m);
drop m;
run;
novinosrin
Tourmaline | Level 20

If you want to use PROC SQL

 


data one;
infile cards ;
input (status    Message  Subject  Field1    Forms  ) ( &:$10.); 
cards;
 1        aba          001  one     "01 & 02" 
 1        bab          002  one     "01 & 02" 
 ;


data two;
infile cards ;
input (status    Message  Subject  Field2    Forms  ) (& :$10.); 
cards;
2         aaa         001  two     "01 & 02" 
2        vvv          002  two    "01 & 02"
;

proc sql;
create table want as
select * from one
outer union corr 
select * from two;
quit;
Tom
Super User Tom
Super User

Why PROC SQL?  Why not just use normal SAS code?

jerrylshen
Obsidian | Level 7
I'm open to either SAS/Proc SQL advices. I'm more familiar with SQL than SAS, but I'm willing to learn the SAS method of doing this

SAS Innovate 2025: Register Today!

 

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 6 replies
  • 1067 views
  • 1 like
  • 3 in conversation