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

I have a dataset like this (below). The variable code takes 4 possible values

The ID variable is not unique. There can be an ID with 2, 3 or 4 codes, although it's rare with 3 and 4. I want to keep the non-unique IDs that have different codes (or conversely, remove the non-unique IDs with repeated codes).

 

What I have:

 

data have;
input Obs ID $12. code $;
cards;
1 AC0000037163 C1
2 AC0000037163 U1
3 BE0000037282 U1
4 BE0000037282 U2
5 CZE0000037693 C2
6 CZE0000037693 C2 7 FR0000037738 U2 8 FR0000037738 C2 ;

 

What I want:

 

data want;
input Obs ID $12. code $;
cards;
1 AC0000037163 C1
2 AC0000037163 U1
3 BE0000037282 U1
4 BE0000037282 U2
7 FR0000037738 U2 8 FR0000037738 C2 ;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
data have;
input Obs ID $12. code $;
cards;
1 AC0000037163 C1
2 AC0000037163 U1
3 BE0000037282 U1
4 BE0000037282 U2
5 CZE0000037693 C2
6 CZE0000037693 C2
7 FR0000037738 U2
8 FR0000037738 C2
;

proc sql;
   create table want as
   select *
   from have
   group by ID
   having count(distinct code) > 1
   ;
quit;

View solution in original post

1 REPLY 1
PeterClemmensen
Tourmaline | Level 20
data have;
input Obs ID $12. code $;
cards;
1 AC0000037163 C1
2 AC0000037163 U1
3 BE0000037282 U1
4 BE0000037282 U2
5 CZE0000037693 C2
6 CZE0000037693 C2
7 FR0000037738 U2
8 FR0000037738 C2
;

proc sql;
   create table want as
   select *
   from have
   group by ID
   having count(distinct code) > 1
   ;
quit;

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
  • 1 reply
  • 215 views
  • 0 likes
  • 2 in conversation