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