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.
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;
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;
I still get stuck trying to make things more complicated than they need to be 😞
Thanks! That should work fine.
Good reference. Thanks!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.