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 ↗
← SASnRRetain first value of a subject onto other records

Introduction

  • A common derivation in clinical data: capture the first score (baseline, first visit, first entry) for each subject and copy that value onto every other record of the same subject.
  • The output keeps the same number of rows as the input; each row just gains an extra column holding the subject's first-visit value.
  • SAS does this in a DATA step using RETAIN plus the first. automatic variable on the BY group.
  • Tidyverse uses group_by() + fill() (or the shorter first()) to push the first value down through the group.
  • Base R uses ave() with a custom function that always returns the first element of each group.
  • This lesson copies each subject's visit-1 score onto visits 2 and 3 so the result is easy to inspect.

Retain first value of a subject onto other records