How to Use PROC TRANSPOSE in SAS (With Examples)


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

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.

Additional Resources

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

How to Use Proc Summary in SAS
How to Use Proc Tabulate in SAS
How to Create Frequency Tables in SAS

Leave a Reply

Your email address will not be published.