Working with Time Series Data |
Using PROC PLOT
The following statements use the PLOT procedure to
plot CPI in the USCPI data set against DATE.
(The data set plotted contains more observations than shown in
the previous examples.)
The plotting character used is a plus sign (+).
proc plot data=uscpi;
plot cpi * date = '+';
run;
The plot is shown in Figure 2.12.
Plot of cpi*date. Symbol used is '+'.
cpi |
140 +
|
|
|
|
| ++ +
135 + + + + +
| + +
| +
| +
| +
|
130 + ++
| + +
| + +
| +
|
| + + +
125 + + +
| + ++
| +
| +
| +
| +
120 +
|
--+-----------+-----------+-----------+-----------+-----------+-----------+-
JUN1988 JAN1989 JUL1989 FEB1990 AUG1990 MAR1991 OCT1991
date
|
|
Figure 2.12: Plot of Monthly CPI Over Time
Controlling the Time Axis: Tick Marks and Reference Lines
In the preceding example, the spacing of values on the time axis
looks a bit odd in that the dates do not match for each year.
Because DATE is a SAS date variable, the PLOT procedure needs
additional instruction on how to place the time axis tick marks.
The following statements use the HAXIS= option to
tell PROC PLOT to mark the axis at the start of each quarter.
proc plot data=uscpi;
plot cpi * date = '+' /
haxis= '1jan89'd to '1jul91'd by qtr;
run;
The plot is shown in Figure 2.13.
Plot of cpi*date. Symbol used is '+'.
140 +
|
|
|
| + + +
135 + + + + +
| + + +
cpi | +
| +
|
130 + + +
| + + +
| +
| +
| + + +
125 + + +
| + + +
| +
| + +
| +
120 +
---+------+------+------+------+------+------+------+------+------+------+--
J A J O J A J O J A J
A P U C A P U C A P U
N R L T N R L T N R L
1 1 1 1 1 1 1 1 1 1 1
9 9 9 9 9 9 9 9 9 9 9
8 8 8 8 9 9 9 9 9 9 9
9 9 9 9 0 0 0 0 1 1 1
date
|
|
Figure 2.13: Plot of Monthly CPI Over Time
The following example improves the plot by placing tick marks every year
and adds quarterly reference lines to the plot using the HREF=option.
The FORMAT statement tells PROC PLOT to print just
the year part of the date values on the axis.
The plot is shown in Figure 2.14.
proc plot data=uscpi;
plot cpi * date = '+' /
haxis= '1jan89'd to '1jan92'd by year
HREF='1apr89'd to '1apr91'd by qtr ;
format date year4.;
run;
Plot of cpi*date. Symbol used is '+'.
cpi | | | | | | | | | |
140 + | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |+ ++
135 + | | | | | | | + ++ +
| | | | | | | | ++ | |
| | | | | | | + | |
| | | | | | | +| | |
| | | | | | | + | | |
| | | | | | | | | |
130 + | | | | | ++ | | |
| | | | | ++ | | | |
| | | | | ++ | | | | |
| | | | + | | | | |
| | | | | | | | | |
| | | + ++ | | | | | |
125 + | | + +| | | | | | |
| |+ ++ | | | | | | |
| + | | | | | | | |
| + | | | | | | | | |
| + | | | | | | | | |
| + | | | | | | | | |
120 + | | | | | | | | |
| | | | | | | | | |
---+------------------+------------------+------------------+--
1989 1990 1991 1992
date
|
|
Figure 2.14: Plot of Monthly CPI Over Time
Marking the Subperiod of Points
In the preceding example, it is a little hard to tell which
month each point is, although the quarterly reference lines help some.
The following example shows how to set the plotting symbol to
the first letter of the month name. A DATA step first makes a copy
of DATE and gives this variable PCHAR a MONNAME1. format.
The variable PCHAR is used in the PLOT statement to supply the
plotting character.
This example also changes the plot by using quarterly tick marks
and by using the YYQC format to print the date values.
This example also changes the HREF=option to use annual reference lines.
The plot is shown in Figure 2.15.
data temp;
set uscpi;
pchar = date;
format pchar monname1.;
run;
proc plot data=temp;
plot cpi * date = pchar /
haxis= '1jan89'd to '1jul91'd by qtr
HREF='1jan90'd to '1jan91'd by year;
format date yyqc4.;
run;
Plot of cpi*date. Symbol is value of pchar.
cpi | | |
140 + | |
| | |
| | |
| | |
| | |
| | | M J J
135 + | J F M A
| | N D |
| | O |
| | S |
| | A |
| | |
130 + | J J |
| | A M |
| | F M |
| J |
| | |
| O N D | |
125 + A S | |
| M J J | |
| A | |
| M | |
| F | |
| J | |
120 + | |
| | |
---+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+--
89:1 89:2 89:3 89:4 90:1 90:2 90:3 90:4 91:1 91:2 91:3
date
|
|
Figure 2.15: Plot of Monthly CPI Over Time
Overlay Plots of Different Variables
Plot different series in different variables by
specifying the different plot requests,
each with its own plotting character,
on the same PLOT statement, and use the OVERLAY option.
For example, the following statements plot
the CPI, FORECAST, L95, and U95 variables produced by PROC ARIMA
in a previous example.
The actual series CPI is labeled with the plot character plus (+).
The forecast series is labeled with the plot character F.
The upper and lower confidence limits are labeled with
the plot character period (.).
The plot is shown in Figure 2.16.
proc arima data=uscpi;
identify var=cpi(1);
estimate q=1;
forecast id=date interval=month lead=12 out=arimaout;
run;
proc plot data=arimaout;
plot cpi * date = '+' forecast * date = 'F'
( l95 u95 ) * date = '.' /
overlay
haxis= '1jan89'd to '1jul92'd by qtr
HREF='1jan90'd to '1jan92'd by year ;
run;
Plot of cpi*date. Symbol used is '+'.
Plot of FORECAST*date. Symbol used is 'F'.
Plot of L95*date. Symbol used is '.'.
Plot of U95*date. Symbol used is '.'.
cpi | | | |
150 + | | |
| | | |
| | | |
| | | | . . .
| | | | .. .F F F
140 + | | .. F FF F . .
| | | . .F F FF . .. ..
| | | F ++ + ++ F. . .. |
| | + + ++ + .. |
| | . + +. | |
130 + | .F + ++ F | |
| + + ++ . | |
| . .+ + + +F | |
| ++ + + +. . | | |
| ++ + F | | |
120 + . | | |
| | | |
---+----+----+----+----+----+----+----+----+----+----+----+----+----+----+--
J A J O J A J O J A J O J A J
A P U C A P U C A P U C A P U
N R L T N R L T N R L T N R L
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
8 8 8 8 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 0 0 0 0 1 1 1 1 2 2 2
date
NOTE: 15 obs had missing values. 77 obs hidden.
|
|
Figure 2.16: Plot of ARIMA Forecast
Overlay Plots of Interleaved Series
Plot interleaved time series by using
the first character of the ID variable to distinguish the
different series as the plot character.
The following example plots the output data set produced by
PROC FORECAST in a previous example.
The _TYPE_ variable is used on the PLOT statement to supply plotting
characters to label the different series.
The actual series is plotted with A, the forecast series is
plotted with F, the lower confidence limit is plotted with L,
and the upper confidence limit is plotted with U.
Since the residual series has a different scale than the other
series, it is excluded from the plot with a WHERE statement.
The plot is shown in Figure 2.17.
proc forecast data=uscpi interval=month lead=12
out=foreout outfull outresid;
var cpi;
id date;
run;
proc plot data=foreout;
plot cpi * date = _type_ /
haxis= '1jan89'd to '1jul92'd by qtr
HREF='1jan90'd to '1jan92'd by year ;
where _type_ ^= 'RESIDUAL';
run;
Plot of cpi*date. Symbol is value of _TYPE_.
cpi | | | |
150 + | | |
| | | |
| | | |
| | | | U
| | | | UU F F
| | | U UF FF L L
140 + | | U UF F FL L
| | | UF F FL L
| | | AA FL L |
| | A A AA A |
| | AA A A| |
| | A F | |
130 + | F A AA | |
| | A AA | |
| A AA | |
| F A AA A | | |
| AA A | | |
| AA A | | |
120 + | | |
| | | |
---+----+----+----+----+----+----+----+----+----+----+----+----+----+----+--
J A J O J A J O J A J O J A J
A P U C A P U C A P U C A P U
N R L T N R L T N R L T N R L
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
8 8 8 8 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 0 0 0 0 1 1 1 1 2 2 2
date
NOTE: 36 obs hidden.
|
|
Figure 2.17: Plot of Forecast
Residual Plots
The following example plots the residual series that was
excluded from the plot in the previous example.
The VREF=0 option is used to draw a reference line at 0
on the vertical axis.
The plot is shown in Figure 2.18.
proc plot data=foreout;
plot cpi * date = '*' /
vref=0
haxis= '1jan89'd to '1jul91'd by qtr
HREF='1jan90'd to '1jan91'd by year ;
where _type_ = 'RESIDUAL';
run;
Plot of cpi*date. Symbol used is '*'.
cpi | | |
1.0 + | |
| | |
| | |
| | |
| | * |
| * * |
0.5 + | * |
| | |
| | *
| * * | |
| * | * |
| | * * |
0.0 +-*--------------------*------+--*------------------------+----------------
| * * | * |
| * * | | *
| | * | * * *
| * * | * * | *
| | |
-0.5 + * | | *
| | |
--+------+------+------+------+------+------+------+------+------+------+--
J A J O J A J O J A J
A P U C A P U C A P U
N R L T N R L T N R L
1 1 1 1 1 1 1 1 1 1 1
9 9 9 9 9 9 9 9 9 9 9
8 8 8 8 9 9 9 9 9 9 9
9 9 9 9 0 0 0 0 1 1 1
date
|
|
Figure 2.18: Plot of Residuals
Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.