SAS and D3.js (1): a macro to draw scatter plot

This post was kindly contributed by SAS ANALYSIS - go there to comment and to read the full post.

HTML is the default output format for SAS since 9.3. Implementing the popular JavaScipt frameworks such as jQuery and D3.js will allow some dynamic effects to the final HTML results. In those cases the HTML file is static, and thus no HTTP server is needed.

At the beginning, I come up with a simple SAS macro to realize the scatter plot which is a SVG format picture on the canvas of a HTML page.

 1. Transform SAS dataset to JSON 
JSON is the dominant data format for web. Actually I found that SAS’s dataset can be easily transformed to JSON . Here the SASHELP.CLASS dataset is transformed to JSON by two consecutive DATA STEPs. Of course, a macro can be a beter way to simplify those steps, like Wade Buskirk’s JSON.sas.

data class;
   set sashelp.class nobs = nobs;
   _weight = cats('{', '"weight":', weight,  ',');
   _height = cats('"height":', height,  ',');
   _age = cats('"age":', age,  ',');
   _name = cats('"name":','"', name, '"', ',');
   if _n_ ne nobs then 
      _sex = cats('"sex":','"', sex, '"', '},');
   else _sex = cats('"sex":','"', sex, '"', '}');
   keep _:;
run;
data class_JSON;
   set class;
   length string $250.;
   string = cats(of _all_);
   keep string;
run;

2. Draw the scatterplot
In the macro below, I set 7 parameters in the case to specify the incoming data, some plotting parameters such as x axis and y axis, and output path. The purpose of the macro is to wrap the HTML, CSS and JavaScript code and export the final HTML file. Besides the parameters in the SAS macro, every element like the color or style is modifiable in the JS script which is wrapped by the macro.

Finally the scatter plot is drawn on the generated HTML file. D3.js is the future of data visualization. Playing SAS and D3 brings me a lot of fun.

This post was kindly contributed by SAS ANALYSIS - go there to comment and to read the full post.