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 ↗
← SASnRTranspose data - wide to long

Introduction

  • The reverse of long-to-wide is wide-to-long — taking one row per subject with many measurement columns and stacking those columns into rows.
  • Long shape is what analytic tools usually want: one row per subject per measurement, with a column naming the measurement and another holding the value.
  • SAS uses proc transpose with by, var, and the name=/rename= options to control the new column names.
  • Tidyverse uses pivot_longer() from tidyr, specifying the columns to stack and the names of the new key/value columns.
  • Base R uses reshape() with direction = "long", providing varying, v.names, timevar, times, and idvar.
  • This lesson takes a WIDE dataset with one row per subject and columns HGB, ALT, AST, and pivots it into a LONG dataset with lbtestcd and lbstresn.

Transpose data - wide to long