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.
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;
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?
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;
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?
Calling @StatDave @SteveDenham @lvm
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
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.