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 ↗
← SASnRReplicate PROC FORMAT Using Named Vectors in R

Introduction

  • Clinical data often travels as numeric codes — sex = 1/2, race = 1/2/3/4 — but reports and SDTM want the human-readable labels.
  • The mapping from code to label is reusable: define it once and apply it wherever the coded column appears.
  • SAS defines the mapping with proc format and applies it with put(code, fmt.) to derive a labelled column.
  • Tidyverse and base R both use the same trick: a named character vector, indexed by the string form of the code.
  • Named-vector lookups are fast, easy to read, and need no extra package — the closest R idiom to SAS formats.
  • This lesson maps Sex codes 1 and 2 to "Male" and "Female" and adds the label as a new column sexc.

Replicate PROC FORMAT Using Named Vectors in R