BookmarkSubscribeRSS Feed
Agent1592
Pyrite | Level 9

Dear SAS community, I am trying to create a more elegant code. I want to joint 3 tables have1, have2 and have3. The problem is that have1 table does not have id2 as a variable and first I have to link table have1 and have2. The code below is working but I would like to have a more elegant code because I may want to link more than 3 tables.

proc sql;
create table want1
        as select *,b.id2
        from have1 as a 
       left join have2  as b on a.id1=b.id1;
create table want2
   as select a.*, b.*
   from want1 as a left join have3 as c
   on a.id2=c.id2;

I tried something like this but it is not working because I do not have id2 in table have1. Could you please let me know what is the most concise code for this.

 

proc sql;
create table want
        as select *,b.id2
        from have1 as a 
       left join have2  as b on a.id1=b.id1
       left join have3 as c on a.id2=c.id2; quit; 

 

1 REPLY 1
SASKiwi
PROC Star
proc sql;
  create table want
  as select *
  from have1 as a 
  left join have2  as b on a.id1=b.id1
  left join have3 as c on b.id2=c.id2
  ; 
quit; 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1 reply
  • 4291 views
  • 1 like
  • 2 in conversation