Hi, Is there a way to convert Microsoft Office RGB colors to the exact same colors in SAS?
I've tried using the following code (SAS version 9.4):
/* 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 colours;
put "MyPeach = %RGB(250,192,144)";
put "MyGreen = %RGB(79,98,40)";
put "MyBlue = %RGB(31,73,125)";
run;
The above code almost works, but the colors it creates in SAS look slightly different from the ones created in Office. I need them to be exactly the same.
Any suggestions?
Thanks!
Hi,
I find color conversion to be very accurate, when I just use the HEX. format to do the conversion from decimal to hex. Using my standard method, this is what I see comparing Powerpoint and the decimal numbers with ODS HTML and the HEX values:
Those colors look pretty much the same to me. Here's the code I used:
I'm not going to address your technique in your macro because it's not entirely clear what the disposition of the CX color value is to me for how you're going to use it.
Without more of an example from you, this is going to be hard to make more concrete suggestions. But the other thing I do is always go to the W3schools color picker here: https://www.w3schools.com/colors/colors_picker.asp which very nicely allows you to enter RGB values and then shows you the hex. I do really trust HEX2 in my PUT function, but the thing I like about the W3Schools site is that they show other shades of that color family and there might be a color I like better. Here's what the BLUE looks like on the W3Schools site:
Cynthia
@happy_mouth wrote:
Hi, Is there a way to convert Microsoft Office RGB colors to the exact same colors in SAS?
I've tried using the following code (SAS version 9.4):
/* 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 colours;
put "MyPeach = %RGB(250,192,144)";
put "MyGreen = %RGB(79,98,40)";
put "MyBlue = %RGB(31,73,125)";
run;
The above code almost works, but the colors it creates in SAS look slightly different from the ones created in Office. I need them to be exactly the same.
Any suggestions?
Thanks!
Do you have any transparency settings in either SAS or Office?
Could you show us a picture (or screen capture) of the comparison?
Hi,
I find color conversion to be very accurate, when I just use the HEX. format to do the conversion from decimal to hex. Using my standard method, this is what I see comparing Powerpoint and the decimal numbers with ODS HTML and the HEX values:
Those colors look pretty much the same to me. Here's the code I used:
I'm not going to address your technique in your macro because it's not entirely clear what the disposition of the CX color value is to me for how you're going to use it.
Without more of an example from you, this is going to be hard to make more concrete suggestions. But the other thing I do is always go to the W3schools color picker here: https://www.w3schools.com/colors/colors_picker.asp which very nicely allows you to enter RGB values and then shows you the hex. I do really trust HEX2 in my PUT function, but the thing I like about the W3Schools site is that they show other shades of that color family and there might be a color I like better. Here's what the BLUE looks like on the W3Schools site:
Cynthia
Hi Cynthia,
Thank you. Your solution works!
--David
Try running this code:
%colormac;
You may have a number of SAS supplied macros that are designed for this purpose, including one named RGB. If you don't get an undefined macro from that code then there will also be a bit about how to request details on using the color macros. The call to the SAS RGB macro looks amazing like yours.
Or you may want to examine this code as an alternative approach to string manipulation:
%let hex = %sysfunc(putn(255,hex2.)); %put &hex.;
Also from RGB percentage to RGB codes:
/* Compute the RGB percentages */
data _null_;
r = 231;
g = 179;
b = 180;
call symputx("r", round((r/255)*100));
call symputx("g", round((g/255)*100));
call symputx("b", round((b/255)*100));
run;
/* Convert to RGB color name */
%COLORMAC;
data _null_;
put "%RGB(&r,&g,&b)";
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.