1
votes

A sample SAS Proc report code is below. I want to change the color of the header background and foreground for one value of the across variable. I used a compute block to change the column background to light gray using the absolute column references c4 and c5. How do I change the header style attributes for c4 and c5 to background=gainboro and foreground=black?

data test;
length name $ 10 disease $ 10.;
infile datalines dsd;
input name $ disease cases rate;
datalines;
State,Fever,4847,25.16
State,Cold,25632,131.5
State,Flu,103825,535.82
Lincoln,Fever,3920,44.17
Lincoln,Cold,16913,190.18
Lincoln,Flu,62965,735.39
Washington,Fever,827,56.56
Washington,Cold,3609,234.26
Washington,Flu,16610,1078.8
Kings,Fever,1026,37.45
Kings,Cold,4984,181.85
Kings,Flu,18388,694.33
Sussex,Fever,1411,78.38
Sussex,Cold,5515,300.46
Sussex,Flu,13881,813.11
Queens,Fever,616,26.03
Queens,Cold,2496,107.75
Queens,Flu,12518,558.09
;
run;


proc report data=test nowd headline headskip
STYLE(Header)={background=charcoal foreground=white }
style(column)={background=gray foreground=black}
style(report)=[rules=rows bordercolor=white];
columns (name disease,(cases rate));
define name/group order=data 'County' style(column)={background=lighttgray} style(header)=[bordertopcolor=gainsboro background=gainsboro foreground=black];
define disease/across '' order=data  ;
define cases/'Cases' format=comma9. ;
define rate/'Rate' format=comma12.1 ;
compute cases;
    call define('_c4_','style','style={background=lighttgray}');
    call define('_c5_','style','style={background=lighttgray}');
endcomp;
run;
quit;
run;
3
Unclear to me what you're asking. You want to change the background of the cell that says cases to black on gainboro?Joe
Disease is an across variable, and there are three levels. I want to change the background of the header cell that says "Cases," but only for the middle level of disease. The other two cells that say "Cases" should keep the style(header)= attributecolleen mclaughlin

3 Answers

1
votes

You can use formats to do something close to what you're asking, but I'm not sure it's possible to do what you're asking - maybe Cynthia Zender on communities.sas.com might?

data test;
length name $ 10 disease $ 10.;
infile datalines dsd;
input name $ disease cases rate;
datalines;
State,Fever,4847,25.16
State,Cold,25632,131.5
State,Flu,103825,535.82
Lincoln,Fever,3920,44.17
Lincoln,Cold,16913,190.18
Lincoln,Flu,62965,735.39
Washington,Fever,827,56.56
Washington,Cold,3609,234.26
Washington,Flu,16610,1078.8
Kings,Fever,1026,37.45
Kings,Cold,4984,181.85
Kings,Flu,18388,694.33
Sussex,Fever,1411,78.38
Sussex,Cold,5515,300.46
Sussex,Flu,13881,813.11
Queens,Fever,616,26.03
Queens,Cold,2496,107.75
Queens,Flu,12518,558.09
;
run;


proc format;
  value $headerbackf
  'Cold' = 'gainsboro'
  other  = 'charcoal';
  value $headerforef
  'Cold' = 'black'
  other  = 'white'
  ;
quit;

proc report data=test nowd headline headskip
STYLE(Header)={background=charcoal foreground=white }
style(column)={background=gray foreground=black}
style(report)=[rules=rows bordercolor=white];
columns (name disease,(cases rate));
define name/group order=data 'County' style(column)={background=lightgray} style(header)=[bordertopcolor=gainsboro background=gainsboro foreground=black];
define disease/across '' order=data style(header)={background=$HEADERBACKF. foreground=$HEADERFOREF.};
define cases/'Cases' format=comma9. style(header)=inherit;
define rate/'Rate' format=comma12.1 ;
compute cases;
    call define('_c4_','style','style={background=lighttgray}');
    call define('_c5_','style','style={background=lighttgray}');
endcomp;
run;

That gets that top row formatted, but, doesn't actually get the row you're asking for. I'm not sure it's possible to.

1
votes

It's possible as @ChrisJ noted that you might be able to do this with CSS styles and nth child selection. It's also possible you can't, unfortunately, due to how SAS does things with PROC REPORT - in particular, in PROC REPORT everything gets shoved inside <tr>s including the header rows, so nth-child and sibling selectors are impossible due to the headers not being children or siblings of each other.

Here's an example of a kludgey version of this, using sashelp.cars as an example.

CSS: (save in a .css file on your drive somewhere, say "c:\temp\test.css"):

@import 'base.css';

/* Red the second (really third) column header value */
.table thead tr:nth-child(2) th:nth-child(3)  {
    color:red
}

/* Yellow background for the mpg headers under Europe */
.table thead tr:nth-child(3) th:nth-child(4),
.table thead tr:nth-child(3) th:nth-child(5)
{
    background-color:yellow
}

/* Green the mpg-city values */
.table thead tr:nth-child(3) th:nth-child(even) {
    color:green
}

SAS program: (assumes the above-saved CSS file)

ods html file='example.html' cssstyle='c:\temp\test.css'(html);
ods pdf file='example.pdf' cssstyle='c:\temp\test.css'(print);
proc sort data=sashelp.cars out=cars; by origin;
run;
proc report data=cars nowd;
  columns type origin,(mpg_city mpg_highway);
  define origin/across;
  define type/group;
  define mpg_City / analysis mean;
  define mpg_highway / analysis mean;    
run;

ods _all_ close;

This is partially based on Kevin Smith's Unveiling the power of Cascading Style Sheets (CSS) in ODS.

Unfortunately, we can't in any way identify a cell that has "MPG(City)" in it except by knowing they'll be even column numbers. We similarly can't identify a cell under a "Europe" except by knowing what cells those will be.

0
votes

Try adding a dummy column _c to the end of your columns statement, and add a define & compute to go with it.

Also, ensure your colour names are actually valid, e.g. lighttgray is invalid and will not work.

columns ... _c ;
define _c / computed noprint ;
compute _c ;
  call define('_c4_','style','style={background=lightgray}');
  call define('_c5_','style','style={background=lightgray}');
endcomp ;