SAS (Base SAS)+
data LONG;
infile datalines dlm='|' dsd missover;
input usubjid : $8. lbtestcd : $8. lbstresn : BEST6.;
label ;
format ;
datalines4;
1001|HGB|13
1001|ALT|30
1001|AST|23
1002|HGB|12
1002|ALT|28
1002|AST|15
;;;;
run;
proc sort data=long ;
by usubjid;
run;
proc transpose data=long out=wide ;
by usubjid;
var lbstresn;
id lbtestcd;
run;
- PROC TRANSPOSE is the SAS workhorse for reshaping between long and wide formats.
- We always sort first:
proc sort data=long; by usubjid;— PROC TRANSPOSE expects the BY variables to be sorted. proc transpose data=long out=wide;names the input and output datasets.by usubjid;tells SAS to transpose separately within each subject — so each subject becomes a single row in WIDE.var lbstresn;names the column whose values will become the cell values in WIDE.id lbtestcd;names the column whose values will become the new column names — HGB, ALT, AST in this sample.- After running, WIDE has one row per
usubjidwith columns HGB, ALT, and AST carrying the originallbstresnvalues.