Hello,
I'm wondering if there is a way to output a table in color by Proc Tabulate. For example, I would like to have the "year" in different colors in the following code. Is it doable?
proc tabulate data=&Pathogen.;
Class year state Quarter/ missing;
var Test ;
table State*(test*f=10.0),year;
where Test__groups_ in ('>= 1');
Label State="State" Test="Test Group >=1 counts";
Title '&Pathogen._TestGrpLt1_count';
run;
you could try to use technique called "traffic lights", here is an example in proc report:
https://support.sas.com/resources/papers/proceedings11/290-2011.pdf
but you can adopt it to tabulate too, which can be seen here:
https://support.sas.com/resources/papers/proceedings/proceedings/sugi31/142-31.pdf
Bart
proc tabulate data=&Pathogen.;
Class year state Quarter/ missing;
var Test ;
table State*(test*f=10.0),year*[style=[COLOR=RED]];
where Test__groups_ in ('>= 1');
Label State="State" Test="Test Group >=1 counts";
Title '&Pathogen._TestGrpLt1_count';
run;
did you try something like "style=" ?
Bart
you could try to use technique called "traffic lights", here is an example in proc report:
https://support.sas.com/resources/papers/proceedings11/290-2011.pdf
but you can adopt it to tabulate too, which can be seen here:
https://support.sas.com/resources/papers/proceedings/proceedings/sugi31/142-31.pdf
Bart
To assign colors based on the value of a variable create a custom format for the color you want and associate it with the variable/values you want.
Example (that you can actually run because you should have the SASHELP.CLASS training data set)
Proc format;
value $sex_color
'F'='lightred'
'M'='blue'
;
value agegrp
low-12='Preteen'
13-17 ='Teen'
;
value agecolor
low-12 = 'gold'
13-17 = 'red'
;
value valcolor
low -<60 = 'darkgreen'
60 - high= 'cyan'
;
run;
proc tabulate data=sashelp.class;
class sex age ;
classlev sex /style=[color=$sex_color.];
classlev age /style=[backgroundcolor=agecolor.];
format age agegrp.;
var height;
table sex,
age*height*mean*[style=[background=valcolor.]]
;
run;
SAS has multiple ways to designate color so is worth looking at the documentation depending on what you actually want.
The formats a examples of how to assign colors and one text (agegrp) based on ranges of values or list of values .
The CLASSLEV will allow you to assign properties to CLASS variables which can keep the actual table syntax a bit cleaner and is very helpful if you are using Proc tabulate to make more than one table with a single procedure call to keep everything set the same without having to duplicate it in the body of the table. Note the use of the Format name associated with the item to color. I show an example of both the text (Color=) and the cell background (backgroundcolor= ) color settings.
Also an example of providing appearance change based on the result of a statistic.
Proc format;
value year color
2021 = 'lighttan'
2022 = 'lightpink'
2023 = 'lightblue'
;
run;
proc tabulate data=&Pathogen.;
Class year state Quarter/ missing;
classlev year /style=[backgroundcolor=yearcolor.];
var Positive Test ;
table State*(positive*f=10.0*[style=olive] test*f=10.0),year*[style=[background=yearcolor.]]*(quarter all);
Label State="State" Positive="Total Positive counts" Test="Total Test counts";
Title '&Pathogen._test#_sum';
run;
Please let me know where I should modify my code.
@ybz12003 wrote:
Proc format; value year color 2021 = 'lighttan' 2022 = 'lightpink' 2023 = 'lightblue' ; run; proc tabulate data=&Pathogen.; Class year state Quarter/ missing; classlev year /style=[backgroundcolor=yearcolor.]; var Positive Test ; table State*(positive*f=10.0*[style=olive] test*f=10.0),year*[style=[background=yearcolor.]]*(quarter all); Label State="State" Positive="Total Positive counts" Test="Total Test counts"; Title '&Pathogen._test#_sum'; run;
Please let me know where I should modify my code.
FWIW, I find that the basic pink is awful light and generally use lightred or verylightred instead when I want a pinkish shade.
Hi:
You might also want to search in the documentation and on the support web site. For example, in this example
contained in this Tech Support note: https://support.sas.com/kb/25/401.html
You can see that the color for each column inherits the color of the header under which the column is nested. This is known as using style=<parent> which is a style override feature of PROC TABULATE. This feature allows you to do either horizontal or vertical "banding" based on the style characteristics that you associate usually with a CLASSLEV header variable.
Cynthia
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.