FOCUS AREAS

Statistics and Operations Research: Examples

Source Code for the Forecast Command Builder

/* Copyright(c) 2000 by SAS Institute Inc., Cary, NC USA */
/* FORCGRAF.SCL:  Source code for FORCGRAF.FRAME.        */
/* Graphs a forecast output data set from TSFS using     */
/* the Version 8 Time Series Graphics Object.            */

%let actColor   = black;
%let predColor  = blue;
%let clColor    = red;
%let residColor = black;
%let refColor   = black;

length hide curview $ 3 marker $ 5 join $ 4 crdate $ 13
       cflabel $ 24;

entry dsn $ timeid $ ;

init:
    control always;

    /* Figure out where the forecast beginning time is   */
    /* (same as the end point of the actual series),     */
    /* and get the data set label and creation date,     */
    /* and the variable label for confidence limits.     */
    dsid = open(dsn);
    dslabel = attrc(dsid, 'LABEL' );
    crdate  = putn(attrn(dsid, 'CRDTE'), 'datetime13.' );

    cflabel = varlabel(dsid,varnum(dsid,'UPPER'));
    cflabel = scan(cflabel,2,' ') || ' confidence limits';

    rc = where(dsid, 'actual ^= .' );
    rc = varstat(dsid,timeid,'max',actualend);
    rc = where(dsid, 'clear' );
    rc = close(dsid);

    /* Set attributes for the time series graph object.  */
    time1.dataSet = dsn;
    time1.actionmode = "Viewport";
    time1.timeVariable = timeid;
    time1._addChartArea(chartID, .);
    time1.MarkerSize = 0.1;
    time1.referenceLineColor = "&refColor";
    time1.actionmode = "Pick";

    /* Set graph title.  Dslabel is model name. */
    if dslabel ^= _blank_
    then titltext = 'Forecast of ' || crdate || ' using ' || dslabel;
    else titltext = 'Forecast of ' || crdate ;
    time1.title1.text = titltext;

    hide   = 'no';
    marker = 'cross';
    join   = 'none';
    curview = 'MP';

    link forview;
return;

main:
    command = word( 1, 'u' );
    call nextcmd();

    select(command);
       when('PICK')    time1.actionmode = "Pick";
       when('HAND')    time1.actionmode = "Datamove";
       when('ZOOMIN')  time1.actionmode = "Viewport";
       when('ZOOMOUT') time1._resetViewport();

       /* Forecast graph w/o 1-step ahead predictions. */
       when('HIDE1STP')
           if curview = 'ERR' then do;
              time1._deleteAnalysisVariable(chartID,
                    residid );
              hide   = 'yes';
              marker = 'none';
              join   = 'line';
              link forview;
              end;
           else if curview = 'MP' then do;
              time1._deleteAnalysisVariable(chartID,
                    predictid );
              time1._addAnalysisVariable(chartID,
                    predictid, "predict", "yes");
              time1._setMarkerColor(chartID, predictid,
                    "&predColor" );
              time1._setMarkerShape(chartID, actualid,
                    "none" );
              time1._setMarkerJoinType(chartID, actualid,
                    "line" );
              end;
           curview = 'FOR';

       /* Forecast graph with 1-step ahead predictions. */
       when('SHOW1STP')
           if curview = 'ERR' then do;
              time1._deleteAnalysisVariable(chartID,
                   residid );
              hide   = 'no';
              marker = 'cross';
              join   = 'none';
              link forview;
              end;
           else if curview = 'FOR' then do;
              time1._deleteAnalysisVariable(chartID,
                    predictid );
              time1._addAnalysisVariable(chartID,
                    predictid, "predict", "no");
              time1._setMarkerShape(chartID, actualid,
                    "cross" );
              time1._setMarkerJoinType(chartID, actualid,
                    "none" );
              time1._setMarkerColor(chartID, predictid,
                    "&predColor" );
              end;
           curview = 'MP';

       /* Prediction errors bar graph. */
       when('ERRORS')
           if curview in ('FOR', 'MP') then do;
              time1._deleteAnalysisVariable(chartID,
                    actualid );
              time1._deleteAnalysisVariable(chartID,
                    predictid );
              time1._deleteAnalysisVariable(chartID,clid );
              time1._deleteReferenceLine(chartID, rid1 );
              time1._addAnalysisVariable(chartID, residid,
                    "error", "no");
              time1._setMarkerColor(chartID, residid,
                    "&residColor" );
              time1._setMarkerJoinType(chartID, residid,
                    "none" );
              time1._setMarkerShape(chartID, residid,
                    "bar" );
              time1._setAnalysisAxisBasisValue(chartID,
                     0.0);
              end;
           curview = 'ERR';

       when('PRINT') time1._print();

       otherwise;
       end;
return;

forview:
    /* Generate the forecast graph. */
    time1._addAnalysisVariable(chartID, actualid, "actual");
    time1._setMarkerJoinType(chartID, actualid, join );
    time1._setMarkerShape(chartID, actualid, marker );
    time1._addAnalysisVariable(chartID, predictid,
          "predict", hide);
    time1._addConfidenceVariables(chartID, clid,
           "lower", "upper", cflabel, "Yes" );
    time1._setForecastingBeginningTime(chartID, actualend );
    time1._addReferenceLine(., rid1, "x", "value",
          actualend );
    time1._SetReferenceLineStyle(chartId, rid1,  "DOTTED");
    time1._setMarkerColor(chartID, actualid, "&actColor" );
    time1._setMarkerColor(chartID, predictid,"&predColor");
    time1._setMarkerColor(chartID, clid,      "&clColor" );
return;

rc = rc;