Highlight forecast regions in graphs

A SAS customer asked how to use background colors and a dashed line to emphasize the forecast region for a graph that shows a time series model. The task requires the following steps:

Use the ATTRPRIORITY=NONE option on the ODS GRAPHICS statement to make sure that the current ODS style will change line patterns, and use the STYLEATTRS statement to set the
line patters for the plot.

Add an indicator variable to the data set that indicates which times are in the "past" (the data region) and which times are in the "future" (the forecast region).

Use the BLOCK statement in PROC SGPLOT to add a background color that differentiates the past and future regions.

Use the SERIES statement to plot the model forecast and use the GROUP= option to visually differentiate the past predictions (solid line) from future predictions (dashed line).

A simple example

A simple "toy" example is the best way to show the essential features of the desired graph. The following DATA step creates a curve in two regions. For x ≤ 6.28, the curve is a sine curve. For x > 6.28, the curve is linear. These two domains correspond to the "Historical" and "Forecast" levels of the indicator variable BlockID. The graph is shown to the left.

The graph emphasizes the forecast region by using color and a line pattern.
The ATTRPRIORITY=NONE option ensures that the line patterns alternate between groups. For details, see Sanjay Matange's article about the interactions between the ATTRPRIORITY= option and the STYLEATTRS statement.
For rapidly oscillating models, you might want to use the DOT line pattern instead of the DASH line pattern.

Of course, this example is very simplistic. The next section shows how you can apply the ideas to a more realistic example.

A time series example with forecast region highlighted

Many SAS procedures create suitable graphs when you enable ODS GRAPHICS. In particular,
many SAS/ETS procedures (such as PROC ARIMA) can create graphs that look similar to this example. The following classic example is taken from the PROC ARIMA documentation. The data are the log-transformed number of passengers who flew on commercial airlines in the US between 1949 and 1961. Based on these data, the ARMIA model forecasts an additional 24 months of passenger traffic.

You can see that the ODS graph uses a dashed line to separate the historical (data) region from the forecast region. However, the graph uses a solid line to display all predicted values, even the forecast.

In the previous PROC ARIMA call, I used the OUT= option on the FORECAST statement to create a SAS data set that contains the predicted values and confidence region. The following DATA step adds an indicator variable to the data:

You can now create the modified graph by using the STYLEATTRS, BLOCK, and SERIES statements. In addition, a BAND statement adds the confidence limits for the predicted values. A SCATTER statement adds the data values. The XAXIS and YAXIS values overlay a grid on the graph.

The final graph is a customized version of the default graph that is created by using PROC ARIMA. The presentation highlights the forecast region by using a different background color and a different line style.

About Author

Rick Wicklin, PhD, is a distinguished researcher in computational statistics at SAS and is a principal developer of PROC IML and SAS/IML Studio. His areas of expertise include computational statistics, simulation, statistical graphics, and modern methods in statistical data analysis. Rick is author of the books Statistical Programming with SAS/IML Software and Simulating Data with SAS.