DATA Step, Macro, Functions and more

Joining 3 tables in SAS SQL

Reply
Frequent Contributor
Posts: 76

Joining 3 tables in SAS SQL

[ Edited ]

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; 

 

Super User
Posts: 3,870

Re: Joining 3 tables in SAS SQL

[ Edited ]
Posted in reply to Agent1592
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; 
Ask a Question
Discussion stats
  • 1 reply
  • 96 views
  • 1 like
  • 2 in conversation