FOCUS AREAS

Graphics

Requires SAS 9.4 or later
graph that uses annotations
Base SAS: GTL Template Language
SAS 9.4 GTL features: BeginGraph statement's SubPixel option, annotations
Note. This example uses Shiller housing data from http://www.econ.yale.edu/~shiller/data.htm. File housingData.html shows the data values.

%let graphs='.';
%let data='.';

%let dpi=100;
%let w=8in;
%let h=4.5in;

libname data &data;

/*--Use Shiller data and add some forecasted values to the end--*/
data forecast;
  set data.housing (where=(date ne .)) end=last;
  length group $8;
  retain prev_date prev_index;
  keep date index group;

  group='Actual';
  if last then do;
    group='Predict';

    dx=date - prev_date;
    dy=-4;

    do i=1 to 6;
       date=prev_date + i*dx;
       index=prev_index + i*dy;
       output;
    end;
  end;
  else do;
    output;
    prev_date=date;
    prev_index=index;
  end;
run;

/*--Events data--*/
data events;
  input date2 Event $8-18 Label $20-29;
  ylabel=205;
  datalines;
1890     
1913     
1914   WW 1
1916.5 WW 1        WW 1
1918   WW 1
1919     
1928     
1929   Depression   
1934.5 Depression  Depression
1940   Depression   
1940.1     
1940.9     
1941   WW 2
1944   WW 2        WW 2
1946   WW 2
1947     
1975     
1976   70's Boom
1977.5 70's Boom   70's Boom
1979   70's Boom
1980     
1983     
1984   80's Boom
1987   80's Boom   80's Boom
1989   80's Boom
1990     
1996     
1997   Last Boom
2003   Last Boom   Last Boom
2008   Last Boom
2008.1   
2010     
;

data merged;
  set forecast events;
run;

/*--Graph with Annotate--*/
proc template;
 define statgraph housing;
  begingraph / subpixel=on;
    entryfootnote halign=left 'This graph was created using the new SGAnnotation support in GTL'; 
    layout overlay / walldisplay=(fill) pad=(top=20) 
        xaxisopts=(display=(ticks tickvalues line) griddisplay=off  
          linearopts=(tickvaluesequence=(start=1890 end=2010 increment=10)))
        y2axisopts=(display=(ticks tickvalues) displaysecondary=(ticks tickvalues)
          griddisplay=on offsetmin=0.05 offsetmax=0.05 
          linearopts=(tickvaluesequence=(start=60 end=200 increment=10) thresholdmax=1));
      blockplot x=date2 block=event / fillattrs=(color=lightgray) datatransparency=0.5
        fillattrs=(color=white) altfillattrs=(color=cxbfd7f4) filltype=alternate
        display=(fill)
        valuehalign=center valuevalign=top; 
      seriesplot x=date y=index / yaxis=y2 group=group lineattrs=(thickness=3) name='p'
        includemissinggroup=false;
      discretelegend 'p' / location=inside halign=0.6 valign=bottom across=1;
      annotate;
    endlayout;
  endgraph;
 end;
run;

/*--Create Annotation data set--*/
options nocodegen;
data anno;
  length label $ 400;
  length anchor function drawspace x1space y1space textweight display justify fillcolor 
         widthunit heightunit $20;
  
  /*--Clear PDV--*/
  call missing (of _all_);

  /*--Draw the arrow--*/
  function='arrow'; drawspace='datavalue'; 
  x1space='datapercent'; y1space='wallpercent'; 
  x1=98;  y1=100; x2=2006.13; y2=200;
  output;
 
  /*--Clear PDV--*/
  call missing (of _all_);

  /*--Draw the label on top right--*/
  function='text'; x1space='datapercent'; y1space='wallpercent'; anchor='left'; textweight='bold';
  x1=98;  y1=100; label='July 2006';
  output; 
 
  /*--Clear PDV--*/
  call missing (of _all_);

  /*--Draw the white rectangle--*/
  function='rectangle'; drawspace='graphpercent'; display='fill'; 
  x1=1;  y1=99; width=80; height=46;
  widthunit='percent'; heightunit='percent'; fillcolor='white'; anchor='topleft';
  output;
 
  /*--Clear PDV--*/
  call missing (of _all_);

  /*--Draw the text title--*/
  function='text';  drawspace='layoutpercent'; x1space=' '; y1space=' '; anchor='topleft'; justify='left'; textweight=' ';   width=70;
  x1=0;  y1=99; label='A History of Home Values for USA';
  output; 
 
  /*--Clear PDV--*/
  call missing (of _all_);

  /*--Draw 1st paragraph--*/
  function='text';  drawspace='layoutpercent'; anchor='topleft'; justify='left'; widthunit='percent';
  x1=0;  y1=92; width=80; textsize=7;
  label="The Yale economist Robert J. Schiller created an index of " || 
        "American Housing proces going back to 1890.  It is based on sale prices of standard " ||
        "existing homes, not new construction, to track the value of housing as an investment " || 
        "over time.  It presents housing values in consistent terms over 116 years, factoring " ||
        "out the effects of inflation.";
   output;

  /*--Clear PDV--*/
  call missing (of _all_);

  /*--Draw 2nd paragraph--*/
  function='text';  drawspace='layoutpercent'; anchor='topleft'; justify='left'; widthunit='percent';
  x1=0;  y1=82; width=80; textsize=7;
  label="The 1890 benchmark is 100 on the chart.  If a standard house " ||
        "sold in 1890 for $100,000 (inflation adjusted to today's dollars), an equivalent " ||
        "standard house would have sold for $66,000 in 1920 (66 on the index scale) and " ||
        "$199,000 in 2006 (199 on the index scale), or 99% higher than 1890.";
   output;

  /*--Clear PDV--*/
  call missing (of _all_);

  /*--Draw 3rd paragraph--*/
  function='text';  x1space='datavalue'; y1space='layoutpercent'; widthunit='data'; anchor='topleft'; justify='left';
  x1=1914;  y1=72; width=36; textsize=7; textweight='bold';
  label="Decline and Run-Up : ";
  output;

  /*--Clear PDV--*/
  call missing (of _all_);

  /*--Draw 3rd paragraph continued--*/
  function='textcont'; textweight='normal';  
  label="Prices dropped as mass production techniques "  ||
        "appeared early in the 20th century.  Prices spiked with post-war housing demand.";
   output;

  /*--Clear PDV--*/
  call missing (of _all_);

  /*--Draw 4th paragraph--*/
  function='text';  x1space='datavalue'; y1space='layoutpercent'; widthunit='data'; anchor='topleft'; justify='left';
  x1=1957;  y1=72; width=38; textsize=7; textweight='bold';
  label="Boom Times : ";
  output;

  /*--Clear PDV--*/
  call missing (of _all_);

  /*--Draw 4th paragraph continued--*/
  function='textcont'; textweight='normal';  
  label="Two gains in recent decades were followed by returns to levels consistent since " ||
        "the late 1950's.  Since 1997, the index has risen about 83 percent.";
   output;

  /*--Clear PDV--*/
  call missing (of _all_);

  /*--Draw Regime labels--*/
  function='text';  x1space='datavalue'; y1space='layoutpercent'; widthunit='data'; anchor='top'; justify='center';
  x1=1916.5;  y1=57; width=8; textsize=7; label="World War 1"; output;
  x1=1935;   y1=57; width=12; label="Great Depression"; output;
  x1=1943.5; y1=57; width=8; label="World War 2"; output;
  x1=1978;   y1=57; width=8; label="1970's Boom"; output;
  x1=1986.5; y1=57; width=8; label="1980's Boom"; output;
  x1=2002;   y1=101; width=8; label="Currrent Boom"; output;

run;

/*----Create Graph-----*/
ods listing style=htmlblue gpath=&graphs image_dpi=&dpi;
ods graphics / reset noscale width=&w height=&h imagename='GTL_SchillerPLot_V94' antialiasmax=1000;
proc sgrender data=merged template=housing sganno=anno;
run;