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

Hi!

 

Hi!  I'm having issues summing by subject  and date.  I need the sum to reset to zero when encountering a new date (by subject).  Here is my code;

 

data want;
set have;
by patient date;

if first.patient and res ^= . then targetsum=res;

else if res ^= . then targetsum+res;
output;
end;

run;


Here a sample of output for one subject. you can see the totals are not resetting with new date.

 

02OCT2017 81 81
02OCT2017 22 103

02OCT2017 39 142

11DEC2017 93 235
11DEC2017 23 258
11DEC2017 57 315

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

It's not clear that PROC SUMMARY does what you need.  It will get you the total for each subject/date, but it will not give you a "running total" for all observations so far.  Here is how you would modify your DATA step to do that:

 

data want;

set have;

by patient date;

if first.date then targetsum=0;

targetsum + res;

run;

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

 

proc summary data=have;

    class patient date;

    var res;

    output out=want sum=;

run;

 

--
Paige Miller
Reeza
Super User

As indicated, don't use a data step, use a PROC designed to summarize the data.

If you need other variables included, look at PROC SUMMARY which can support adding in other variables in the output.

 

https://github.com/statgeek/SAS-Tutorials/blob/master/proc_means_basic.sas
Astounding
PROC Star

It's not clear that PROC SUMMARY does what you need.  It will get you the total for each subject/date, but it will not give you a "running total" for all observations so far.  Here is how you would modify your DATA step to do that:

 

data want;

set have;

by patient date;

if first.date then targetsum=0;

targetsum + res;

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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