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

Hi

 

Given Dataset:

Id Code

1 a

1 b

1 c

1 d

2 a

2 c

3 a

3 c

3 d

4 a

4 b

5 a

5 c

Data Want:

Id

2

5

 

I only need the (a,c) pair as output. But I am getting different Output.

proc sql;

create table want as

select *

case when code in ('a','c') then 1 end as check

from have where code in ('a','c');run;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

So if they have anything else they are off the list?

 select id from have
 group by id
 having min(code in ('a','c'))=1
 ;

Or do they also have to have both A and C?

proc sql ;
 select id from have
 group by id
 having min(code in ('a','c'))=1
   and count(distinct code)=2
 ;

View solution in original post

9 REPLIES 9
Tom
Super User Tom
Super User
Your output looks nothing like what your code (once typos are fixed) would produce.
What is the criteria for picking ID=2 and ID=5? Just have a or c (or even having both) would not result in just 2 and 5 from your example input.
Kalai2008
Pyrite | Level 9
Thanks for checking. My Criteria is to pick the ID's that have only this pair "a' and "c". ID 2 and 5 have the values "a" and "c".
Tom
Super User Tom
Super User

So if they have anything else they are off the list?

 select id from have
 group by id
 having min(code in ('a','c'))=1
 ;

Or do they also have to have both A and C?

proc sql ;
 select id from have
 group by id
 having min(code in ('a','c'))=1
   and count(distinct code)=2
 ;
Kalai2008
Pyrite | Level 9
Tom, Thank you. the second part of the code worked good. Appreciate it.
novinosrin
Tourmaline | Level 20

data have;
input Id Code $;
cards;

1 a

1 b

1 c

1 d

2 a

2 c

3 a

3 c

3 d

4 a

4 b

5 a

5 c
;

proc sql;
create table want as
select *
from have
group by id
having count(distinct code)=2 and sum(code in ('a','c'))=2;
quit;
Kalai2008
Pyrite | Level 9
Thank you, but it is not working good with my real data.
novinosrin
Tourmaline | Level 20

That means you really need to provide a better an representative sample. A Mock sample is fine, but has to be a representative sample

for instance, your values might be of mixed cases, or your values might have leading blanks and so forth

Stephen_c_tran
Calcite | Level 5

Another query:

proc sql;
create table want as
select  id
       ,count(*) as count_id
       ,sum(case when code = 'a' or code = 'c' then 1
                 else 0 end) as count_a_c
from have
group by id
having calculated count_id = calculated count_a_c
quit;

Kalai2008
Pyrite | Level 9
Thank you so much.. I will try this one.

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
  • 9 replies
  • 580 views
  • 4 likes
  • 4 in conversation