SAS and D3.js (2): a macro to draw dynamic bar chart

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

More and more statistical softwares are able to create interactive web application. The recent shiny package by RStuido is a good example. Besides, in the world that JavaScript rules the browsers, D3.js seems to be very promising for rich data visualization.

One of the greatest things about D3.js is that it will bring interactivity to the web page by just a few JavaScript lines.
For example, people like to see different angles of a bar chart to discover information. First the bar chart may be displayed from left to right alphabetically. Second it is sorted by each category’s frequency.
At the web era, a single dynamic bar chart would satisfy those requirements.

In SAS, we can wrap those effects into single macros for repeated usage.

1. Transform SAS dataset to JSON 
This time the SASHELP.CLASS dataset in SAS is still used. Since the objective is going to be a simple vertical bar chart, I only transform two variables WEIGHT and NAME into JSON format.

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

 2. Draw the bar chart
The x axis is corresponding to the kids’ name from the SASHELP.CLASS dataset, while the y axis is for those kids’ weight. The output file is name as vbar.html at the C drive. I add a check box on the top for user to turn on/off the sortable effect on the resulting SVG. Therefore The user will have the freedom to control the bar chart.
 

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