When working with data, we frequently want to restructure the "long" data to "wide" data.
We need programming features to handle this.
Let us assume that we have some lab data collected in the structure of "one record per subject per lab test" as shown below.
usubjid |
lbtestcd |
lbstresn |
1001 |
HGB |
13 |
1001 |
ALT |
30 |
1001 |
AST |
23 |
1002 |
HGB |
12 |
1002 |
ALT |
28 |
1002 |
AST |
15 |
And, let us say that we want to restructure this data such that there is "one record per subject" and the the test results of each lab test are to be presented as separate columns as shown below.
usubjid |
HGB |
ALT |
AST |
1001 |
13 |
30 |
23 |
1002 |
12 |
28 |
15 |
There are multiple ways of obtaining this result in both SAS and R. Below is one such approach.
data long;
infile cards dlm='|' truncover;
input usubjid$ lbtestcd$ lbstresn;
format lbstresn best6.;
cards;
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;
#Load required libraries
library(tidyverse)
library(haven)
long<-tribble(
~usubjid,~lbtestcd,~lbstresn,
"1001","HGB",13,
"1001","ALT",30,
"1001","AST",23,
"1002","HGB",12,
"1002","ALT",28,
"1002","AST",15,
)
wide<-pivot_wider(long,id_cols=usubjid,values_from=lbstresn,names_from = lbtestcd)