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

The dataset looks something like this 

 

VENUE  SPORT

A           Hockey

B           Basketball

C           Softball 

B           Hockey 

C           Softball

A           Football 

 

I need to select only venues with one unique sport played there, so in this case Venue C would get selected because only one sport (softball) is played there. Any help would be greatly appreciated. Thanks.  

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

data have;
input VENUE $ SPORT : $20.;
cards;
A           Hockey
B           Basketball
C           Softball 
B           Hockey 
C           Softball
A           Football 
;
run;
proc sql;
create table want as
 select *
  from have
   group by venue
    having count(distinct sport)=1;
quit;

View solution in original post

7 REPLIES 7
Jagadishkatam
Amethyst | Level 16

proc sort data=have dupout=want nodup;

by venue sport;

run;

 

want dataset will have your expected output.

Thanks,
Jag
katsas1
Calcite | Level 5

Nodup deletes duplicated observations. I'm after retaining records with a single unique sport per venue and removing those with more than than one unique observation. Nodup retains venues with multiple unique observations. 

Jagadishkatam
Amethyst | Level 16

by data step

 

proc sort data=have nodupkey;
by venue sport;
run;

data want;
set have;
by venue sport;
retain cnt;
if first.venue then cnt=1;
else cnt=cnt+1;
if last.venue and cnt<=1;
run;
Thanks,
Jag
Astounding
PROC Star

Another approach:

 

proc freq data=have;

tables venue * sport / noprint out=combinations;

run;

 

proc freq data=combinations;

tables venue / noprint out=want (where=(count=1));

run;

 

proc print data=want;

run;

Reeza
Super User

SQL again offers a one step solution.

 

proc sql;

create table want as

select venue,  count(distinct sport) as num_sports

from have

group by venue

where calculated num_sports=1;

quit;

Ksharp
Super User

data have;
input VENUE $ SPORT : $20.;
cards;
A           Hockey
B           Basketball
C           Softball 
B           Hockey 
C           Softball
A           Football 
;
run;
proc sql;
create table want as
 select *
  from have
   group by venue
    having count(distinct sport)=1;
quit;

katsas1
Calcite | Level 5

This solution worked great. It's particularly useful if duplicate records need to be retain (like dollar amounts for each row). Thank you!

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
  • 7 replies
  • 2688 views
  • 3 likes
  • 5 in conversation