SAS (Base SAS)+
data WIDE;
infile datalines dlm='|' dsd missover;
input usubjid : $4. HGB : best32. ALT : best32. AST : best32.;
label ;
format ;
datalines4;
1001|13|30|23
1002|12|28|15
;;;;
run;
proc sort data=wide;
by usubjid;
run;
proc transpose data=wide
out=long(rename=(col1=lbstresn))
name=lbtestcd ;
by usubjid;
var HGB ALT AST;
run;- PROC TRANSPOSE works in both directions — for wide-to-long the pattern uses
varto list the columns to stack andname=/rename=to control the new column names. - Sort first:
proc sort data=wide; by usubjid;— PROC TRANSPOSE expects BY variables to be sorted. proc transpose data=wide out=long(rename=(col1=lbstresn)) name=lbtestcd;names the input and the output, and tells SAS how to label the new key/value columns.name=lbtestcdrenames the automatic "_NAME_" column — the one that holds the old column names — tolbtestcd.rename=(col1=lbstresn)renames the automaticCOL1column (the one that holds the stacked values) tolbstresn.by usubjid;keeps the subject identifier fixed across the stacked rows, so each subject appears once per lab test.var HGB ALT AST;names the columns whose values we want to stack into the long layout.- After running, LONG has one row per
usubjidper lab test, with columnsusubjid,lbtestcd, andlbstresn.