Language Reference
WIDETOLONG Call
CALL WIDETOLONG (LongX, LongY, Group, Y <, X> <, YLabels> ) ;
This subroutine is supported by the IML procedure and the iml action.
The WIDETOLONG subroutine converts time series data from a wide form (an matrix) into a long form, which consists of an
vector and an ID variable.
If the input matrix Y contains p variables, the output vector LongY is a column vector that contains the columns of Y stacked on top of each other. The vector Group contains an ID variable that enables you to determine which rows of LongY correspond to which columns of Y.
The output arguments are as follows:
- LongX
contains repeated values of the X vector.
- LongY
contains a stacked copy of the time series data.
- Group
contains an ID variable that identifies each series.
The input arguments are as follows:
- Y
- X
specifies an N-element vector that specifies values of the independent (time) variable. If this argument is not specified, the vector
T(1:N)is used by default.- YLabels
specifies a p-element vector that contains the values to be used for the output variable Group. If this argument is not specified, the vector
"Y1":"Yp"is used by default.
In the following example, the matrix Y contains data in two columns and three rows. The rows of X contain the times (or positions) that correspond to each row of Y. A call to the WIDETOLONG subroutine converts the data into three vectors. The OutY vector contains the columns of Y (stacked on top of each other), the OutX vector contains repeated values of X, and the Group vector contains an ID variable, as shown in Figure 496.
X = {0, 1.5, 4};
Y = {6 2,
5 4,
2 9 };
call WideToLong(OutX, OutY, Group,
Y, X);
print OutX OutY Group;
Figure 496: Convert Wide Data to Long Data
| OutX | OutY | Group |
|---|---|---|
| 0 | 6 | Y1 |
| 1.5 | 5 | Y1 |
| 4 | 2 | Y1 |
| 0 | 2 | Y2 |
| 1.5 | 4 | Y2 |
| 4 | 9 | Y2 |
A common use of the WIDETOLONG subroutine is to convert multiple columns of data into a form that can be easily graphed by using the SERIES subroutine. This is illustrated by the following statements, which create Figure 497:
/* exchange rates of UK pound and Spanish pesetas to the US dollar */
X = T(1966:1994);
Y = {0.485709 0.435193, 0.5632 0.505549, 0.568364 0.506419,
0.564515 0.50816, 0.566155 0.505694, 0.576441 0.51991,
0.626625 0.500617, 0.703709 0.498295, 0.706515 0.498295,
0.784033 0.50758, 0.924894 0.57547, 0.863692 0.712845,
0.867812 0.662508, 0.802721 0.632045, 0.724715 0.733154,
0.826736 0.822732, 0.925951 1.004932, 0.978113 1.189962,
1.14864 1.232828, 1.030533 1.228113, 1.124233 1.174585,
1.027294 1.121564, 1.00786 1.107348, 1.109298 1.04584,
1 1, 1.036266 1.003191, 1.232419 1.143178,
1.256732 1.416842, 1.266178 1.394937 };
run WideToLong(Year, ExchangeRate, ID, /* output (long) */
Y, X, {"UK" "Spain"}); /* input (Y is wide) */
title "Exchange Rates";
call series(Year, ExchangeRate) group=ID grid={X Y}
option="curvelabel curvelabelpos=end";
Figure 497: A Graph of Multiple Time Series
