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

Retain minimum score upto the current observation


Lesson Description
-
  • Sometimes, we want to work with the concept of "Retain minimum score upto the current observation" 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.

*==============================================================================;
* Create the input dataset;
*==============================================================================;
data pmin;
    input usubjid visitnum score;
    datalines;
101 1 20
101 2 .
101 3 30
102 1 90
102 2 10
102 3 .
102 4 91
102 5 .
;
run;

*==============================================================================;
* Sort the dataset by usubjid and visitnum;
*==============================================================================;

proc sort data=pmin;
    by usubjid visitnum;
run;

*==============================================================================;
* Retain previous non-missing low score;
*==============================================================================;

data pmin01;
    set pmin;
    by usubjid visitnum;

    
retain minscore;

    
* Store the original score;
    orig_score = score;

    
if first.usubjid then minscore = .;

    
*Store minimum score and assign minscore when score is missing;
    
if not missing(score) then minscore = min(score,minscore);
    else score=minscore;
run;
#==============================================================================;
#Retain previous non-missing low score;
#==============================================================================;

pmin <- tribble(
  ~usubjid,~visitnum,~score,
  101,1,20,
  101,2,NA,
  101,3,30,
  102,1,90,
  102,2,10,
  102,3,NA,
  102,4,91,
  102,5,NA
)

pmin01 <- pmin %>%
  arrange(usubjid,visitnum) %>% 
  group_by(usubjid) %>% 
  mutate(
    orig_score = score,
    temp_score = if_else(is.na(score),Inf,score),
    minscore=cummin(temp_score),
    score = if_else(is.na(score),minscore,score)
  )
pmin <- data.frame(
  usubjid = c(101, 101, 101, 102, 102, 102, 102, 102),
  visitnum = c(1, 2, 3, 1, 2, 3, 4, 5),
  score = c(20, NA, 30, 90, 10, NA, 91, NA)
  , stringsAsFactors = FALSE
)

pmin01 <- pmin[order(pmin$usubjid, pmin$visitnum), ]

pmin01$orig_score <- pmin01$score

pmin01$minscore <- ave(pmin01$score, pmin01$usubjid, FUN = function(x) {
  temp <- ifelse(is.na(x), Inf, x)
  cummin(temp)
})

pmin01$score <- ifelse(is.na(pmin01$orig_score), pmin01$minscore, pmin01$score)
  • cummin() builds a running minimum; NA is treated as Inf.
  • ifelse() fills missing scores with the running minimum.