Using PROC TRANSPOSE

Overview

In the previous section, we compared actual revenue values to target revenue values using an array as a lookup table. Remember the following:
  • Sasuser.Monthsum has an observation for each month and year.
    Table 16.12 SAS Data Set Sasuser.Monthsum (first five observations of selected variables)
    SaleMon
    RevCargo
    MonthNo
    JAN1997
    $171,520,869.10
    1
    JAN1998
    $238,786,807.60
    1
    JAN1999
    $280,350,393.00
    1
    FEB1997
    $177,671,530.40
    2
    FEB1998
    $215,959,695.50
    2
  • Sasuser.Ctargets has one variable for each month and one observation for each year.
    Table 16.13 SAS Data Set Sasuser.Ctargets (selected variables)
    Year
    Jan
    Feb
    Mar
    Apr
    May
    Jun
    1997
    192284420
    86376721
    28526103
    260386468
    109975326
    102833104
    1998
    108645734
    147656369
    202158055
    41160707
    264294440
    267135485
    1999
    85730444
    74168740
    39955768
    312654811
    318149340
    187270927
Using arrays was a good solution because the orientation of the data sets differed. An alternate solution is to transpose Sasuser.Ctargets using PROC TRANSPOSE, and then merge the transposed data set with Sasuser.Monthsum by the values of Year and Month.
General form, PROC TRANSPOSE:
PROC TRANSPOSE <DATA=input-data-set>
<OUT=output-data-set>
<NAME=prefix text>
<PREFIX=variable-name>;
BY <DESCENDING> variable-1
<...<DESCENDING> variable-n>
<NOTSORTED>;
VAR variable(s);
RUN;
Here is an explanation of the syntax:
DATA=input-data-set
names the SAS data set to transpose.
OUT=output-data-set
names the output data set.
NAME=variable-name
specifies the name for the variable in the output data set that contains the name of the variable that is being transposed to create the current observation.
PREFIX=variable-name
specifies a prefix to use in constructing names for transposed variables in the output data set. For example, if PREFIX=VAR, the names of the variables are VAR1, VAR2, ...,VARn.
BY statement
is used to transpose each BY group.
VAR variable(s)
names one or more variables to transpose.
Note: If output-data-set does not exist, PROC TRANSPOSE creates it by using the DATAn naming convention.
Note: If you omit the VAR statement, the TRANSPOSE procedure transposes all of the numeric variables in the input data set that are not listed in another statement.
Note: You must list character variables in a VAR statement if you want to transpose them.
The TRANSPOSE procedure creates an output data set by restructuring the values in a SAS data set. When the data set is restructured, selected variables are transposed into observations. The TRANSPOSE procedure can often eliminate the need to write a lengthy DATA step to achieve the same result. The output data set can be used in subsequent DATA or PROC steps for analysis, reporting, or further data manipulation.
PROC TRANSPOSE does not print the output data set. Use PROC PRINT, PROC REPORT, or some other SAS reporting tool to print the output data set.
SAS Dat Set Sasuser. Ctargets

Example

The following program transposes the SAS data set Sasuser.Ctargets. The OUT= option specifies the name of the output data set, Work.Ctarget2. All of the variables in Sasuser.Ctargets are transposed because all of the variables are numeric and a VAR statement is not used in the program.
proc transpose data=sasuser.ctargets
     out=work.ctarget2;
run;
Table 16.14 Input Data Set Sasuser.Ctargets (selected variables)
Year
Jan
Feb
Mar
Apr
May
Jun
1997
192284420
86376721
28526103
260386468
109975326
102833104
1998
108645734
147656369
202158055
41160707
264294440
267135485
1999
85730444
74168740
39955768
312654811
318149340
187270927
Table 16.15 Output Data Set: Work.Ctarget2
Obs
_NAME_
COL1
COL2
COL3
1
Year
1997
1998
1999
2
Jan
192284420
108645734
85730444
3
Feb
86376721
147656369
74168740
4
Mar
28526103
202158055
39955768
5
Apr
260386468
41160707
31265481
6
May
109975326
264294440
318149340
7
Jun
102833104
267135485
187270927
8
Jul
196728648
208694865
123394421
9
Aug
236996122
83456868
34273985
10
Sep
112413744
286846554
151565752
11
Oct
125401565
275721406
141528519
12
Nov
72551855
230488351
178043261
13
Dec
136042505
24901752
181668256
Notice that in the output data set, the variables are named _NAME_, COL1, COL2, and COL3.
_NAME_ is the default name of the variable that PROC TRANSPOSE creates to identify the source of the values in each observation in the output data set. This variable is a character variable whose values are the names of the variables that are transposed from the input data set. For example, in Work.Ctarget2 the values in the first observation in the output data set come from the values of the variable Year in the input data set.
The remaining transposed variables are named COL1...COLn by default. In Work.Ctarget2, the values of the variables COL1, COL2, and COL3 represent the target cargo revenue for each month in the years 1997, 1998, and 1999.

Adding Descriptive Variable Names

You can use PROC TRANSPOSE options to give the variables in the output data set more descriptive names. The NAME= option specifies a name for the _NAME_ variable.
The PREFIX= option specifies a prefix to use in constructing names for transposed variables in the output data set. For example, if PREFIX=Ctarget, the names of the variables are Ctarget1, Ctarget2, and Ctarget3.
proc transpose data=sasuser.ctargets 
     out=work.ctarget2
     name=Month
     prefix=Ctarget;
run;
Table 16.16 Output Data Set: Work.Ctarget2
Obs
Month
Ctarget1
Ctarget2
Ctarget3
1
Year
1997
1998
1999
2
Jan
192284420
108645734
85730444
3
Feb
86376721
147656369
74168740
4
Mar
28526103
202158055
39955768
5
Apr
260386468
41160707
31265481
6
May
109975326
264294440
318149340
7
Jun
102833104
267135485
187270927
8
Jul
196728648
208694865
123394421
9
Aug
236996122
83456868
34273985
10
Sep
112413744
286846554
151565752
11
Oct
125401565
275721406
141528519
12
Nov
72551855
230488351
178043261
13
Dec
136042505
24901752
181668256
Note: The RENAME=data set option can also be used with PROC TRANSPOSE to change variable names.
proc transpose data=sasuser.ctargets 
     out=work.ctarget2 (rename=(col1=Ctarget1 
         col2=Ctarget2 col3=Ctarget3))
     name=Month;
run;
The default label for the _NAME_ variable is NAME OF FORMER VARIABLE. To see this, print the transposed data set using PROC PRINT with the LABEL option. You can use a LABEL statement to override the default label.
proc transpose data=sasuser.ctargets 
     out=work.ctarget2
     name=Month 
     prefix=Ctarget; 
run;
proc print data=work.ctarget2 label;
label Month=MONTH;
run;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.67.235