TRANSPOSE Procedure

Overview: TRANSPOSE Procedure

What Does the TRANSPOSE Procedure Do?

The TRANSPOSE procedure creates an output data set by restructuring the values in a SAS data set, transposing selected variables into observations. The TRANSPOSE procedure can often eliminate the need to write a lengthy DATA step to achieve the same result. Further, the output data set can be used in subsequent DATA or PROC steps for analysis, reporting, or further data manipulation.
PROC TRANSPOSE does not produce printed output. To print the output data set from the PROC TRANSPOSE step, use PROC PRINT, PROC REPORT, or another SAS reporting tool.
To create transposed variable, the procedure transposes the values of an observation in the input data set into values of a variable in the output data set.

What Types of Transpositions Can PROC TRANSPOSE Perform?

Simple Transposition

The following example illustrates a simple transposition. In the input data set, each variable represents the scores from one tester. In the output data set, each observation now represents the scores from one tester. Each value of _NAME_ is the name of a variable in the input data set that the procedure transposed. Thus, the value of _NAME_ identifies the source of each observation in the output data set. For example, the values in the first observation in the output data set come from the values of the variable Tester1 in the input data set. The statements that produce the output follow.
proc print data=proclib.product noobs;
   title 'The Input Data Set';
run;

proc transpose data=proclib.product
               out=proclib.product_transposed;
run;

proc print data=proclib.product_transposed noobs;
   title 'The Output Data Set';
run;
A Simple Transposition
                               The Input Data Set                              1

                    Tester1    Tester2    Tester3    Tester4

                       22         25         21         21
                       15         19         18         17
                       17         19         19         19
                       20         19         16         19
                       14         15         13         13
                       15         17         18         19
                       10         11          9         10
                       22         24         23         21
                              The Output Data Set                              2

    _NAME_     COL1    COL2    COL3    COL4    COL5    COL6    COL7    COL8

    Tester1     22      15      17      20      14      15      10      22
    Tester2     25      19      19      19      15      17      11      24
    Tester3     21      18      19      16      13      18       9      23
    Tester4     21      17      19      19      13      19      10      21

Complex Transposition Using BY Groups

The next example, which uses BY groups, is more complex. The input data set represents measurements of the weight and length of fish at two lakes. The statements that create the output data set do the following:
  • transpose only the variables that contain the length measurements
  • create six BY groups, one for each lake and date
  • use a data set option to name the transposed variable
A Transposition with BY Groups
                                Input Data Set                                1

     L
     o                     L      W      L      W      L      W      L      W
     c                     e      e      e      e      e      e      e      e
     a                     n      i      n      i      n      i      n      i
     t               D     g      g      g      g      g      g      g      g
     i               a     t      h      t      h      t      h      t      h
     o               t     h      t      h      t      h      t      h      t
     n               e     1      1      2      2      3      3      4      4

 Cole Pond     02JUN95    31    0.25    32    0.30    32    0.25    33    0.30
 Cole Pond     03JUL95    33    0.32    34    0.41    37    0.48    32    0.28
 Cole Pond     04AUG95    29    0.23    30    0.25    34    0.47    32    0.30
 Eagle Lake    02JUN95    32    0.35    32    0.25    33    0.30     .     .
 Eagle Lake    03JUL95    30    0.20    36    0.45     .     .       .     .
 Eagle Lake    04AUG95    33    0.30    33    0.28    34    0.42     .    
.
                  Fish Length Data for Each Location and Date                  2

                 Location        Date    _NAME_     Measurement

                Cole Pond     02JUN95    Length1         31
                Cole Pond     02JUN95    Length2         32
                Cole Pond     02JUN95    Length3         32
                Cole Pond     02JUN95    Length4         33
                Cole Pond     03JUL95    Length1         33
                Cole Pond     03JUL95    Length2         34
                Cole Pond     03JUL95    Length3         37
                Cole Pond     03JUL95    Length4         32
                Cole Pond     04AUG95    Length1         29
                Cole Pond     04AUG95    Length2         30
                Cole Pond     04AUG95    Length3         34
                Cole Pond     04AUG95    Length4         32
                Eagle Lake    02JUN95    Length1         32
                Eagle Lake    02JUN95    Length2         32
                Eagle Lake    02JUN95    Length3         33
                Eagle Lake    02JUN95    Length4          .
                Eagle Lake    03JUL95    Length1         30
                Eagle Lake    03JUL95    Length2         36
                Eagle Lake    03JUL95    Length3          .
                Eagle Lake    03JUL95    Length4          .
                Eagle Lake    04AUG95    Length1         33
                Eagle Lake    04AUG95    Length2         33
                Eagle Lake    04AUG95    Length3         34
                Eagle Lake    04AUG95    Length4          .
For a complete explanation of the SAS program that produces these results, see Transposing BY Groups.