The SAS Output Delivery System and reporting techniques

Alternate Group Colors based on Group Var in Proc Report

Reply
Super User
Posts: 19,869

Alternate Group Colors based on Group Var in Proc Report

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
Occasional Contributor
Posts: 15

Re: Alternate Group Colors based on Group Var in Proc Report

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..!!
Super User
Posts: 19,869

Re: Alternate Group Colors based on Group Var in Proc Report

Posted in reply to syam_india_kochi
Sorry, I don't think I was clear before, looking for varying the row colours based on group not the column colours.

Thanks!
SAS Super FREQ
Posts: 8,868

Re: Alternate Group Colors based on Group Var in Proc Report

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]
Super User
Posts: 19,869

Re: Alternate Group Colors based on Group Var in Proc Report

Posted in reply to Cynthia_sas
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!
Ask a Question
Discussion stats
  • 4 replies
  • 674 views
  • 0 likes
  • 3 in conversation