FOCUS AREAS

Graphics

annotated graph
Base SAS: Graph Template Language

%let gpath='.';
%let dpi=100;

data housing;
    input Date Index;
    infile datalines delimiter=',';
datalines;
1890,100
1891,88.011779785
1892,95.421691895
1893,92.297363281
1894,123.98046875
1895,117.45507813
1896,100.30297852
1897,106.51568604
1898,110.18408203
1899,103.85308838
1900,101.57427979
1901,87.326049805
1902,100.47363281
1903,93.074890137
1904,101.85430908
1905,87.248535156
1906,103.52514648
1907,109.31646729
1908,100.81915283
1909,95.380371094
1910,93.110595703
1911,97.543334961
1912,102.3651123
1913,95.408508301
1914,96.977111816
1915,88.148742676
1916,93.72479248
1917,85.009033203
1918,75.566589355
1919,70.513793945
1920,66.07409668
1921,65.614257813
1922,74.796142578
1923,76.350036621
1924,74.28692627
1925,78.162780762
1926,72.490600586
1927,71.380249023
1928,73.282104492
1929,72.614318848
1930,69.491882324
1931,68.645202637
1932,68.337158203
1933,72.865905762
1934,73.27935791
1935,78.069946289
1936,79.414855957
1937,79.717590332
1938,78.464599609
1939,78.549682617
1940,81.730773926
1941,73.815856934
1942,68.502807617
1943,70.922973633
1944,80.308959961
1945,87.75
1946,106.50585938
1947,109.32965088
1948,101.22253418
1949,100.04656982
1950,105.89483643
1951,103.89862061
1952,103.9743042
1953.125,114.71325684
1953.375,115.06854248
1953.625,114.65063477
1953.875,113.84515381
1954.125,114.31225586
1954.375,114.69470215
1954.625,115.01489258
1954.875,115.62036133
1955.125,115.92071533
1955.375,115.12426758
1955.625,115.97302246
1955.875,115.54187012
1956.125,115.04736328
1956.375,114.18054199
1956.625,114.29577637
1956.875,113.79418945
1957.125,114.45196533
1957.375,114.44921875
1957.625,112.83154297
1957.875,113.66638184
1958.125,112.8458252
1958.375,112.53283691
1958.625,111.61523438
1958.875,111.79705811
1959.125,111.24859619
1959.375,111.53375244
1959.625,110.56756592
1959.875,110.45831299
1960.125,110.91589355
1960.375,109.92370605
1960.625,109.79174805
1960.875,109.64953613
1961.125,109.45135498
1961.375,109.25311279
1961.625,108.99731445
1961.875,109.50921631
1962.125,109.82421875
1962.375,109.44897461
1962.625,109.32165527
1962.875,108.72894287
1963.125,109.07867432
1963.375,109.41821289
1963.625,108.85931396
1963.875,109.11950684
1964.125,109.53100586
1964.375,109.11047363
1964.625,109.05450439
1964.875,109.85223389
1965.125,110.18164063
1965.375,108.95318604
1965.625,108.45050049
1965.875,109.26361084
1966.125,109.5144043
1966.375,108.07513428
1966.625,107.519104
1966.875,106.67871094
1967.125,106.71459961
1967.375,106.49804688
1967.625,106.31964111
1967.875,106.38970947
1968.125,105.76531982
1968.375,104.94598389
1968.625,104.49176025
1968.875,105.215271
1969.125,106.45233154
1969.375,105.66876221
1969.625,106.25543213
1969.875,106.95306396
1970.125,107.85095215
1970.375,107.76177979
1970.625,108.43994141
1970.875,109.55780029
1971.125,109.16906738
1971.375,109.53070068
1971.625,109.425354
1971.875,110.19000244
1972.125,110.60229492
1972.375,110.190979
1972.625,111.05621338
1972.875,110.08984375
1973.125,109.17590332
1973.375,106.96990967
1973.625,106.21295166
1973.875,105.07611084
1974.125,105.20422363
1974.375,104.05541992
1974.625,103.11523438
1974.875,102.50512695
1975.125,105.16320801
1975.375,106.06176758
1975.625,103.08227539
1975.875,103.45495605
1976.125,103.57330322
1976.375,106.27581787
1976.625,105.46411133
1976.875,105.57519531
1977.125,107.8916626
1977.375,110.02197266
1977.625,110.64471436
1977.875,113.3258667
1978.125,115.26794434
1978.375,117.34655762
1978.625,117.39331055
1978.875,118.39471436
1979.125,121.80523682
1979.375,121.38684082
1979.625,119.79431152
1979.875,118.81567383
1980.125,116.71234131
1980.375,113.83905029
1980.625,114.93560791
1980.875,112.36773682
1981.125,110.20465088
1981.375,110.18554688
1981.625,108.63549805
1981.875,106.87243652
1982.125,106.97906494
1982.375,106.80023193
1982.625,103.30963135
1982.875,103.31292725
1983.125,105.63879395
1983.375,106.04333496
1983.625,105.31781006
1983.875,104.55737305
1984.125,105.16656494
1984.375,105.63354492
1984.625,105.64807129
1984.875,105.43572998
1985.125,106.69903564
1985.375,106.97503662
1985.625,107.88446045
1985.875,108.27764893
1986.125,109.48455811
1986.375,112.77532959
1986.625,113.69763184
1986.875,114.64294434
1987.125,116.09942627
1987.375,118.35852051
1987.625,119.46398926
1987.875,119.46221924
1988.125,119.93066406
1988.375,123.11798096
1988.625,123.82373047
1988.875,123.31915283
1989.125,124.48223877
1989.375,125.79052734
1989.625,125.84790039
1989.875,124.894104
1990.125,123.47259521
1990.375,123.39208984
1990.625,121.04693604
1990.875,116.28735352
1991.125,113.54333496
1991.375,115.07147217
1991.625,114.85308838
1991.875,113.07751465
1992.125,111.97686768
1992.375,112.61358643
1992.625,111.69354248
1992.875,109.7008667
1993.125,108.67675781
1993.375,109.09442139
1993.625,109.62817383
1993.875,108.43572998
1994.125,108.84790039
1994.375,110.22094727
1994.625,109.71661377
1994.875,108.43603516
1995.125,107.65112305
1995.375,108.62731934
1995.625,109.00512695
1995.875,107.66656494
1996.125,107.31329346
1996.375,108.00616455
1996.625,108.33325195
1996.875,106.73364258
1997.125,107.03417969
1997.375,108.54681396
1997.625,109.40722656
1997.875,109.21630859
1998.125,110.3883667
1998.375,113.09423828
1998.625,114.90466309
1998.875,115.24523926
1999.125,116.64355469
1999.375,118.65368652
1999.625,121.14447021
1999.875,121.62322998
2000.125,123.29931641
2000.375,126.0803833
2000.625,128.06933594
2000.875,129.06396484
2001.125,129.8815918
2001.375,132.5838623
2001.625,135.43054199
2001.875,136.13317871
2002.125,138.67443848
2002.375,141.50012207
2002.625,145.75976563
2002.875,147.60754395
2003.125,149.45898438
2003.375,151.96374512
2003.625,156.64575195
2003.875,160.07946777
2004.125,164.36816406
2004.375,169.29321289
2004.625,174.20654297
2004.875,177.77661133
2005.125,184.65332031
2005.375,188.98474121
2005.625,195.0065918
2005.875,195.35095215
2006.125,198.01135254
2006.375,196.17858887
2006.625,192.38916016
2006.875,192.28747559
2007.125,190.046875
2007.375,184.44897461
2007.625,179.86328125
2007.875,170.09069824
2008.125,157.13220215
2008.375,151.07128906
2008.625,142.38366699
2008.875,133.97460938
2009.125,127.32629395
2009.375,129.99780273
2009.625,132.89855957
2009.875,130.93701172
2010.125,126.88287354
2010.375,131.98474121
2010.625,129.5300293
2010.875,124.58575439
2011.125,118.63641357
2011.375,120.52478027
2011.625,120.08374023
;
run;

/*--Add some forecasted values to the end--*/
data forecast;
  set 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;
/*  proc print;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     
;
/*proc print;run;*/

data merged;
  set forecast events;
  run;
/*proc print;run;*/


/*--Graph with Annotate--*/
proc template;
  define statgraph housing;
    begingraph;
      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; /* pull in annotations from PROC SGRENDER sganno= */
      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='wallpercent'; y1space='wallpercent'; 
  x1=98;  y1=100; x2=2006.4; y2=200; scale=0.1;
  output;
 
  /*--Clear PDV--*/
  call missing (of _all_);

  /*--Draw the label on top right--*/
  function='text';  anchor='left'; textweight='bold';
  x1=91;  y1=97; 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=45;
  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=1911;  y1=72; width=45; textsize=7; textweight='bold';
  label="Decline and Run-Up : ";
  output;

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

  /*--Draw 3rd paragraph continued--*/
/*  function='textcont';  drawspace='layoutpercent'; anchor='topleft'; justify='left'; widthunit='percent';*/
  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=58; width=8; textsize=7; label="World War 1"; output;
  x1=1935;   y1=58; width=12; label="Great Depression"; output;
  x1=1943.5; y1=58; width=8; label="World War 2"; output;
  x1=1978;   y1=58; width=8; label="1970's Boom"; output;
  x1=1986.5; y1=58; width=8; label="1980's Boom"; output;
  x1=2002;   y1=102; width=8; label="Current Boom"; output;

run;

/*proc print;run;*/

/*--Render graph with annotations--*/
ods _all_ close;
ods listing gpath=&gpath image_dpi=&dpi;

ods graphics / reset width=7.48in height=4.4in imagename='V94_Schiller_Anno' 
              antialiasmax=1000; 
proc sgrender data=merged template=housing sganno=anno;
run;

ods _all_ close;