BookmarkSubscribeRSS Feed
marleeakerson
Calcite | Level 5

Hello,

 

I need some help with a proc SQL problem. Take the sample data below: 

 

Table 1

Code      Description 

1                  X

2                  Y

3                  Z

 

Table 2

Code              ID         

2                    a

3                    b

4                    c

 

So I want to pull all the observations from Table 2 that have a matching Code from Table 1 (so this would be Code 2 and Code 3). 

 

Is there a way to do this in SAS (I presume it is easiest with proc SQL but if it works with a data step that is great too) 

 

Thanks!

 

 

2 REPLIES 2
Reeza
Super User
Do you want other columns for Table2?

proc sql;
create table want as select * from table1 where code in (select code from table2);
quit;

proc sql;
create table want as select t1.*, t2.ID
from table1 t1 right join
table2 t2
on t1.code=t2.code;
quit;
Reeza
Super User
And just a quick note that the SQL documentation has many examples that come in useful.
https://documentation.sas.com/?docsetId=sqlproc&docsetTarget=n1ncn0pznd8wrln1tnp3xdxjz9xz.htm&docset...

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1008 views
  • 0 likes
  • 2 in conversation