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.; run; |
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 SAS Studio via SAS OnDemand for Academics.
12 Comments
Tricks of the trade... Great example and report. Might need to rename your blog to from SAS Dummy to SAS Star! :-)
Hi Chris, great post! Short and sweet. Great visual. Could please also specify how to show yellow/golden stars?
Sure thing, Leonid. Simply replace "red" with "gold".
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.
Dayle,
If using EG, try changing your results output to HTML instead of SAS Report (the default) -- see if that helps.
thanks - it looks good
Pingback: Tip for coding your color values in SAS Enterprise Guide - The SAS Dummy
Hi, Chris - we've been using this for a while (although the code CONSTANTLY evolves - now we are adding the accessibility option for Section 508 compliance and PROC ODSTEXT) and I love the stars - what would really be nice is if we could get half-stars like the harvey balls. Now, for some of my reports, I use unicode fraction symbols for the half. I read somewhere that we could possibly add symbols / create our own fonts. This would be a wonderful addition. Any clever font designers out there?
http://support.sas.com/resources/papers/proceedings12/213-2012.pdf
Louise, looks like there is a proposal for a half-filled star in Unicode. Seems like something you could have written!
Well, I have been known to find the answer to a SAS question in one of my own papers - so many (SAS) options, so little time, my brain is full? It *might* have been me, or someone in tech support (Bari/Jane E/Scott H) relaying my desire for half-stars! Thanks!
This seems to work in HTML, but will this work in PDF?
Amazing.