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

I have the following dataset, what i would like to do see by each group and yearmonth, total number of records, and sum all the Types

 

patientIDgroupyearmonthType
1ABC2021011
2ABC2021021
3GHI2021021
4DEF2021011
5DEF2021010
6ABC2021021
7DEF2021020
8DEF2021020
9GHI2021021
10GHI2021010
11ABC2021010
12GHI2021010

 

Want:

groupyearmonthtotal_participantsType_1
ABC20210121
ABC20210222
DEF20210121
DEF20210220
GHI20210120
GHI20210222

 

I tried the following:

proc sql;
select distinct group, yearmonth, count(patientID) as total_participants, sum(Type) as Type
from test; quit;

 

but it wont' give me what i need

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Add a GROUP BY since you want your results grouped by group and yearmonth.

 

proc sql;
select distinct group, yearmonth, count(patientID) as total_participants, sum(Type) as Type
from test
group by group, yearmonth;
 quit;

@pacman94 wrote:

I have the following dataset, what i would like to do see by each group and yearmonth, total number of records, and sum all the Types

 

patientID group yearmonth Type
1 ABC 202101 1
2 ABC 202102 1
3 GHI 202102 1
4 DEF 202101 1
5 DEF 202101 0
6 ABC 202102 1
7 DEF 202102 0
8 DEF 202102 0
9 GHI 202102 1
10 GHI 202101 0
11 ABC 202101 0
12 GHI 202101 0

 

Want:

group yearmonth total_participants Type_1
ABC 202101 2 1
ABC 202102 2 2
DEF 202101 2 1
DEF 202102 2 0
GHI 202101 2 0
GHI 202102 2 2

 

I tried the following:

proc sql;
select distinct group, yearmonth, count(patientID) as total_participants, sum(Type) as Type
from test; quit;

 

but it wont' give me what i need


 

View solution in original post

1 REPLY 1
Reeza
Super User

Add a GROUP BY since you want your results grouped by group and yearmonth.

 

proc sql;
select distinct group, yearmonth, count(patientID) as total_participants, sum(Type) as Type
from test
group by group, yearmonth;
 quit;

@pacman94 wrote:

I have the following dataset, what i would like to do see by each group and yearmonth, total number of records, and sum all the Types

 

patientID group yearmonth Type
1 ABC 202101 1
2 ABC 202102 1
3 GHI 202102 1
4 DEF 202101 1
5 DEF 202101 0
6 ABC 202102 1
7 DEF 202102 0
8 DEF 202102 0
9 GHI 202102 1
10 GHI 202101 0
11 ABC 202101 0
12 GHI 202101 0

 

Want:

group yearmonth total_participants Type_1
ABC 202101 2 1
ABC 202102 2 2
DEF 202101 2 1
DEF 202102 2 0
GHI 202101 2 0
GHI 202102 2 2

 

I tried the following:

proc sql;
select distinct group, yearmonth, count(patientID) as total_participants, sum(Type) as Type
from test; quit;

 

but it wont' give me what i need


 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 418 views
  • 0 likes
  • 2 in conversation