|
data WIDE; proc sort data=wide;
|
|
Description: The PROC TRANSPOSE procedure in SAS is used to transform a dataset from wide format to long format. In this code snippet, the dataset "wide" is transposed into a new dataset named "long" using the PROC TRANSPOSE procedure. data=wide: This specifies the input dataset "wide" that contains the data in wide format. out=long(rename=(col1=lbstresn)): This specifies the output dataset "long" where the transposed data will be stored. The RENAME statement is used to rename the variable generated by PROC TRANSPOSE as "col1" to "lbstresn" to match the desired variable name. name=lbtestcd: This specifies the variable "lbtestcd" as the name variable. The values of this variable in the wide dataset will become the values of a new variable in the long dataset, which will contain the original column names. by usubjid: This specifies the BY statement, indicating that the data should be transposed for each unique value of the variable "usubjid". var HGB ALT AST;: This specifies the variables to be transposed from wide to long format. In this case, the variables "HGB", "ALT", and "AST" are selected. By running the PROC TRANSPOSE procedure with the specified options, the "wide" dataset is transposed into the "long" dataset. The transposed dataset will have variables "usubjid", "lbtestcd" (containing the original column names), and "lbstresn" (containing the values from the wide dataset). |
|
long<-pivot_longer(wide, |
|
Description: The pivot_longer function in R, part of the Tidyverse package, is used to transform a dataset from wide format to long format. In this code snippet, the dataset "wide" is transformed into a new dataset named "long" using the pivot_longer function. cols = c(HGB, ALT, AST): This specifies the columns to be transformed from wide to long format. In this case, the variables "HGB", "ALT", and "AST" are selected. names_to = "lbtestcd": This specifies the name of the new variable that will contain the column names from the wide dataset. The values in this variable will correspond to the original column names. values_to = "lbstresn": This specifies the name of the new variable that will contain the values from the wide dataset. The values in this variable will correspond to the values previously present in the selected columns. By using the pivot_longer function with the specified arguments, the "wide" dataset is transformed into the "long" dataset. The "long" dataset will have variables "lbtestcd" (containing the original column names) and "lbstresn" (containing the values from the wide dataset), along with any other existing variables. |