Three ways to specify colors in SAS statistical graphics procedures

What’s in a name? As Shakespeare’s Juliet said, “That which we call a rose / By any other name would smell as sweet.”
A similar statement holds true for the names of colors in SAS:
“Rose” by any other name would look as red!

SAS enables you to specify a color in several ways. This article shows how to use color names and RGB colors to specify colors in the SAS statistical graphics procedures, such as PROC SGPLOT. It gathers together color resources in a single location, so you might want to bookmark this article for future reference.

Pre-defined SAS color names

SAS knows dozens of names of colors.
There’s “red” and “brown,” of course, but also designer colors such as “aquamarine,” “chartreuse,” “khaki,” and “teal.” The following call to the SGPLOT procedure creates a scatter plot for which the marker color is set to “rose”:

proc sgplot data=sashelp.class;
scatter x=height y=weight / markerattrs=(size=14 symbol=CircleFilled
                            color="Rose" );
run;

If you want to see some of the color names that SAS recognizes, run the following statements, which will print a list of colors to the SAS log:

proc registry list startat="COLORNAMES";
run;

The SAS color-naming convention

Some of the more “imaginative” names that SAS supports (such as “Peru,” “Thistle,” and “Gainsboro”) might be unfamiliar. Fortunately,
SAS has a color-naming scheme that enables you to specify a descriptive name for many colors, such as “Red,” “Light Gray,” or “Dark Green.”
To use the color-naming scheme, choose one value from each of the following categories:

  1. Lightness: Black (*), Very Dark, Dark, Medium (default), Light, Very Light, or White (*)
  2. Saturation: Gray (*), Grayish, Moderate, Strong, Vivid (default)
  3. Hue: Blue, Purple, Red, Orange or Brown, Yellow, Green

So, for example, valid names for colors include the following:

  • “Very Dark Grayish Blue”
  • “Dark Moderate Purple”
  • “Strong Red” (default value for Lightness)
  • “Light Brown” (default value for Saturation)

A few exceptions to the “choose one from each category” rule are indicated by asterisks. If you use Black or White, you cannot specify Saturation or Hue. Similarly, if you specify Gray, you cannot specify a Hue.

You can also specify a mixture of hues by specifying two adjacent hues in the list. Optionally, you can use the -ish suffix to diminish a hue, as in the following examples:

  • “Medium Strong Red Orange”
  • “Light Yellowish Green”
  • “Light Strong Brownish Orange”

For example, the following scatter plot sets the color of markers by using the SAS color-naming scheme:

proc sgplot data=sashelp.class;
scatter x=height y=weight / markerattrs=(size=14 symbol=CircleFilled
                            color="Light Strong Brownish Orange" );
run;

Representing colors by their RGB values

SAS also enables you to specify a color by using the red-green-blue (RGB) color model.
The RGB color model is an additive model in which a color is specified in terms of red, green, and blue components. Each component ranges from 0 (which indicates the absence of a component) to 255 (which indicates the full presence of a component). A color is therefore a triplet of values that indicates the relative proportions of red, green, and blue. For example, the RGB triplet (255, 96, 96) represents a color that has a maximum amount of red and a small amount of green and blue.

The drawback of the RGB model is readability. If I write a program and specify a color as the RGB triplet (255, 96, 96), it is difficult to know what color is going to be produced.
It turns out that this color is exactly “Rose,” but you might have had a hard time predicting that in advance.

Most of the time, RGB triplets are compactly represented by a hexadecimal integer. The prefix CX tells SAS to interpret the integer as a color. You can find the hexadecimal representation of a color such as “Rose” by using the handy reference chart that lists SAS colors and their hexadecimal values.

The chart says that the RGB color for “Rose” is CXFF6060. This value is read two digits at a time:

  • CX means that the value is a color
  • FF is the hexadecimal (base-16) value for 16*15 + 15 = 255, which is the red component
  • 60 is the hexadecimal value for 16*6 + 0 = 96, which is the green component
  • 60 is the blue component

Consequently, the following statements reproduce the first image in this article:

/* "Rose" = RGB(255,96,96) = cxFF6060 */
proc sgplot data=sashelp.class;
scatter x=height y=weight / markerattrs=(size=14 symbol=CircleFilled
                            color=cxFF6060 );
run;

If you are a hard-core programmer, you’re probably already thinking about how to write a SAS macro that converts a triplet of RGB values into a hexadecimal color. If you want to try to construct the macro yourself, stop reading now!

You can use the following SAS macro to create a hexadecimal value from a triplet of RGB colors. It’s easy to use the PUT function to construct the hexadecimal value inside of a DATA step, but it is slightly more challenging to use the macro language to construct an “in-line” string that can be used outside of the DATA step. You can use the following macro to specify the COLOR= option for statement in the SGPLOT procedure:

/* convert integer 0--255 to 2 digit hex 00-FF */
%macro hex2(n);
  %local digits n1 n2;
  %let digits = 0123456789ABCDEF;
  %let n1 = %substr(&digits, &n / 16 + 1, 1);
  %let n2 = %substr(&digits, &n - &n / 16 * 16 + 1, 1);
  &n1&n2
%mend hex2;
 
/* convert RGB triplet (r,g,b) to SAS color in hexadecimal. 
   The r, g, and b parameters are integers in the range 0--255 */
%macro RGB(r,g,b);
  %cmpres(CX%hex2(&r)%hex2(&g)%hex2(&b))
%mend RGB;
 
data A;
put "Rose = %RGB(255,96,96)";
run;
 
proc sgplot data=sashelp.class;
scatter x=height y=weight / markerattrs=(size=14 symbol=CircleFilled
                            color=%RGB(255,96,96) );
run;

Because (255, 96, 96) specifies the same color as “rose,” the image is identical to the first graph in this article.

So what’s in a name? A lot of information! But in SAS, “rose” by the name of CXFF6060 looks just as sweet!

tags: Getting Started, SAS Programming, Statistical Graphics