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

Hello. This is a segment of a table with Name, month/years, value.

What is a straightforward way to get the cum sum to reset at the next Name?

The MONTHYEAR is sorted sequentially. Thanks!

 

HAVE:                                  WANTED CUMSUM COLUMN:

NAME    MONTHYEAR    VALUE      CUMSUM
Name1   06/2019                   2               2
Name1   07/2019                   0               2
Name1   08/2019                   5               7
Name1   09/2019                   6             13
Name1   10/2019                  1             14
Name2   10/2017                  1              1   
Name2   11/2017                  0              1   
Name2   12/2017                  2               4           
Name2   01/2018                  4               8      
Name2   02/2018                  1               9
Name3....etc.

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data have;
input NAME $    MONTHYEAR  $  VALUE;*      CUMSUM;
cards;
Name1   06/2019                   2               2
Name1   07/2019                   0               2
Name1   08/2019                   5               7
Name1   09/2019                   6             13
Name1   10/2019                  1             14
Name2   10/2017                  1              1   
Name2   11/2017                  0              1   
Name2   12/2017                  2               4           
Name2   01/2018                  4               8      
Name2   02/2018                  1               9
;

data want;
set have;
by name;
if first.name then CUMSUM=value;
else CUMSUM+value;
run;

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

data have;
input NAME $    MONTHYEAR  $  VALUE;*      CUMSUM;
cards;
Name1   06/2019                   2               2
Name1   07/2019                   0               2
Name1   08/2019                   5               7
Name1   09/2019                   6             13
Name1   10/2019                  1             14
Name2   10/2017                  1              1   
Name2   11/2017                  0              1   
Name2   12/2017                  2               4           
Name2   01/2018                  4               8      
Name2   02/2018                  1               9
;

data want;
set have;
by name;
if first.name then CUMSUM=value;
else CUMSUM+value;
run;
crawfe
Quartz | Level 8

I still get stuck trying to make things more complicated than they need to be 😞

Thanks! That should work fine.

Reeza
Super User
BY group processing does this in SAS.
See this page for examples and how it works. Novinosrin's code is likely what you need to answer the question. This explains how it works.
https://documentation.sas.com/?docsetId=lrcon&docsetTarget=p0xu93fy5eemkyn1p6mj5elses7j.htm&docsetVe...
crawfe
Quartz | Level 8

Good reference. Thanks!

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
  • 4 replies
  • 865 views
  • 1 like
  • 3 in conversation