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

I'm having difficulty with a pretty easy procedure. I'm trying to get the number of records based on groups (O, IV and Combination of O+IV). 

I have the following data.

data have;
infile datalines delimiter=",";
input ID, recordkey, route $;
datalines;
1,1,IV
1,2,IV
1,3,IV
2,4,O
2,5,IV
2,6,IV
3,7,O
3,8,O
;

How would I get a count of unique IDs that are O alone, IV alone and combination of O and IV? 

 

I want to get the following results:

Oral=1

IV=1

Combo=1

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
infile datalines delimiter=",";
input ID recordkey route $;
datalines;
1,1,IV
1,2,IV
1,3,IV
2,4,O
2,5,IV
2,6,IV
3,7,O
3,8,O
;

proc sql;
create table want as
select 'Oral' as name,count(distinct id) as n from have group by id having sum(route='O')=count(*)
union
select 'IV' as name,count(distinct id) from have group by id having sum(route='IV')=count(*)
union
select 'Combo' as name,count(distinct id) from have group by id having count(distinct route)>1;
quit;

View solution in original post

7 REPLIES 7
novinosrin
Tourmaline | Level 20

do these 

3,7,O
3,8,0

 belong to O?  

 

And count of distinct ids for IV seems 2 , are you sure it's IV=3????

tm28
Fluorite | Level 6

Sorry, I updated the error in the initial code on the last line.

novinosrin
Tourmaline | Level 20

Hi @tm28   a very interesting problem.  Certainly not that simple for me as it is probably for you

 

data have;
infile datalines delimiter=",";
input ID recordkey route $;
datalines;
1,1,IV
1,2,IV
1,3,IV
2,4,O
2,5,IV
2,6,IV
3,7,O
3,8,O
;

proc sql;
create table want as
select grp, count(distinct id) as count
from
(select *,ifc(count(distinct route) =1 ,route,'combo') as grp from have group id)
group by grp;
quit;
tm28
Fluorite | Level 6

This works great for the most part, except for O route.. It should have a count of 1, not 2.

novinosrin
Tourmaline | Level 20

@tm28  Here is my test:

 

data have;
infile datalines delimiter=",";
input ID recordkey route $;
datalines;
1,1,IV
1,2,IV
1,3,IV
2,4,O
2,5,IV
2,6,IV
3,7,O
3,8,O
;

proc sql;
create table want as
select grp, count(distinct id) as count
from
(select *,ifc(count(distinct route) =1 ,route,'combo') as grp from have group id)
group by grp;
quit;

proc print noobs;run;
grp count
IV 1
O 1
combo 1
Ksharp
Super User
data have;
infile datalines delimiter=",";
input ID recordkey route $;
datalines;
1,1,IV
1,2,IV
1,3,IV
2,4,O
2,5,IV
2,6,IV
3,7,O
3,8,O
;

proc sql;
create table want as
select 'Oral' as name,count(distinct id) as n from have group by id having sum(route='O')=count(*)
union
select 'IV' as name,count(distinct id) from have group by id having sum(route='IV')=count(*)
union
select 'Combo' as name,count(distinct id) from have group by id having count(distinct route)>1;
quit;
tm28
Fluorite | Level 6

This got to what I wanted (minus the union part of the code), thanks so much! 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1248 views
  • 0 likes
  • 3 in conversation