Announcement Icon Online training class for Clinical R programming batch starts on Monday, 02Feb2026. Click here for details.

Replicate PROC FORMAT Using Named Vectors in R


Lesson Description
-
  • Sometimes, we want to work with the concept of "Replicate PROC FORMAT Using Named Vectors in R" in a clear, repeatable way.
  • This lesson walks through a simple example and shows the key steps.
  • We will see one approach on how we can do it in SAS and R.
* Create format using PROC FORMAT;
proc format;
    value sexfmt 
    
1 = "Male" 
    
2 = "Female"
    ;
run;

data dm;
  input usubjid $ sex;
  datalines;
01 1
02 2
03 1
04 2
;
run;

* Apply the format and create labeled variable;
data dm_labeled;
    set dm;
    sexc = put(sex, sexfmt.);
run;
  • `proc format` defines a reusable mapping from coded values to labels.
  • The `value sexfmt` block assigns `1 = 'Male'` and `2 = 'Female'`.
  • `put(sex, sexfmt.)` uses the format to derive labels from codes and stores the result in a new variable `sexc`.
library(tidyverse)

# Sample tibble with coded values

dm <- tibble( usubjid = c("01", "02", "03", "04"), sex = c(1, 2, 1, 2) )

# Define named vector format

sex_fmt <- c("1" = "Male", "2" = "Female")

# Apply the format using mutate

dm_labeled <- dm %>%

mutate( sexc = sex_fmt[as.character(sex)] )
  • The dataset `dm` includes a numeric column `sex` with coded values (1, 2).
  • `sex_fmt` is a named vector that maps codes to labels: 1 = 'Male', 2 = 'Female'.
  • `mutate(sexc = sex_fmt[as.character(sex)])` performs the mapping similar to `put()` in SAS.
  • This creates a labeled variable `sexc` just like in the SAS example.
dm <- data.frame(
  usubjid = c("01", "02", "03", "04"),
  sex = c(1, 2, 1, 2)
  , stringsAsFactors = FALSE
)

sex_fmt <- c("1" = "Male", "2" = "Female")

dm_labeled <- dm

dm_labeled$sexc <- sex_fmt[as.character(dm_labeled$sex)]
  • A named vector maps codes to labels.
  • Indexing with as.character(code) applies the mapping.