BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Relax
Fluorite | Level 6

Hello All,

I have a small sample dataset, which has three variables, ID, Grade and Result.  I want to report this dataset using PROC Report and assign the different colors to Grade based on the value of variable Result.  Let's say, if Result is Pass then the background color of Grade is green; otherwise, it would be in red.  At the same time, I only want to see ID and Grade in the output, so I use NOPRINT for Result.  Here is my code:

DATA WORK.Test;
INFILE DATALINES;
INPUT ID Grade Result $;
DATALINES;
1 90 Pass
2 65 Pass
3 57 Fail
;
RUN;

PROC REPORT DATA=WORK.Test
STYLE(REPORT) = {OUTPUTWIDTH=60%}
STYLE(HEADER) = [JUST = CENTER FONT_FACE = calibri FONT_SIZE = 3 FOREGROUND = Black BACKGROUND = Orange PROTECTSPECIALCHARS=OFF];
COLUMN ID Grade Result;
DEFINE ID / DISPLAY STYLE(COLUMN)=[JUST=l FONT_FACE = calibri FONT_SIZE = 3];
DEFINE Grade / DISPLAY STYLE(COLUMN)=[JUST=l FONT_FACE = calibri FONT_SIZE = 3];
DEFINE Result / NOPRINT STYLE(COLUMN)=[JUST=l FONT_FACE = calibri FONT_SIZE = 3];
COMPUTE Grade;
IF Result = 'Pass' THEN CALL DEFINE("_COL_", "STYLE", "STYLE={BACKGROUND=CX00FF00}");
IF Result = 'Fail' THEN CALL DEFINE("_COL_", "STYLE", "STYLE={BACKGROUND=CXFF0000}");
ENDCOMP;
QUIT;

The output I want is

Relax_0-1615833805805.png

Could someone kindly help me figure out how to do it?

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Try this:

PROC REPORT DATA=WORK.Test
STYLE(REPORT) = {OUTPUTWIDTH=60%}
STYLE(HEADER) = [JUST = CENTER FONT_FACE = calibri FONT_SIZE = 3 FOREGROUND = Black BACKGROUND = Orange PROTECTSPECIALCHARS=OFF];
COLUMN ID Result Grade ;
DEFINE ID / DISPLAY STYLE(COLUMN)=[JUST=l FONT_FACE = calibri FONT_SIZE = 3];
DEFINE Grade / DISPLAY STYLE(COLUMN)=[JUST=l FONT_FACE = calibri FONT_SIZE = 3];
DEFINE Result / NOPRINT STYLE(COLUMN)=[JUST=l FONT_FACE = calibri FONT_SIZE = 3];
COMPUTE Grade;
IF Result = 'Pass' THEN CALL DEFINE(_COL_, "STYLE", "STYLE={BACKGROUND=CX00FF00}");
IF Result = 'Fail' THEN CALL DEFINE(_COL_, "STYLE", "STYLE={BACKGROUND=CXFF0000}");
ENDCOMP;
QUIT;

Two things to change:

Column order. Proc Report builds report items from left to right. So the column Grade can't see Result if the Result column is to the right on the Columns statement.

Second, _col_ is a key word. When you place it in quotes then it becomes a value, which doesn't match the requirement for call define statements.

View solution in original post

6 REPLIES 6
ballardw
Super User

Try this:

PROC REPORT DATA=WORK.Test
STYLE(REPORT) = {OUTPUTWIDTH=60%}
STYLE(HEADER) = [JUST = CENTER FONT_FACE = calibri FONT_SIZE = 3 FOREGROUND = Black BACKGROUND = Orange PROTECTSPECIALCHARS=OFF];
COLUMN ID Result Grade ;
DEFINE ID / DISPLAY STYLE(COLUMN)=[JUST=l FONT_FACE = calibri FONT_SIZE = 3];
DEFINE Grade / DISPLAY STYLE(COLUMN)=[JUST=l FONT_FACE = calibri FONT_SIZE = 3];
DEFINE Result / NOPRINT STYLE(COLUMN)=[JUST=l FONT_FACE = calibri FONT_SIZE = 3];
COMPUTE Grade;
IF Result = 'Pass' THEN CALL DEFINE(_COL_, "STYLE", "STYLE={BACKGROUND=CX00FF00}");
IF Result = 'Fail' THEN CALL DEFINE(_COL_, "STYLE", "STYLE={BACKGROUND=CXFF0000}");
ENDCOMP;
QUIT;

Two things to change:

Column order. Proc Report builds report items from left to right. So the column Grade can't see Result if the Result column is to the right on the Columns statement.

Second, _col_ is a key word. When you place it in quotes then it becomes a value, which doesn't match the requirement for call define statements.

Reeza
Super User

This seems to work as well, though @ballardw  has identified the root of the issue - that one often catches me as well with CALL DEFINE.

This uses the option to explicitly name the column being shaded rather than it being implicit, which I personally prefer as it makes the code slightly more legible IMO.

 

PROC REPORT DATA=WORK.Test
STYLE(REPORT) = {OUTPUTWIDTH=60%}
STYLE(HEADER) = [JUST = CENTER FONT_FACE = calibri FONT_SIZE = 3 FOREGROUND = Black BACKGROUND = Orange PROTECTSPECIALCHARS=OFF];
COLUMN ID Grade Result;
DEFINE ID / DISPLAY STYLE(COLUMN)=[JUST=l FONT_FACE = calibri FONT_SIZE = 3];
DEFINE Grade / DISPLAY STYLE(COLUMN)=[JUST=l FONT_FACE = calibri FONT_SIZE = 3];
DEFINE Result / NOPRINT STYLE(COLUMN)=[JUST=l FONT_FACE = calibri FONT_SIZE = 3];
COMPUTE Result;
IF Result ="Pass" THEN  CALL DEFINE("grade", "style", "style=[background=CX00FF00]");
else IF Result="Fail" THEN  CALL DEFINE("grade", "style", "style=[background=CXFF0000]"); 
ENDCOMP;
QUIT;

@Relax wrote:

Hello All,

I have a small sample dataset, which has three variables, ID, Grade and Result.  I want to report this dataset using PROC Report and assign the different colors to Grade based on the value of variable Result.  Let's say, if Result is Pass then the background color of Grade is green; otherwise, it would be in red.  At the same time, I only want to see ID and Grade in the output, so I use NOPRINT for Result.  Here is my code:

DATA WORK.Test;
INFILE DATALINES;
INPUT ID Grade Result $;
DATALINES;
1 90 Pass
2 65 Pass
3 57 Fail
;
RUN;

PROC REPORT DATA=WORK.Test
STYLE(REPORT) = {OUTPUTWIDTH=60%}
STYLE(HEADER) = [JUST = CENTER FONT_FACE = calibri FONT_SIZE = 3 FOREGROUND = Black BACKGROUND = Orange PROTECTSPECIALCHARS=OFF];
COLUMN ID Grade Result;
DEFINE ID / DISPLAY STYLE(COLUMN)=[JUST=l FONT_FACE = calibri FONT_SIZE = 3];
DEFINE Grade / DISPLAY STYLE(COLUMN)=[JUST=l FONT_FACE = calibri FONT_SIZE = 3];
DEFINE Result / NOPRINT STYLE(COLUMN)=[JUST=l FONT_FACE = calibri FONT_SIZE = 3];
COMPUTE Grade;
IF Result = 'Pass' THEN CALL DEFINE("_COL_", "STYLE", "STYLE={BACKGROUND=CX00FF00}");
IF Result = 'Fail' THEN CALL DEFINE("_COL_", "STYLE", "STYLE={BACKGROUND=CXFF0000}");
ENDCOMP;
QUIT;

The output I want is

Relax_0-1615833805805.png

Could someone kindly help me figure out how to do it?

Thanks in advance!


 

Relax
Fluorite | Level 6

Dig it.  Thanks for showing me one more option!

Reeza
Super User
Note that in this case, the order doesn't matter as well 😉
Relax
Fluorite | Level 6

Yes, I noticed that.  So it may be easier for me to memorize and practice it the next time when I use PROC Report😀 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

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
  • 6 replies
  • 1560 views
  • 5 likes
  • 3 in conversation