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

Retain first value of a subject onto other records


Lesson Description
-
  • Sometimes, we want to work with the concept of "Retain first value of a subject onto other records" 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 fs;
    input usubjid visitnum score;
    datalines;
101 1 20
101 2 10
101 3 30
102 1 90
102 2 10
102 3 100
;
run;

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

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

*=============================================================================;
*Retain the first score onto all records;
*=============================================================================;

data fs01;
    set fs;
    by usubjid visitnum;

    
retain first_score;
    if first.usubjid then first_score = score;
run;
#==============================================================================;
#Retain first score;
#==============================================================================;

fs <- tribble(
  ~usubjid,~visitnum,~score,
  101,1,20,
  101,2,10,
  101,3,30,
  102,1,90,
  102,2,10,
  102,3,100
)

fs01 <- fs %>% 
  arrange(usubjid,visitnum) %>% 
  group_by(usubjid) %>% 
  mutate(
    seq = row_number(),
    first_score = if_else(seq==1,score,NA)
  ) %>% 
  fill(first_score,.direction="down")
fs <- data.frame(
  usubjid = c(101, 101, 101, 102, 102, 102),
  visitnum = c(1, 2, 3, 1, 2, 3),
  score = c(20, 10, 30, 90, 10, 100)
  , stringsAsFactors = FALSE
)

fs01 <- fs[order(fs$usubjid, fs$visitnum), ]

fs01$first_score <- ave(fs01$score, fs01$usubjid, FUN = function(x) x[1])
  • order() sorts by subject and visit.
  • ave(..., FUN = function(x) x[1]) carries the first score per subject.