BookmarkSubscribeRSS Feed
mgrasmussen
Quartz | Level 8

Dear SAS experts 

 

I would like to categorize my data based on cumulatative sums. I would like to compute cumulutative sums, but at the row when the sum reaches =>5, I would like the sum to start over. Moreover, I would like the data to be categorized somehow such that each "bout" of cumulative sums are categorized together. 

 

Given the example data below:

 

data example;
input value;
datalines;
2
3
6
2
6
2
;
run;

 

I would like the resulting dataset to look as such:

 

value     cumsum_var     Categorical_var

2            2                       1
3            5                       1       
6            6                       2
2            2                       3
6            8                       3
2            2                       .

 

Is this possible to do? I have not managed to find a solution yet. I am thinking that a cumulative sum variable must be created, but there may be a more efficient way of achieving the desired result.

 

I probably would be able to create the categorical variable if I could create the sum variable.

 

Thank you

 

 

4 REPLIES 4
Ksharp
Super User
data example;
input value;
datalines;
2
3
6
2
6
2
;
run;

data want;
 set example;
 cum+value;
 if cum>5 then do;cum=value;Categorical_var+1;end;
run;
mgrasmussen
Quartz | Level 8

Hey KSharp

 

Thanks for the code.

 

It appears that row 4 and 5 are categorized seperately when they should be in the same category? It appears that the code only works as intended in the first 2 rows.

 

Thank you

PaigeMiller
Diamond | Level 26

On row 2, the cumulative sum is >=5, but you don't change categorical_var. That seems to be different than what your text is saying.

--
Paige Miller
mgrasmussen
Quartz | Level 8

Dear PaigeMiller

 

True. I meant to say that the row when the cumulative sum reaches >=5 should be included in the row/rows which precede this row.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 536 views
  • 0 likes
  • 3 in conversation