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 - if_else - nested conditions

Introduction

  • When a derivation needs three or more categories — say, under 20, 20-59, and 60+ — we can nest if/else calls instead of reaching for case_when.
  • SAS stacks if/else if/else clauses one after another in a DATA step.
  • Tidyverse nests if_else() inside the no slot of an outer if_else(). Base R does the same with ifelse().
  • For more than 3-4 branches, case_when() (tidyverse) becomes noticeably easier to read.
  • This lesson bands age into three categories and assigns the label to agegr1.
  • After running, rows fall into ">= 60 Years", "20 - < 60 Years", or "< 20 Years".

conditional logic - if_else - nested conditions