Announcement Icon Online training class for Clinical R programming batch starts on Monday, 02Feb2026. Click here for details.

Cumulative Sum, Mean, and Product


Lesson Description
-
  • Sometimes, we want to work with the concept of "Cumulative Sum, Mean, and Product" in a clear, repeatable way.
  • This lesson walks through a simple example and shows the key steps.
  • We will see one approach on how we can do it in SAS and R.

 

data scores;
    input id score;
    datalines;
1 10 
2 15 
3 20 
4 25 
5 30 
;
run;

data cumulative;
    set scores;
    retain cum_sum cum_prod cum_avg count;

    
if _n_ = 1 then do;
        cum_sum = score;
        cum_prod = score;
        count = 
1;
        cum_avg = score;
    
end;
    else do;
        cum_sum + score;
        cum_prod = cum_prod * score;
        count + 
1;
        cum_avg = cum_sum / count;
    
end;
run;
  • Calculates cumulative sum, product, and average using retained variables.
  • `cum_sum` is incremented with each score. `cum_prod` multiplies progressively.
  • `cum_avg` divides total sum by the count of rows seen so far.
library(tidyverse)

scores <- tribble(
  ~id, ~score,
  1, 10,
  2, 15,
  3, 20,
  4, 25,
  5, 30
)

cumulative <- scores %>%
  mutate(
    cum_sum = cumsum(score),
    cum_prod = cumprod(score),
    cum_avg = cum_sum / row_number()
  )
  • `cumsum()` computes the cumulative sum.
  • `cumprod()` computes the cumulative product.
  • `cum_avg` is derived by dividing the cumulative sum by row number.
scores <- data.frame(
  id = c(1, 2, 3, 4, 5),
  score = c(10, 15, 20, 25, 30)
  , stringsAsFactors = FALSE
)

cumulative <- scores

cumulative$cum_sum <- cumsum(cumulative$score)

cumulative$cum_prod <- cumprod(cumulative$score)

cumulative$cum_avg <- cumulative$cum_sum / seq_along(cumulative$score)
  • cumsum/cumprod compute running totals and products.
  • seq_along() provides the row index for a running average.