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

Obs city date cpi cpi_lag
1 Chicago JAN90 128.1 .
2 Chicago FEB90 129.2 128.1
3 Chicago MAR90 129.5 129.2
4 Chicago APR90 130.4 129.5
5 Chicago MAY90 130.4 130.4
6 Chicago JUN90 131.7 130.4
7 Chicago JUL90 132.0 131.7
8 Los Angeles JAN90 132.1 .
9 Los Angeles FEB90 133.6 132.1
10 Los Angeles MAR90 134.5 133.6
11 Los Angeles APR90 134.2 134.5
12 Los Angeles MAY90 134.6 134.2
13 Los Angeles JUN90 135.0 134.6
14 Los Angeles JUL90 135.6 135.0
15 New York JAN90 135.1 .
16 New York FEB90 135.3 135.1
17 New York MAR90 136.6 135.3
18 New York APR90 137.3 136.6
19 New York MAY90 137.2 137.3
20 New York JUN90 137.1 137.2
21 New York JUL90 138.4 137.1


Figure 14: Transposed Data Sets

Transposed Data Set

Obs date Chicago Los_Angeles New_York
1 JAN90 128.1 132.1 135.1
2 FEB90 129.2 133.6 135.3
3 MAR90 129.5 134.5 136.6
4 APR90 130.4 134.2 137.3
5 MAY90 130.4 134.6 137.2
6 JUN90 131.7 135.0 137.1
7 JUL90 132.0 135.6 138.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

Obs date city CPI
1 JAN90 Chicago 128.1
2 FEB90 Chicago 129.2
3 MAR90 Chicago 129.5
4 APR90 Chicago 130.4
5 MAY90 Chicago 130.4
6 JUN90 Chicago 131.7
7 JUL90 Chicago 132.0
8 JAN90 Los_Angeles 132.1
9 FEB90 Los_Angeles 133.6
10 MAR90 Los_Angeles 134.5
11 APR90 Los_Angeles 134.2
12 MAY90 Los_Angeles 134.6
13 JUN90 Los_Angeles 135.0
14 JUL90 Los_Angeles 135.6
15 JAN90 New_York 135.1
16 FEB90 New_York 135.3
17 MAR90 New_York 136.6
18 APR90 New_York 137.3
19 MAY90 New_York 137.2
20 JUN90 New_York 137.1
21 JUL90 New_York 138.4


Last updated: June 19, 2025