BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aaronh
Quartz | Level 8

Hello experts,

 

So I have a dataset X that I want to subset based on the key values stored in dataset Y:

 

proc sql;
create table test as
select x.*
from x, y
where x.key1 = y.key1 and 
x.key2 = y.key2; quit;

However, when the proc sql completes running, I noticed that TEST dataset had almost twice as many rows as X. So I am wondering why this is happening and what alternatives are there?

 

To give you an example of what I am trying to achieve: say X has 'apple' and 'banana' under key1, but Y only has 'banana' under key1, how do I make sure the subset of X only has banana for key1?

 

Thank you!

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

You must make sure that there are no duplicates of key1-key2 pairs in table y. Can be done with:

 

proc sql;
create table test as
select x.*
from x, (select distinct key1, key2 from y) as yy
where x.key1 = yy.key1 and  x.key2 = yy.key2;
quit;
PG

View solution in original post

2 REPLIES 2
PGStats
Opal | Level 21

You must make sure that there are no duplicates of key1-key2 pairs in table y. Can be done with:

 

proc sql;
create table test as
select x.*
from x, (select distinct key1, key2 from y) as yy
where x.key1 = yy.key1 and  x.key2 = yy.key2;
quit;
PG
aaronh
Quartz | Level 8
Thank you so much PG! I was thinking of using a data step merge, but would require a proc sort dedup as well as a lot of renaming and sorting, while this can be achieved in SQL in one step.

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
  • 2 replies
  • 1227 views
  • 1 like
  • 2 in conversation