BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Daily1
Quartz | Level 8

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

Daily1_0-1718000621968.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

Made some changes to the Proc REPORT code, see below. Here are the details for the changes:

  • Added a _dummy column as the last column. In a compute block for a given column you can only access the values of the columns to the left of the current column. So by adding the _dummy column, I can access all the column values to the left in the _dummy compute block. The value of the _dummy column is not printed (NOPRINT)
  • added ORDER to the ID variable to display each value
  • Since value1 and value2 are analysis variables you need to access the value using columnName.statistic in our case value1.sum or value2.sum
  • This name can also be used in the call define to reference the proper column
  • change foreground to background in the call define
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;

 

 

View solution in original post

3 REPLIES 3
BrunoMueller
SAS Super FREQ

Made some changes to the Proc REPORT code, see below. Here are the details for the changes:

  • Added a _dummy column as the last column. In a compute block for a given column you can only access the values of the columns to the left of the current column. So by adding the _dummy column, I can access all the column values to the left in the _dummy compute block. The value of the _dummy column is not printed (NOPRINT)
  • added ORDER to the ID variable to display each value
  • Since value1 and value2 are analysis variables you need to access the value using columnName.statistic in our case value1.sum or value2.sum
  • This name can also be used in the call define to reference the proper column
  • change foreground to background in the call define
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;

 

 

Daily1
Quartz | Level 8

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;
BrunoMueller
SAS Super FREQ

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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

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
  • 3 replies
  • 471 views
  • 3 likes
  • 2 in conversation