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!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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