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 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 13 and Figure 14.

Figure 13: Original Data Sets

Original Data Set

Obscitydatecpicpi_lag
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 14: 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 15.

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 15: 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


Last updated: June 19, 2025