Hi everyone,
I'm working on a PROC REPORT in SAS and need some help with conditional formatting. Specifically, I want to compare two columns, value1
and value2
, and highlight the greater value in red. Below is a simplified version of my dataset and the PROC REPORT code I've tried:
data sample;
input id value1 value2;
datalines;
1 10 15
2 20 5
3 25 30
4 40 35
5 50 50
;
run;
proc report data=sample nowd;
column id value1 value2;
define id / 'ID' order=data;
define value1 / sum 'Value 1' display;
define value2 / sum 'Value 2' display;
compute value1;
if value1 > value2 then do;
call define(_col_, "style", "style={foreground=Red}");
end;
endcomp;
run;
I want Output
Made some changes to the Proc REPORT code, see below. Here are the details for the changes:
proc report data=sample nowd;
column id value1 value2 _dummy;
define id / 'ID' order order=data;
define value1 / sum 'Value 1' ;
define value2 / sum 'Value 2' ;
define _dummy / computed noprint;
compute _dummy;
if value1.sum > value2.sum then do;
call define("value1.sum", "style", "style={background=Red}");
end;
endcomp;
run;
Made some changes to the Proc REPORT code, see below. Here are the details for the changes:
proc report data=sample nowd;
column id value1 value2 _dummy;
define id / 'ID' order order=data;
define value1 / sum 'Value 1' ;
define value2 / sum 'Value 2' ;
define _dummy / computed noprint;
compute _dummy;
if value1.sum > value2.sum then do;
call define("value1.sum", "style", "style={background=Red}");
end;
endcomp;
run;
Thank you for your help. I have some addition query .
This code highlights the greater value in red, but only when the ID equals 1. Is it not working properly?
Revised Data
data sample;
input id value1 value2;
datalines;
1 10 15
1 20 5
3 25 30
1 40 35
5 50 50
1 10 15
1 20 5
3 25 30
1 40 35
5 50 50
1 10 15
1 20 5
3 40 30
1 40 35
5 55 50
;
run;
proc report data=sample nowd;
column id value1 value2 _dummy;
define id / 'ID' order order=data;
define value1 / sum 'Value 1';
define value2 / sum 'Value 2';
define _dummy / computed noprint;
compute _dummy;
if id = 1 and value1.sum > value2.sum then do;
call define("value1.sum", "style", "style={background=Red}");
end;
endcomp;
run;
You might have noticed, that the id value is only in the first row (when a new id value starts), after that it is not visiable and therefor the id variable is empty. You need to put the value of the first row for each id value aside and use this in the compute block. To put the value aside, use a COMPUTE BEFORE columnName compute block. See the example below. I use the _dummy variable to display the contents of the idkeep variable.
proc report data=sample nowd;
column id value1 value2 _dummy;
define id / 'ID' display order order=data;
define value1 / sum 'Value 1';
define value2 / sum 'Value 2';
define _dummy / computed;
compute before id;
idkeep = id;
endcomp;
compute _dummy;
_dummy = idkeep;
if idkeep = 1 and value1.sum > value2.sum then do;
call define("value1.sum", "style", "style={background=Red}");
end;
endcomp;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.