FIRST. and LAST. are automatic SAS variables created during BY-group processing. They are commonly used to derive baseline records, endpoint records, first occurrences, last occurrences, and subject-level summaries. In R, similar functionality is achieved using group_by(), slice(), row_number(), and summarise().
FIRST.variable is 1 for the first observation in a BY group, while LAST.variable is 1 for the last observation in a BY group. In R, equivalent logic is typically implemented using group_by() combined with slice(1) or slice(n()).
proc sort data=ADLB; by USUBJID VISITNUM; run; data FIRST_REC; set ADLB; by USUBJID; if first.USUBJID; run;
library(dplyr) FIRST_REC <- ADLB %>% arrange(USUBJID, VISITNUM) %>% group_by(USUBJID) %>% slice(1) %>% ungroup()
FIRST. and LAST. processing is one of the most important Data Step concepts in clinical programming. It is frequently used to derive baseline values, endpoint values, first adverse events, last assessments, and subject-level summaries.
Keep only the first record for each subject.
USUBJID VISITNUM AVAL CDISC001 1 25 CDISC001 2 30 CDISC001 3 35 CDISC002 1 40 CDISC002 2 45
proc sort data=ADLB; by USUBJID VISITNUM; run; data FIRST_REC; set ADLB; by USUBJID; if first.USUBJID; run;
library(dplyr) FIRST_REC <- ADLB %>% arrange(USUBJID, VISITNUM) %>% group_by(USUBJID) %>% slice(1) %>% ungroup()
USUBJID VISITNUM AVAL CDISC001 1 25 CDISC002 1 40
📝 Note: FIRST.USUBJID identifies the first observation within each subject.
Keep the earliest record for each subject and laboratory parameter.
USUBJID LBTESTCD VISITNUM AVAL CDISC001 ALT 1 25 CDISC001 ALT 2 30 CDISC001 AST 1 40 CDISC001 AST 2 42 CDISC002 ALT 1 35 CDISC002 ALT 2 50
proc sort data=ADLB; by USUBJID LBTESTCD VISITNUM; run; data BASELINE; set ADLB; by USUBJID LBTESTCD; if first.LBTESTCD; run;
library(dplyr) BASELINE <- ADLB %>% arrange(USUBJID, LBTESTCD, VISITNUM) %>% group_by(USUBJID, LBTESTCD) %>% slice(1) %>% ungroup()
USUBJID LBTESTCD VISITNUM AVAL CDISC001 ALT 1 25 CDISC001 AST 1 40 CDISC002 ALT 1 35
📝 Note: A common clinical programming use case is deriving baseline records using FIRST. processing.
Keep only the last record for each subject.
USUBJID VISITNUM AVAL CDISC001 1 25 CDISC001 2 30 CDISC001 3 35 CDISC002 1 40 CDISC002 2 45
proc sort data=ADLB; by USUBJID VISITNUM; run; data LAST_REC; set ADLB; by USUBJID; if last.USUBJID; run;
library(dplyr) LAST_REC <- ADLB %>% arrange(USUBJID, VISITNUM) %>% group_by(USUBJID) %>% slice(n()) %>% ungroup()
USUBJID VISITNUM AVAL CDISC001 3 35 CDISC002 2 45
📝 Note: LAST.USUBJID identifies the final observation within each subject.
Keep the most recent record for each subject and laboratory parameter.
USUBJID LBTESTCD VISITNUM AVAL CDISC001 ALT 1 25 CDISC001 ALT 2 30 CDISC001 ALT 3 35 CDISC001 AST 1 40 CDISC001 AST 2 42 CDISC002 ALT 1 35 CDISC002 ALT 2 50
proc sort data=ADLB; by USUBJID LBTESTCD VISITNUM; run; data LAST_ASSESS; set ADLB; by USUBJID LBTESTCD; if last.LBTESTCD; run;
library(dplyr) LAST_ASSESS <- ADLB %>% arrange(USUBJID, LBTESTCD, VISITNUM) %>% group_by(USUBJID, LBTESTCD) %>% slice(n()) %>% ungroup()
USUBJID LBTESTCD VISITNUM AVAL CDISC001 ALT 3 35 CDISC001 AST 2 42 CDISC002 ALT 2 50
📝 Note: LAST. processing is commonly used to derive endpoint and latest assessment records.
Key Takeaway: • FIRST.variable identifies the first observation in a BY group • LAST.variable identifies the last observation in a BY group • slice(1) and slice_head(n=x) in R corresponds to FIRST. • slice(n()) and slice_tail(n=x) in R corresponds to LAST. • Common clinical uses include baseline and endpoint derivations