Add a stars-style rating column to your SAS output

Now, the Star-Belly Sneetches
Had bellies with stars.
The Plain-Belly Sneetches
Had none upon thars.
- from "The Sneetches", by Dr. Seuss

Recently a user on the SAS-L mailing list had this challenge: "I would like to display stars in a table (created by PROC REPORT) based on variable values. For example, if value=3, then display five stars with 3 in yellow, and the other two in grey."

In her original post, the user had sketched out an approach that used a custom SAS format with uppercase and lowercase Xs representing the desired "star" states. It turns out that this was very close to a working approach. All she needed to do was apply the same technique that I used to add Harvey Balls to a SAS report. Like the Fix-It-Up Chappie, I'm here to offer the solution.

In my Harvey Balls example, I used ODS ESCAPECHAR and Unicode character codes to add extended characters to my SAS format labels. There is a Unicode character for a star (HEX 2605), so the same approach can work. In addition, the ESCAPECHAR sequence can set off style instructions, such as a color definition. This allows you to control the character color "mid-stream". For example, this sequence specifies a red star followed by a gray star:

 ~{style [color=red] ~{unicode '2605'x}} ~{style [color=ygr] ~{unicode '2605'x}}

As it happens, I keep some movie rating data that I downloaded from my Netflix account history (a very rich source of trivial SAS examples). When I apply a custom "star" format to my data, I can produce a report like this:
Here's my SAS format and the PROC PRINT code that references it. The format and report look perfect in HTML, PDF and RTF output:

/* ODS EXCAPECHAR needed to set style/unicode cues */
ods escapechar='~';
/* Captured these in macro variables for readability and */
/* easy maintenance                                      */
%let graystar = ~{style [color=ygr] ~{unicode '2605'x}};
%let redstar =  ~{style [color=red] ~{unicode '2605'x}};
/* using a 4-star system where 1 means "zero stars" */
/* and 5 means the full "4 stars"                   */
proc format lib=work;
value stars
 1 = "&graystar.&graystar.&graystar.&graystar."
 2 = "&redstar.&graystar.&graystar.&graystar." 
 3 = "&redstar.&redstar.&graystar.&graystar." 
 4 = "&redstar.&redstar.&redstar.&graystar." 
 5 = "&redstar.&redstar.&redstar.&redstar." 
title "Movie titles with STARS on THARS";
proc print data=work.movies noobs;
  format rating stars.;

If you want to try it yourself, you can download my program with test data:

>> Complete SAS program with test data

This program should work in SAS Display Manager, SAS Enterprise Guide (select HTML output), and even the SAS University Edition (SAS Studio).

tags: ods escapechar, proc format


  1. Posted June 10, 2014 at 10:45 pm | Permalink

    Tricks of the trade... Great example and report. Might need to rename your blog to from SAS Dummy to SAS Star! :-)

  2. Posted June 11, 2014 at 10:57 am | Permalink

    Hi Chris, great post! Short and sweet. Great visual. Could please also specify how to show yellow/golden stars?

    • Chris Hemedinger Chris Hemedinger
      Posted June 11, 2014 at 11:06 am | Permalink

      Sure thing, Leonid. Simply replace "red" with "gold".

      %let goldstar = ~{style [color=gold] ~{unicode '2605'x}};
  3. Dayle
    Posted July 30, 2014 at 7:56 am | Permalink

    I was able to run the report excatly as you posted the code but the stars were not colored. Is there a particular version of EG that I need or is there something I need to turn on.

    • Chris Hemedinger Chris Hemedinger
      Posted July 30, 2014 at 8:56 am | Permalink


      If using EG, try changing your results output to HTML instead of SAS Report (the default) -- see if that helps.

      • Dayle
        Posted July 30, 2014 at 9:09 am | Permalink

        thanks - it looks good

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <p> <pre lang="" line="" escaped=""> <q cite=""> <strike> <strong>