Good morning,
I am trying to create the report and higlight one field based on another field meaning. It is similar to what I found in SAS communiy:
proc report nowd data=sashelp.class;
col name sex age weight height;
compute height;
if sex='F' then do;
call define(_col_,"style","style={background = pink}");
end;
endcomp;
run;
But the thing is if you use, let's say, Weight instead of Sex field in a compute block - it doesn't work!
proc report nowd data=sashelp.class;;
col name sex age weight height;
compute height;
if weight>100 then do;
call define(_col_,"style","style={background = pink}");
end;
endcomp;
run;
In my data, I am creating an additional field in dataset and after that I am trying to use it as an indicator to highlight the other filed. Like,
data test;
set sashelp.class;
letter = substr(name,1,1);
run;
proc report nowd data=test;
col name sex age weight height letter;
compute height;
if letter="A" then do;
call define(_col_,"style","style={background = pink}");
end;
endcomp;
run;
This code again doesn't give me desired result. Can somebody give me a hint WHY?
You're welcome.
The second example works if you show Height and Letter in the col definition
proc report nowd data=test;
col name sex age weight letter height ;
compute height;
if letter="A" then do;
call define(_col_,"style","style={background = pink}");
end;
endcomp;
run;
When you refer to a numeric field, you have to add ".sum" to make it work. For example:
proc report nowd data=sashelp.class;;
col name sex age weight height;
compute height;
if weight.sum>100 then do;
call define(_col_,"style","style={background = pink}");
end;
endcomp;
run;
Thank you very much, Thierry- with numeric value it now works! Do you have any solution for the second example, when I added field to the sas dataset and after that trying to use it in compute block? It is string format (letter). What can I do here in order to make it work?
You're welcome.
The second example works if you show Height and Letter in the col definition
proc report nowd data=test;
col name sex age weight letter height ;
compute height;
if letter="A" then do;
call define(_col_,"style","style={background = pink}");
end;
endcomp;
run;
I was wondering, why my example didn't work, because I showed both fields in col definition. But the trick is to show created field (in my case it is Letter) before Hight. And it works now! Great! Thank you so much!!!!
@OLevin wrote:
I was wondering, why my example didn't work, because I showed both fields in col definition. But the trick is to show created field (in my case it is Letter) before Hight. And it works now!
Great! Thank you so much!!!!
A general rule with proc report is that the result table is built from left to right. So a calculated field can't reference the value of a column further to the right to set current cell properties or values because that cell has not been built yet.
So in:
proc report nowd data=test; col name sex age weight height letter; compute height; if letter="A" then do; call define(_col_,"style","style={background = pink}"); end; endcomp; run;
since the column order has LETTER to the right of HEIGHT the value of letter is considered undefined for setting column height properties.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.