BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Hi,

I know I've seen this on here before but can't seem to find it at all.

I'm using proc report to output some data and would like to alternate the background of the row colors based on the groups, so each group is differentiable from each other. I know I need to use a compute block and call define my style but not sure of the logic to get each group a different colour.

Pointers in the right direction are appreciated.

Thanks!

Sample

ID Value Colour
1 2 white
1 3 white
2 3 lightgrey
2 4 lightgrey
6 3 white
8 2 lightgrey



Message was edited by: Reeza null
1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ
Hi:
Then you can use the _ROW_ value in your CALL DEFINE statement instead of _COL_. If you only wanted the GROUP headers to be a certain color, then you could just use a format. You can still use a format to determine the colors of the rows, but the technique will be different based on whether you have 1 row per group (#1b) or multiple rows per group (#1c).

cynthia
[pre]
proc format;
value $prod "Men's Dress" = 'lightblue'
"Women's Dress" = 'lightpink'
"Men's Casual" = 'lightblue'
"Women's Casual" = 'lightpink'
"Sandal" = 'beige'
"Boot" = 'cxdddddd'
"Slipper" = 'yellow'
"Sport Shoe" = 'cyan';
run;

title;
ods listing close;

ods html file='c:\temp\grpcolor.html' style=sasweb;
proc report data=sashelp.shoes nowd;
title '1a) coloring only the group header';
where region in ('Asia', 'Canada', 'Pacific');
column product sales,region;
define product / group
style(column)={background=$prod. foreground=black};
define region / across 'Region';
define sales/ sum ' ';
run;

proc report data=sashelp.shoes nowd;
title '1b) coloring the entire row';
where region in ('Asia', 'Canada', 'Pacific');
column product sales,region;
define product / group ;
define region / across 'Region';
define sales/ sum ' ';
compute product;
svar = catt('style={background=',put(product,$prod.),' foreground=black}');
call define(_row_,'style',svar);
endcomp;
run;

proc report data=sashelp.shoes nowd;
title '1c) coloring multiple rows -- need to "hold" value of group var on each row';
where region in ('Asia', 'Canada', 'Pacific');
column product usecolor region sales;
define product / group ;
define usecolor / computed /* noprint */;
define region / group 'Region';
define sales/ sum 'Total';
break before product / ;
compute usecolor / character length=30;
if upcase(_break_) = 'PRODUCT' then do;
holdprod = product;
end;
usecolor = put(holdprod,$prod.);
svar = catt('style={background=',usecolor,' foreground=black}');
call define(_row_,'style',svar);
endcomp;
run;

ods html close;
[/pre]

View solution in original post

4 REPLIES 4
syam_india_kochi
Calcite | Level 5
Hi,
If I understood you correctly, the followig can be the solution

proc report data=xyz;
column a b ;
define a/"a" style(column)=[backgroundcolor=maroon foreground=white];
define b/"b" style(column)=[backgroundcolor=red foreground=white];
run;

And alternative method could be using the call define style option in the computed column.


ex:
proc report data=xyz;
column a b ;
define a/"a" style(column)=[backgroundcolor=maroon foreground=white];

compute a;
if a="some value" then do;
call define(_col_,"style","style=[backgroundcolor=maroon foreground=white]");
end;
endcomp;

** you can use some condition to format your output;
**using proc format to customise 'style' options are also reliable;
run;



Cheers..!!
Reeza
Super User
Sorry, I don't think I was clear before, looking for varying the row colours based on group not the column colours.

Thanks!
Cynthia_sas
SAS Super FREQ
Hi:
Then you can use the _ROW_ value in your CALL DEFINE statement instead of _COL_. If you only wanted the GROUP headers to be a certain color, then you could just use a format. You can still use a format to determine the colors of the rows, but the technique will be different based on whether you have 1 row per group (#1b) or multiple rows per group (#1c).

cynthia
[pre]
proc format;
value $prod "Men's Dress" = 'lightblue'
"Women's Dress" = 'lightpink'
"Men's Casual" = 'lightblue'
"Women's Casual" = 'lightpink'
"Sandal" = 'beige'
"Boot" = 'cxdddddd'
"Slipper" = 'yellow'
"Sport Shoe" = 'cyan';
run;

title;
ods listing close;

ods html file='c:\temp\grpcolor.html' style=sasweb;
proc report data=sashelp.shoes nowd;
title '1a) coloring only the group header';
where region in ('Asia', 'Canada', 'Pacific');
column product sales,region;
define product / group
style(column)={background=$prod. foreground=black};
define region / across 'Region';
define sales/ sum ' ';
run;

proc report data=sashelp.shoes nowd;
title '1b) coloring the entire row';
where region in ('Asia', 'Canada', 'Pacific');
column product sales,region;
define product / group ;
define region / across 'Region';
define sales/ sum ' ';
compute product;
svar = catt('style={background=',put(product,$prod.),' foreground=black}');
call define(_row_,'style',svar);
endcomp;
run;

proc report data=sashelp.shoes nowd;
title '1c) coloring multiple rows -- need to "hold" value of group var on each row';
where region in ('Asia', 'Canada', 'Pacific');
column product usecolor region sales;
define product / group ;
define usecolor / computed /* noprint */;
define region / group 'Region';
define sales/ sum 'Total';
break before product / ;
compute usecolor / character length=30;
if upcase(_break_) = 'PRODUCT' then do;
holdprod = product;
end;
usecolor = put(holdprod,$prod.);
svar = catt('style={background=',usecolor,' foreground=black}');
call define(_row_,'style',svar);
endcomp;
run;

ods html close;
[/pre]
Reeza
Super User
Thanks!

I wanted 'banding' so I used a combination of methods, adding some things in my datastep and then in the proc report, the code provided definitely helped in getting there!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 2606 views
  • 0 likes
  • 3 in conversation