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 - Concatenate character results into a single value in a group

Introduction

  • Some reports ask for all of a subject's results joined into a single string — for example, "Y;N;Y" for a subject with three visits — collapsing many rows into one.
  • The output has one row per subject, with a long character column holding the joined values.
  • SAS does this with RETAIN + CATX in a DATA step, then keeps only the last row of each subject.
  • Tidyverse uses group_by() %>% summarize(str_flatten(...)) to build the concatenated string in one step.
  • Base R uses aggregate() with a paste(..., collapse = ";") helper to achieve the same effect.
  • This lesson joins each subject's non-missing results into one semicolon-separated string; missing values are dropped from the join automatically.

Retain - Concatenate character results into a single value in a group