BookmarkSubscribeRSS Feed
Jhpark1222
Calcite | Level 5

Hi I am currently struggling to get a cumulative 'decayed' sum of variables.

For example, if variable y has value like:

 

day  y

1      4

2      3

3      2

4      6

5      1

6      2

 

i want to get a variable 'desumy', by each person (let's assume that example that i brought is from a single person),  assuming that decaying rate is 0.5 in a day:

 

day  y   desumy

1      4       0

2      3      4*0.5

3      2      4*0.5*0.5 + 3*0.5

4      6      4*0.5*0.5*0.5 + 3*0.5*0.5

5      1      4*0.5*0.5*0.5 + 3*0.5*0.5

6      2

 

i figured out that all i have to do is to create variable 'desumy' that satisfies

desumy_t = (desumy_t-1 + y_t-1)*0.5, but i am struggling to make a code implementing what i want.

 

Thank you.

6 REPLIES 6
Ksharp
Super User

You want this ?

 

data have;
input day  y;
cards;
1      4
2      3
3      2
4      6
5      1
6      2
;

data want;
 set have;
 retain  lag_desumy 0;
 desumy=sum(lag_desumy,lag(y))*0.5;
 lag_desumy=desumy;
 drop lag_desumy;
run;
Jhpark1222
Calcite | Level 5

Thanks a lot for your reply.

I am trying to use this in proc nlmixed in order to estimate maximum likelihood, but in proc nlmixed, 'retain ' seems to not work.

0.5 in my original question was decay rate phro, which is a parameter that needs to be estimated through maximum likelihood estimation process.

is there any advice?

Tom
Super User Tom
Super User

Just use retain and lag().  It is even easier if you use a sum statement as then the target variable of the sum statement is automatically retained.

data want;
  set have;
  desumy + lag(y)*0.5 ;
run;
Jhpark1222
Calcite | Level 5

Thanks a lot for your reply.

I am trying to use this in proc nlmixed in order to estimate maximum likelihood, but in proc nlmixed, 'retain ' seems to not work.

0.5 in my original question was decay rate phro, which is a parameter that needs to be estimated through maximum likelihood estimation process.

is there any advice?

SteveDenham
Jade | Level 19

Not real sure this approach will work, but you might try arranging your data  so that each desumy and y are read in as input, and desumy_t is a dependent variable.  Then you could fit this

 

desumy_t = (desumy_t-1 + y_t-1)*prho;

 

assuming some sort of count related distribution (poisson, negbin or generalized poisson).

 

SteveDenham

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