BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
happy_mouth
Obsidian | Level 7

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

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:

decimal_hex_colors.png

Those colors look pretty much the same to me. Here's the code I used:

the_code.png

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:

color_picker.png

 

Cynthia

View solution in original post

6 REPLIES 6
Reeza
Super User

@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?

PGStats
Opal | Level 21

Could you show us a picture (or screen capture) of the comparison?

PG
Cynthia_sas
SAS Super FREQ

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:

decimal_hex_colors.png

Those colors look pretty much the same to me. Here's the code I used:

the_code.png

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:

color_picker.png

 

Cynthia

happy_mouth
Obsidian | Level 7

Hi Cynthia,

 

Thank you. Your solution works!

 

--David

ballardw
Super User

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.;
Cecillia_Mao
Obsidian | Level 7

Also from RGB percentage to RGB codes:

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=graphref&docsetTarget=p0e...

 

/* 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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 3185 views
  • 8 likes
  • 6 in conversation