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 ↗
← SASnRconditional logic - case_when - .default and missing values

Introduction

  • Many derivations are multi-branch: if X then A, else if Y then B, else C. A typical example is age banding — under 18, 18 to 59, 60 and over.
  • SAS handles this with if-else if-else inside a DATA step. Tidyverse offers case_when() from dplyr, which reads top-to-bottom like a truth table. Base R chains ifelse() calls.
  • We also have to think about missing values — they can be handled explicitly (a dedicated branch) or default to the final fallback.
  • This lesson builds an age band using both approaches: one where missing ages fall through to the default branch, and one where they get their own "Age Missing" category.
  • After running, dm1 has four rows with agegrp values derived from the age, and dm2 treats the missing age as a separate category.

conditional logic - case_when - .default and missing values