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

Hi ,
I am trying to sum one variable as long as another remains constant. I want to cumulative sum dur as long as a is constant. when a changes the sum restarts. when a new id, the sum restarts.
id a dur
1 1 30
1 1 30
1 2 10
2 1 20
2 2 10
2 2 10
3 2 10
3 1 10

and I would like to do this:
id a dur sum
1 1 30 30
1 1 30 60
1 2 10 10
2 1 20 20
2 2 10 10
2 2 10 20
3 2 10 10
3 1 10 10

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@nataliavanessa0 wrote:
thank you @CarmineVerrell

But what if I don't want to combine all 1s and all 2s by ID, I just one to sum dur while a is constant but when it changes I want to restart the sum until it changes again?
something like this:

and I would like to do this:
id a dur sum
1 1 30 30
1 1 30 60
1 2 10 10
1 1 10 10

Thanks.

That would be the NOTSORTED option on the BY statement.

Data outdata;
   set indata;
   by notsorted id a;
   if first.a=1 then cumdur=0;
   cumdur+dur;
run;

Notsorted aplies to all the the variables on the by statement regardless of the position it appears in the By statement.

 

View solution in original post

3 REPLIES 3
CarmineVerrell
SAS Employee

You would need to sort your data and then use a data step.

Here is an example.

 

data indata;
infile cards;
input id a dur;
cards;
1 1 30
1 1 30
1 2 10
2 1 20
2 2 10
2 2 10
3 2 10
3 1 10
;
run;

 

 

Proc sort data=indata ;
by id a;
run;

 

Data outdata;
set indata;
by id a;
if first.a=1 then cumdur=0;
cumdur+dur;
if last.a;
run;

nataliavanessa0
Calcite | Level 5
thank you @CarmineVerrell

But what if I don't want to combine all 1s and all 2s by ID, I just one to sum dur while a is constant but when it changes I want to restart the sum until it changes again?
something like this:

and I would like to do this:
id a dur sum
1 1 30 30
1 1 30 60
1 2 10 10
1 1 10 10

Thanks.
ballardw
Super User

@nataliavanessa0 wrote:
thank you @CarmineVerrell

But what if I don't want to combine all 1s and all 2s by ID, I just one to sum dur while a is constant but when it changes I want to restart the sum until it changes again?
something like this:

and I would like to do this:
id a dur sum
1 1 30 30
1 1 30 60
1 2 10 10
1 1 10 10

Thanks.

That would be the NOTSORTED option on the BY statement.

Data outdata;
   set indata;
   by notsorted id a;
   if first.a=1 then cumdur=0;
   cumdur+dur;
run;

Notsorted aplies to all the the variables on the by statement regardless of the position it appears in the By statement.

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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