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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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