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

I have two data sets A and B that contain a list of ID values. I would like to create data set C that has all ID values present in data set A except those that are also present in data set B. This works unless I have a duplicate record in A that is also present in B (ID value 3 here). The example produces data set C that has ID values 1, 3, and 5. How do I ensure I get a data set C with only ID values 1 and 5?

 

data a; input id; cards;
1
2
3
3
5
6
;
run;

data b; input id; cards;
2
3
4
6
6
;
run;

proc sql;
create table c as
select id from a
except all
select id from b;
quit;

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Did you try

proc sql;
   create table c as
   select id from a
   except 
   select id from b
   ;
quit;

when you are using All with except then you get "left overs" when there are more of a value in the first set than the second.

View solution in original post

3 REPLIES 3
smantha
Lapis Lazuli | Level 10
proc sql;

create table c

as select a.*

from a  a where a.id not in (select distinct id from b);

quit;
ballardw
Super User

Did you try

proc sql;
   create table c as
   select id from a
   except 
   select id from b
   ;
quit;

when you are using All with except then you get "left overs" when there are more of a value in the first set than the second.

PGStats
Opal | Level 21

When you add ALL to UNION, INTERSECT or EXCEPT SQL set operations, you state that you want to skip a final scan of the resulting table to remove duplicate entries. It speeds up the operation, but you should only use it when the input tables already contain distinct records or you actually want to keep duplicates.

 

Examples of ALL processing:

data a; input id; cards;
1
2
3
3
5
6
7
7
7
;
run;

data b; input id; cards;
2
3
4
6
6
7
7
;
run;

proc sql;
select id from a
except all
select id from b;
select id from a
union all
select id from b;
select id from a
intersect all
select id from b;
quit;
PG

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
  • 3 replies
  • 460 views
  • 3 likes
  • 4 in conversation