Wednesday, January 16, 2013

Make all SAS tables sortable in the output HTML



Most procedures in SAS generate stylish tables in the output HTML files. An option to sort the tables without bothering SAS again will be very handy for people who are familiar with Excel.

The JavaScript libary jQuery has a number of plug-ins. The tablesorter is one of them, which realizes the column-wise sorting. We only need to add several lines of JavaScript below into the very beginning of a SAS output HTML file. Then all tables in the HTML files are able to be sorted easily by clicking on the table head. For example, the resulting HTML from some SAS codes such as proc print data = sashelp.class; run; with the jQuery plug-in will have sortable effect above.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="http://cdn.jsdelivr.net/tablesorter/2.0.5b/jquery.tablesorter.min.js"></script>
<script>
$(document).ready(function( ) {    
   $('.table').tablesorter({widgets: ['zebra']});
});
</script>
How to do it
First we open the HTML file by any text editor such as Windows Notepad, and the head lines of the file by the SAS command proc print data = sashelp.class; run;  will be like --


The second thing is to copy and paste the 7 lines of JavaSctipt above to the HTML file, just under the title tag. Then all is just done. All SAS tables can be sorted now as long as you have Internet connection. No server or other software is needed.

Another way to do it is to use a SAS macro. Andrew has a nice demo on his blog for implementing it.

7 comments:

  1. Charlie, this is an interesting line of work your investigating; marrying up SAS and Javascript libraries. Please keep up the good work!

    ReplyDelete
    Replies
    1. David -- Thanks for your warm words. I just have fun to play around SAS with other languages. Charlie

      Delete
  2. Hi Charlie, This looks awesome! I am unfamilar with java. How can I create this html?

    ReplyDelete
    Replies
    1. Hi Adrienne -- Thanks for your comment. You just need open the HTML file with any text editor, then insert the six lines of JavaScript in the Head of the HTML. That is it^-^ Charlie

      Delete
  3. Very cool. Can you give a full SAS code example? I had a little trouble making mine work. I used the headtext= option with ODS HTML and changed the single quotes to double quotes so I could quote the entire JS script. My server blocked google API, so this is probably the issue.

    ReplyDelete
  4. melondonkey: Here is the SAS code for sorting ODS HTML tables.

    Charlie: Thank you for posting this.

    ReplyDelete
    Replies
    1. Andrew -- Thanks. Your post gave a nice SAS macro. I really like it. Charlie

      Delete