A little announcement. A lot to explore.

Happy learning!

Explore lessons, including SAS & R programs and practice data.

Search lessons

Type at least 3 characters to see matching lessons.

Open SAS ↗Open R ↗
← SASnRLEAD: Access the Next Row's Value

Introduction

  • Sometimes we need the value from the next row rather than the previous one — forward-looking comparisons like time to next event, next visit's score, or duration until a future milestone.
  • SAS does not have a native LEAD function. The common workaround is to sort the data in descending order and use LAG on the reversed data, or merge a dataset to itself with an offset key.
  • R tidyverse provides dplyr::lead(), which is the mirror image of lag() and works naturally inside group_by().
  • Base R builds the shifted vector with c(tail(x, -1), NA) — dropping the first element and appending NA at the end.
  • We use the same 3-subject, 3-visit dataset to show all three approaches side by side.

LEAD: Access the Next Row's Value