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 two following datasets

demographics

groupIDagegender
A12354Female
A45632Female
A65635Male
B14545Female
B44447Male

events

IDtermserious
123fever1
123chills0
444heart attack1

 

What I would like to do is show the data by GROUP in demographics and total by group, total record ID from events and total 'serious' from events

 

want:

grouptotaltotal_eventstotal_serious
A321
B211

Here is what i have so far

 

proc sql;

select distinct a.group, count(a.ID) as total_participants, count(b.ID) as total_events

from demographics a

left join events b on a.ID = b.ID

group by a.group;

quit;

 

i am able to get the total participants and total events but not serious

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

See this:

data demo;
input group $ ID $ age gender $;
datalines;
A 123 54  Female
A 456 32  Female
A 656 35  Male
B 145 45  Female
B 444 47  Male
;

data events;
infile datalines dsd;
input ID $ term :$20. serious;
datalines;
123,fever,1
123,chills,0
444,heart attack,1
;

proc sql;
create table want as
  select
    a.group,
    count(distinct a.id) as total,
    count(b.term) as events,
    sum(b.serious) as serious
  from demo a left join events b
  on a.id = b.id
  group by a.group
;
quit;

proc print data=want noobs;
run;

Result:

group	total	events	serious
A	3	2	1
B	2	1	1

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User

See this:

data demo;
input group $ ID $ age gender $;
datalines;
A 123 54  Female
A 456 32  Female
A 656 35  Male
B 145 45  Female
B 444 47  Male
;

data events;
infile datalines dsd;
input ID $ term :$20. serious;
datalines;
123,fever,1
123,chills,0
444,heart attack,1
;

proc sql;
create table want as
  select
    a.group,
    count(distinct a.id) as total,
    count(b.term) as events,
    sum(b.serious) as serious
  from demo a left join events b
  on a.id = b.id
  group by a.group
;
quit;

proc print data=want noobs;
run;

Result:

group	total	events	serious
A	3	2	1
B	2	1	1

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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