BookmarkSubscribeRSS Feed
Timbim
Obsidian | Level 7

Hi there,

 

I have two tables to merge and whilst I know how to merge using both SAS data step and Proc Sql, I prefer to use proc sql however the problem I am facing is how to  keep all columns for the first table and only specific columns for the second table.

 

It should be

 

Proc Sql;

Create table Accounts as

Select * from table 1

Select a, b,c  from table 2

left join  table 2 on ( table1.a=table2.a);

quit;

 

I truly appreciate your help.

 

Kind regards,

Mags

1 REPLY 1
Reeza
Super User

Please post your code using a code box. In the Rich Text Editor it's the 6/7th icons (notebook or i). This makes it easier to copy/paste, doesn't introduce emoticons and its more legible. 

 

You use * as a wildcard to reference all columns. To reference all columns in a specific table list it as either 'TableName.*' or 'Alias.*'.

Typically I use a table alias so I don't have to type out the full name.  t1/t2/tN is common. You can also use this convention on the join conditions and elsewhere in the query. See example below:

 

Proc Sql;

Create table Accounts as
Select t1.*, t2.ID, t2.Age
from table1 as t1 /*alias is t1 for table1*/
left join  table2 as t2 /*alias is t2 for table2*/
on t1.a=t2.a;

quit;

@Timbim wrote:

Hi there,

 

I have two tables to merge and whilst I know how to merge using both SAS data step and Proc Sql, I prefer to use proc sql however the problem I am facing is how to  keep all columns for the first table and only specific columns for the second table.

 

It should be

 

Proc Sql;

Create table Accounts as

Select * from table 1

Select a, b,c  from table 2

left join  table 2 on ( table1.a=table2.a);

quit;

 

I truly appreciate your help.

 

Kind regards,

Mags


 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 525 views
  • 0 likes
  • 2 in conversation