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

Hello,

 

I need assistance on how to color code specific cells using PROC REPORT. I'm trying to resolve my code below as the logic is not reading a certain text variable NAME IN (‘Ann’,’Ian’). Please assist.

 

 

proc report data=TEST nowd;
TITLE1 j=left "Sample Report
		column ID  ICT_DT  POST_RISK_LEVEL;
		define INIT_DATE/display;
		define NAME/display;
		
compute ICT_DT; 
            if INIT_DATE=. then call define(_col_,"style","style={background=RED}");
			else
			if INIT_DATE =. AND NAME IN (‘Ann’,’Ian’) then call define(_col_,"style","style={background=WHITE}"); /*NOT WORKING*/
			endcomp;
run;

 

 

image.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

Hi:

  Again, you cannot test for NAME in the COMPUTE block for INIT_DATE because of the PROC REPORT "left-to-right" rule. As shown in this output:

Cynthia_sas_0-1650393951035.png

My report #1 was generated with some fake data and a version of your code with NAME appearing AFTER INIT_DATE in the COLUMN statement. This will not work. However, in my report #2, I used the _DUMMY helper variable technique that was suggested. This is a timing issue based on the left-to-right rule. In your code at the point in time when INIT_DATE is being placed on the report row, the COMPUTE block doesn't know what the value for NAME is because NAME is not yet visible on the report row. PROC REPORT handles each COMPUTE block as the variable is placed on the report row. So when INIT_DATE is being placed and the COMPUTE block is being executed, PROC REPORT does not have any value for NAME yet. Remember the COMPUTE block is building the report row, line by line, column by column.

  In my #2 report, I did some additional highlighting with different colors so you could see the way that each variable on the report row can be touched in a different COMPUTE block.

  Here's my code:

Cynthia_sas_1-1650394182751.png

There have been many previous forum postings about PROC REPORT and the left-to-right rule. I'd recommend looking at some of those previous postings.

Cynthia

 

 

 

 

View solution in original post

10 REPLIES 10
PaigeMiller
Diamond | Level 26

Two possible causes:

 

NAME IN (‘Ann’,’Ian’) 

get rid of the curly quotes, make sure you are using straight quotes

 

Also, your logic is flawed, the code 

 

else if INIT_DATE =. AND NAME IN (‘Ann’,’Ian’)

never executes. Why? Because first you test to see if INIT_DATE=. and the logic never gets to the else if part of the code for the three rows shown in your desired output.

--
Paige Miller
Cynthia_sas
SAS Super FREQ
Hi:
In addition to the above suggestions, I am not sure that your code produces the results you show. You do NOT have NAME in the COLUMN statement. Typically, PROC REPORT works from Left to right in setting the style. You show 4 variables in your current COLUMN statement, but you only show 3 columns in your screen shot of the output. We need to see ALL of your code, not just parts of the code. ALL the DEFINE statements, the entire COLUMN statement and ALL of the compute blocks.
Cynthia
annaleticia
Fluorite | Level 6
Hello,

Here's the updated code --- the syntax still not reading the NAME column.

proc report data=TEST nowd;
TITLE1 j=left "Sample Report
column ID INIT_DATE NAME ;
define INIT_DATE/display;
define NAME/display;

compute INIT_DATE;
if INIT_DATE =. AND NAME IN ("Ann","Ian") then call define(_col_,"style","style={background=WHITE}"); /*NOT WORKING*/
else if INIT_DATE=. AND NAME IN ("Tom") then call define(_col_,"style","style={background=RED}"); /*NOT WORKING*/
endcomp;
run;
PaigeMiller
Diamond | Level 26

Please paste your code into the code window from now on. The code window appears when you click on the "little running man" icon.

 

You would help yourself, and help us help you, by properly indenting your code as well, as I show below.

 

Give this a try:

 

proc report data=test nowd;
    title1 j=left "Sample Report";
    column id init_date name _dummy;
    define init_date/display;
    define name/display;
    define _dummy/noprint;

    compute _dummy;
        if init_date =. and name in ("Ann","Ian") then call define('init_date',"style","style={background=WHITE}"); 
        else if init_date=. and name in ("Tom") then call define('init_date',"style","style={background=RED}"); 
    endcompute;
run;

 

Thanks to @BrunoMueller for the idea to use _DUMMY to help with coloring columns (see: https://communities.sas.com/t5/SAS-Programming/Alternate-column-coloring-using-proc-report/m-p/79063...)

 

 

--
Paige Miller
annaleticia
Fluorite | Level 6
Hello,
For some reason, the program is still not reading the color coding for the name variable.
*name in ("Ann","Ian")
PaigeMiller
Diamond | Level 26

Are there errors in the log? If so, show us the log.

 

If not, show us the code you used.

--
Paige Miller
PaigeMiller
Diamond | Level 26

Also, you should use

 

name in ("Anna","Ian")

instead of 

 

name in ("Ann","Ian")

 

Pay attention to details like that.

--
Paige Miller
annaleticia
Fluorite | Level 6
Hello, Here's the updated code --- the syntax still not reading the NAME column. proc report data=TEST nowd; TITLE1 j=left "Sample Report column ID INIT_DATE NAME ; define INIT_DATE/display; define NAME/display; compute INIT_DATE; if INIT_DATE =. AND NAME IN ("Ann","Ian") then call define(_col_,"style","style={background=WHITE}"); /*NOT WORKING*/ else if INIT_DATE=. AND NAME IN ("Tom") then call define(_col_,"style","style={background=RED}"); /*NOT WORKING*/ endcomp; run;
Cynthia_sas
SAS Super FREQ

Hi:

  Again, you cannot test for NAME in the COMPUTE block for INIT_DATE because of the PROC REPORT "left-to-right" rule. As shown in this output:

Cynthia_sas_0-1650393951035.png

My report #1 was generated with some fake data and a version of your code with NAME appearing AFTER INIT_DATE in the COLUMN statement. This will not work. However, in my report #2, I used the _DUMMY helper variable technique that was suggested. This is a timing issue based on the left-to-right rule. In your code at the point in time when INIT_DATE is being placed on the report row, the COMPUTE block doesn't know what the value for NAME is because NAME is not yet visible on the report row. PROC REPORT handles each COMPUTE block as the variable is placed on the report row. So when INIT_DATE is being placed and the COMPUTE block is being executed, PROC REPORT does not have any value for NAME yet. Remember the COMPUTE block is building the report row, line by line, column by column.

  In my #2 report, I did some additional highlighting with different colors so you could see the way that each variable on the report row can be touched in a different COMPUTE block.

  Here's my code:

Cynthia_sas_1-1650394182751.png

There have been many previous forum postings about PROC REPORT and the left-to-right rule. I'd recommend looking at some of those previous postings.

Cynthia

 

 

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 10 replies
  • 2183 views
  • 0 likes
  • 3 in conversation