简体   繁体   中英

How to calculate cumulative product in SAS?

I need to create a variable that takes the product of the values of all prior values and including the one in the current obs.

data temp;
input time cond_prob;
datalines;    
    1 1
    2 0.2
    3 0.3
    4 0.4
    5 0.6    
;
run;

Final data should be:

1 1
2 0.2 (1*0.2)
3 0.06 (0.2* 0.3)
4 0.024 (0.06 * 0.4
5 0.0144 (0.024 *0.6)

This seems like a simple code but I can't get it to work. I can do cumulative sums but cumulative product is not working when using the same logic.

Use the RETAIN functionality. For the first record I set it to a value of 1 because anything multiplied by 1 will stay the same.

data want;
set temp;

retain cum_product 1;

cum_product = cond_prob * cum_product;

run;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM