SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ModeratelyWise
Obsidian | Level 7

Hello all,

So I have a dataset of clients that is made up of multiple episode and the episodes are made up of multiple events.  I want to sum the total days a client spent during a particular episode (i.e. sum all the days for each event that occurred during the episode).

 

Client_IDEpisode_IDEvent_IDEvntDays1EvntDays2EvntDays3EvntDays4EvntDays5
00100100160000
00100100206000
00100200300002
00200300430000
00200300500080
00200300608000
00200400720000
00200500800500

 

EvntDays1, EvntDays2, etc are the number of days spent in a particular event placement and there will be only one non-zero column per row.  I want the end results to look like this:

 

Client_IDEpisode_IDTotal_Days
001

001

12
0010022
00200319
0020042
0020055

 

Does that make sense?  Let me know what you think.

 

Best,

 

Marc

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

Try:

data WANT 

  set HAVE;

  by Client_ID Episode_ID;

  if first.Episode_ID then Total_Days=0;

  Total_Days+sum(of EvntDays:);

  if last.Episode_ID;

run;

Untested as no data was supplied in usable form.

 

View solution in original post

5 REPLIES 5
ChrisNZ
Tourmaline | Level 20

Try:

data WANT 

  set HAVE;

  by Client_ID Episode_ID;

  if first.Episode_ID then Total_Days=0;

  Total_Days+sum(of EvntDays:);

  if last.Episode_ID;

run;

Untested as no data was supplied in usable form.

 

ModeratelyWise
Obsidian | Level 7

Thanks @ChrisNZ .  This worked! So did @Ksharp's code.  I just went in sequence.  

Ksharp
Super User
UNTESTED CODE:

proc sql;
create table want as
select Client_ID ,Episode_ID ,
sum(EvntDays1)+sum(EvntDays2)+sum(EvntDays3)+sum(EvntDays4)+sum(EvntDays5)
as Total_Days
from have
group by Client_ID ,Episode_ID;
quit;
maguiremq
SAS Super FREQ

This is the reusable form that @ChrisNZ is talking about:

data have;
infile datalines truncover;
input Client_ID	$ Episode_ID $	Event_ID $	EvntDays1	EvntDays2	EvntDays3	EvntDays4	EvntDays5;
datalines;
001	001	001	6	0	0	0	0
001	001	002	0	6	0	0	0
001	002	003	0	0	0	0	2
002	003	004	3	0	0	0	0
002	003	005	0	0	0	8	0
002	003	006	0	8	0	0	0
002	004	007	2	0	0	0	0
002	005	008	0	0	5	0	0
;

There are probably better resources, but here is one.

 

Both @ChrisNZ's and @Ksharp 's solutions work.

ModeratelyWise
Obsidian | Level 7

@maguiremq Thanks! I will make sure to format the data like this in the future. 

 

 

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 4785 views
  • 4 likes
  • 4 in conversation