Working with Time Series Data

Transposing Data Sets

The TRANSPOSE procedure is used to transpose data sets from one form to another. The TRANSPOSE procedure can transpose variables and observations, or transpose variables and observations within BY groups. This section discusses some applications of the TRANSPOSE procedure relevant to time series data sets. For more information about PROC TRANSPOSE, see Base SAS Procedures Guide.

Transposing from Interleaved to Standard Time Series Form

The following statements transpose part of the interleaved-form output data set FOREOUT, produced by PROC FORECAST in a previous example, to a standard form time series data set. To reduce the volume of output produced by the example, a WHERE statement is used to subset the input data set.

Observations with _TYPE_=ACTUAL are stored in the new variable ACTUAL; observations with _TYPE_=FORECAST are stored in the new variable FORECAST; and so forth. Note that the method used in this example works only for a single variable.

title "Original Data Set";
proc print data=foreout(obs=10);
   where date > '1may1991'd & date < '1oct1991'd;
run;
proc transpose data=foreout out=trans(drop=_name_);
   var cpi;
   id _type_;
   by date;
   where date > '1may1991'd & date < '1oct1991'd;
run;
title "Transposed Data Set";
proc print data=trans(obs=10);
run;

The TRANSPOSE procedure adds the variables _NAME_ and _LABEL_ to the output data set. These variables contain the names and labels of the variables that were transposed. In this example, there is only one transposed variable, so _NAME_ has the value CPI for all observations. Thus, _NAME_ and _LABEL_ are of no interest and are dropped from the output data set by using the DROP= data set option. (If none of the variables transposed have a label, PROC TRANSPOSE does not output the _LABEL_ variable and the DROP=_LABEL_ option produces a warning message. You can ignore this message, or you can prevent the message by omitting _LABEL_ from the DROP= list.)

The original and transposed data sets are shown in Figure 3.17 and Figure 3.18. (The observation numbers shown for the original data set reflect the operation of the WHERE statement.)

Figure 3.17: Original Data Sets

Original Data Set

Obsdate_TYPE__LEAD_cpi
37JUN1991ACTUAL0136.000
38JUN1991FORECAST0136.146
39JUN1991RESIDUAL0-0.146
40JUL1991ACTUAL0136.200
41JUL1991FORECAST0136.566
42JUL1991RESIDUAL0-0.366
43AUG1991FORECAST1136.856
44AUG1991L951135.723
45AUG1991U951137.990
46SEP1991FORECAST2137.443


Figure 3.18: Transposed Data Sets

Transposed Data Set

Obsdate_LABEL_ACTUALFORECASTRESIDUALL95U95
1JUN1991US Consumer Price Index136.0136.146-0.14616..
2JUL1991US Consumer Price Index136.2136.566-0.36635..
3AUG1991US Consumer Price Index.136.856.135.723137.990
4SEP1991US Consumer Price Index.137.443.136.126138.761


Transposing Cross-Sectional Dimensions

The following statements transpose the variable CPI in the CPICITY data set shown in a previous example from time series cross-sectional form to a standard form time series data set. (Only a subset of the data shown in the previous example is used here.) Note that the method shown in this example works only for a single variable.

title "Original Data Set";
proc print data=cpicity;
run;

proc sort data=cpicity out=temp;
   by date city;
run;
proc transpose data=temp out=citycpi(drop=_name_);
   var cpi;
   id city;
   by date;
run;

title "Transposed Data Set";
proc print data=citycpi;
run;

The names of the variables in the transposed data sets are taken from the city names in the ID variable CITY. The original and the transposed data sets are shown in Figure 3.19 and Figure 3.20.

Figure 3.19: Original Data Sets

Original Data Set

Obscitydatecpicpilag
1ChicagoJAN90128.1.
2ChicagoFEB90129.2128.1
3ChicagoMAR90129.5129.2
4ChicagoAPR90130.4129.5
5ChicagoMAY90130.4130.4
6ChicagoJUN90131.7130.4
7ChicagoJUL90132.0131.7
8Los AngelesJAN90132.1.
9Los AngelesFEB90133.6132.1
10Los AngelesMAR90134.5133.6
11Los AngelesAPR90134.2134.5
12Los AngelesMAY90134.6134.2
13Los AngelesJUN90135.0134.6
14Los AngelesJUL90135.6135.0
15New YorkJAN90135.1.
16New YorkFEB90135.3135.1
17New YorkMAR90136.6135.3
18New YorkAPR90137.3136.6
19New YorkMAY90137.2137.3
20New YorkJUN90137.1137.2
21New YorkJUL90138.4137.1


Figure 3.20: Transposed Data Sets

Transposed Data Set

ObsdateChicagoLos_AngelesNew_York
1JAN90128.1132.1135.1
2FEB90129.2133.6135.3
3MAR90129.5134.5136.6
4APR90130.4134.2137.3
5MAY90130.4134.6137.2
6JUN90131.7135.0137.1
7JUL90132.0135.6138.4


The following statements transpose the CITYCPI data set back to the original form of the CPICITY data set. The variable _NAME_ is added to the data set to tell PROC TRANSPOSE the name of the variable in which to store the observations in the transposed data set. (If the (DROP=_NAME_ _LABEL_) option were omitted from the first PROC TRANSPOSE step, this would not be necessary. PROC TRANSPOSE assumes ID _NAME_ by default.)

The NAME=CITY option in the PROC TRANSPOSE statement causes PROC TRANSPOSE to store the names of the transposed variables in the variable CITY. Because PROC TRANSPOSE recodes the values of the CITY variable to create valid SAS variable names in the transposed data set, the values of the variable CITY in the retransposed data set are not the same as in the original. The retransposed data set is shown in Figure 3.21.

data temp;
   set citycpi;
   _name_ = 'CPI';
run;

proc transpose data=temp out=retrans name=city;
   by date;
run;

proc sort data=retrans;
   by city date;
run;
title "Retransposed Data Set";
proc print data=retrans;
run;

Figure 3.21: Data Set Transposed Back to Original Form

Retransposed Data Set

ObsdatecityCPI
1JAN90Chicago128.1
2FEB90Chicago129.2
3MAR90Chicago129.5
4APR90Chicago130.4
5MAY90Chicago130.4
6JUN90Chicago131.7
7JUL90Chicago132.0
8JAN90Los_Angeles132.1
9FEB90Los_Angeles133.6
10MAR90Los_Angeles134.5
11APR90Los_Angeles134.2
12MAY90Los_Angeles134.6
13JUN90Los_Angeles135.0
14JUL90Los_Angeles135.6
15JAN90New_York135.1
16FEB90New_York135.3
17MAR90New_York136.6
18APR90New_York137.3
19MAY90New_York137.2
20JUN90New_York137.1
21JUL90New_York138.4