Generating amazing graphics from SAS without SAS/Graph

July, 2008
by Phil Mason

I was at SAS Global Forum in San Antonio a while back, and I remember seeing a demo booth about SAS BI Dashboard. It was showing some amazing looking graphics that I struggled to imagine could be possible using SAS, although it was a SAS product. I asked the Tech Support guy how they were generated and was surprised that they were actually produced as an Adobe Flash object. It seemed that SAS had produced some Flash objects and integrated them into the rest of SAS to become this new product - which was not yet available. I greatly look forward to being able to try this out when it does become available, but I was sure that SAS could do something similar in the meantime.

I have been developing a "Rich Internet Application" using SAS for the last few months, which uses a lot of Javascript, AJAX, and HTML to make it very interactive. A newsletter I receive called "The Code Project" has alerted me to the many resources available to make powerful RIAs. One such resource I discovered this way is XML/SWF Charts (http://www.maani.us/xml_charts/). These are a small free download of a number of Adobe Flash objects. You put them onto your web server and you can then make some amazing looking graphs, very easily.

The data for these graphs is provided in XML form. To make a graph you merely have to construct an XML file containing the data and an HTML file with some specific tags which embed the Flash Object and pass it parameters to define the graph(s) you want to create. Here is a SAS program that creates a graph in this way.

* summarise some data that I will then graph ;

proc summary data=sashelp.shoes nway ;

  class product ;

  var stores sales inventory returns ;

  output out=shoes sum= ;

run ;

 

%let xml=shoes.xml ;

data _null_ ;

  file "c:\wamp\www\&xml" ; * this is a location on my web server that I will put the XML ;

  if _n_=1 then

    do ;

      put '<chart><chart_data>' ;

      put '<row><null/>' ;

      put '<string>sales</string>' ;

      put '<string>inventory</string>' ;

      put '</row>' ;

    end ;

  set shoes end=end ;

  format sales inventory 12.2 ;

  put '<row>' ;

  put '<string>' product +(-1) '</string>' ;

  put '<number>' sales +(-1) '</number>' ;

  put '<number>' inventory +(-1) '</number>' ;

  put '</row>' ;

  if end then

    do ;

      put '</chart_data>' ;

      put "<chart_transition type='drop' delay='1' duration='1' order='series' />" ;

      put '</chart>' ;

    end ;

run ;

data _null_ ;

  file 'c:\wamp\www\swf_shoes.html' ; * this is where I put the HTML, also on the web server ;

  put '<HTML><BODY bgcolor="#FFFFFF">' ;

  put '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' ;

  put 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"' ;

  put 'WIDTH="800" HEIGHT="500" id="charts" ALIGN="">' ;

  put '<PARAM NAME=movie VALUE="charts.swf?library_path=charts_library&xml_source=' "&xml" '">' ;

  put '<PARAM NAME=quality VALUE=high>' ;

  put '<PARAM NAME=bgcolor VALUE=#666666>' ;

  put '<EMBED src="charts.swf?library_path=charts_library&xml_source=' "&xml" '"' ;

  put 'quality=high bgcolor=#666666 WIDTH="800" HEIGHT="500" NAME="charts" ALIGN="" swLiveConnect="true"' ;

  put 'TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">' ;

  put '</EMBED></OBJECT></BODY></HTML>' ;

run ;

To understand the HTML generated you can look at the web site which documents this very well. You can also see a gallery showing all the kinds of graphs that you can produce here.

The SAS code is fairly simple. You need to ensure you make an XML file in the precise format required by the flash object. The object is unforgiving and any deviations from the format required can give unpredictable results. However, with a little work this code could be made into a more generalized macro to produce graphs of all 20 types that the object supports.

If you click here you can see the graph actually produced by the HTML which the SAS program creates.