How to use PROC TRANSPOSE in SAS (With Examples)

PROC TRANSPOSE is a SAS procedure that can be used to convert a dataset from a wide format to a long or vice versa. It can be used to transpose or pivot one or more variables from one form to another. It can also be used to convert character variables into numeric variables. Examples of using PROC TRANSPOSE include converting a dataset with multiple columns for a single item into a dataset with a single column for multiple items, and also converting a dataset with a single column for multiple items into a dataset with multiple columns for a single item.


You can use PROC TRANSPOSE in SAS to quickly transpose a dataset from a .

This function uses the following basic syntax:

proc transpose data=long_data out=wide_data;
    by var1;
    id var2;
    var var3;
run;

where:

  • by: The variable to place along the rows
  • id: The variable to place along the columns
  • var: The variable whose values are placed within the dataset

The following example shows how to use PROC TRANSPOSE in practice.

Example: How to Use PROC TRANSPOSE in SAS

Suppose we have the following dataset in a long format in SAS:

/*create dataset in long format*/
data long_data;
    input team $ variable $ value;
    datalines;
A Points 88
A Assists 12
A Rebounds 22
B Points 91
B Assists 17
B Rebounds 28
C Points 99
C Assists 24
C Rebounds 30
D Points 94
D Assists 28
D Rebounds 31
;
run;

/*view dataset*/
proc print data=long_data;

We can use PROC TRANSPOSE to convert this dataset from a long format to a wide format:

/*create new dataset in wide format*/
proc transpose data=long_data out=wide_data;
    by team;
    id variable;
    var value;
run;

/*view wide data*/
proc print data=wide_data;

Notice that this dataset contains the same information as the previous dataset, but it’s simply displayed in a wide format.

By default, SAS creates a _NAME_ variable that shows which variable is used for the values in the  dataset.

Feel free to use the DROP statement to drop this variable when using PROC TRANSPOSE:

/*create new dataset in wide format*/
proc transpose data=long_data out=wide_data(drop=_name_);
    by team;
    id variable;
    var value;
run;

/*view wide data*/
proc print data=wide_data;

Notice that the _NAME_ variable has been dropped from the dataset.

The following tutorials explain how to perform other common tasks in SAS:

x