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

I output my sas data to ods Excel format, I have a requirement that if the variable contains a certain string, I need to do some kind of color coding ( background color, or change the color of the font) to display in excel file. I do have the idea of doing it in compute block for numeric variable > or < conditions. Never done strings, please suggest some ideas, Example coding in the compute block of proc report or directing to sources also greatly appreciated. Thanks. I am attaching the sample of how I want Variable 1, how it is there in my data. variable2 is how I want to show in excel output, If the string contains "continuous" I need to do background color yellow and/or font in red.

SASuserlot_0-1602116831321.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

Hi:
You are using the COMPUTE BEFORE and the CALL DEFINE on the wrong variables. There are no variables on the break line written with the LINE statement. Did you review my paper on break processing that I posted: https://support.sas.com/resources/papers/proceedings17/SAS0431-2017.pdf -- the LINE statement writes EXTRA break lines, but the row from the LINE statement spans the entire table. There are NOT any data cells when you use a LINE statement.
So here are my numbered comments that correspond to the output below -- Your original code produced this output with all the color coding attempts removed. I made flag visible so you could see that it did not exist on the row written by the LINE statement:

Cynthia_sas_0-1602379233082.png


1) in your CALL DEFINE, you are changing the color for the PRODUCT cell. Your LINE statement is writing out REGION, but you're changing the color of the PRODUCT cell. The LINE statement is writing 1 report row -- PRODUCT is NOT a column on this report row. FLAG is not a column on this report row. You can't color code PRODUCT on this row and you can't test FLAG on this row. At the point in time where the COMPUTE block before region is EXECUTING, the FLAG variable is not visible on the report row.
2) FLAG appears on every report row for data so you could have a COMPUTE block for FLAG that changed the background color of PRODUCT on each data row..
3) you spelled yellow incorrectly as yelllow, so the color will not be recognized when it does work.

Here's a reworked version of your program showing color coding that works on every data row and also shows how to color the LINE statement rows.

Cynthia_sas_1-1602379292203.png

Notice that I have a separate COMPUTE block for FLAG and I test that value on every row where FLAG appears. Then I use the value of flag to determine the background color of the PRODUCT cell on that same row.

 

Just because EAST and WEST are underneath the PRODUCT column header, does not mean that you can use the CALL DEFINE in the COMPUTE BEFORE REGION to change the color of EAST or WEST. Notice that in the COMPUTE BEFORE REGION statement, I have added background= to the style override on the COMPUTE statement. The ONLY thing that touches the LINE output row is the style override that you have on the COMPUTE block (but there are NOT any data cells on that row, there's only what you write with the LINE statement).

 

Cynthia

View solution in original post

31 REPLIES 31
jimbarbour
Meteorite | Level 14

In a Proc Report, try the following COMPUTE block:

COMPUTE	Variable2;
	IF	INDEX(UPCASE(Variable2), 'CONTINUOUS')	THEN
		CALL DEFINE(_COL_, "style", "STYLE=[BACKGROUD=YELLOW]");
ENDCOMP; 

Jim

SASuserlot
Barite | Level 11

tried no luck, but its not throwing error either. attaching code, second pic is the how variable look in dataset, third excel output

 PROC REPORT data=sdsp5 nowindows
style(report)=[font=("Times New Roman", 11pt) protectspecialchars=on]
style(header)=[font=("Times New Roman", 11pt, bold) just=center protectspecialchars=on]
style(column)=[font=("Times New Roman", 11pt) protectspecialchars=on];
.............
..............
...............
DEFINE Study_Design/ "" display style=[just=c vjust=t cellwidth=8.882cm tagattr='type:String format:Text']; DEFINE Study_status /" " display style=[just=c vjust=t cellwidth=2.718cm tagattr='type:String format:Text']; DEFINE Study_start_date /"" display style=[just=c vjust=t cellwidth=2.818cm tagattr='type:String format:Text']; DEFINE Exchange_standards /"" display style=[just=c vjust=t cellwidth=3.212cm tagattr='type:String format:Text']; DEFINE terminology_standards /"" display style=[just=c vjust=t cellwidth=4.643cm tagattr='type:String format:Text']; %end; compute before hline/ style=[font=("Times New Roman", 11pt, bold) ]; line @140 hline $100.; if index((Study_design),"[Missing]") then call define(_col_, "style","style=[Background=Yellow]"); endcomp; RUN;

SASuserlot_0-1602123760193.png

SASuserlot_1-1602123795089.png

 

 

jimbarbour
Meteorite | Level 14

Is that your entire Proc Report definition?  I am used to seeing COLUMN definitions.

 

And I'm used to seeing COMPUTE Study_Design if I'm doing a call define for the column Study_Design.

 

Jim

SASuserlot
Barite | Level 11
 PROC REPORT data=sdsp5 nowindows
       style(report)=[font=("Times New Roman", 11pt) protectspecialchars=on]
      style(header)=[font=("Times New Roman", 11pt, bold) just=center protectspecialchars=on]
      style(column)=[font=("Times New Roman", 11pt) protectspecialchars=on];
/*	  by ph ;*/
	  where &ph;
      COLUMN ph Hline Study_Identifier brief_title Study_Design Study_status Study_start_date
	   		 Exchange_standards terminology_standards;
      DEFINE ph/ " " group noprint style=[just=c vjust=t cellwidth=2.745cm tagattr='type:String format:Text'];
      DEFINE Hline/ " " group noprint style=[font=("Times New Roman",11pt)just=c vjust=t vjust=t cellwidth=2.745cm tagattr='type:String format:Text'];
 %if &phx=1 %then %do;
      DEFINE Study_Identifier/"Study Identifier" display style=[just=l vjust=t cellwidth=2cm tagattr='type:String format:Text'];
      DEFINE brief_title/ "Brief Title" display style=[just=c vjust=t cellwidth=3.882cm tagattr='type:String format:Text'];
      DEFINE Study_Design/ "Study Design" display style=[just=c vjust=t cellwidth=5.882cm tagattr='type:String format:Text'];
      DEFINE Study_status /" Study Status" display style=[just=c vjust=t cellwidth=2.718cm tagattr='type:String format:Text'];
      DEFINE Study_start_date /"Study Start Date" display style=[just=c vjust=t cellwidth=2.818cm tagattr='type:String format:Text'];
      DEFINE Exchange_standards /"Exchange standards" display style=[just=c vjust=t cellwidth=3.212cm tagattr='type:String format:Text'];
      DEFINE terminology_standards /"Terminology Standards" display style=[just=c vjust=t cellwidth=4.643cm tagattr='type:String format:Text'];
  %end;

%else %do;
      DEFINE Hline/ " " group noprint style=[font=("Times New Roman",11pt)just=c vjust=t cellwidth=2.745cm tagattr='type:String format:Text'];
      DEFINE Study_Identifier/"" display style=[just=l vjust=t cellwidth=2cm tagattr='type:String format:Text'];
      DEFINE brief_title/ "" display style=[just=c vjust=t cellwidth=3.882cm tagattr='type:String format:Text'];
      DEFINE Study_Design/ "" display style=[just=c vjust=t cellwidth=8.882cm tagattr='type:String format:Text'];
      DEFINE Study_status /" " display style=[just=c vjust=t cellwidth=2.718cm tagattr='type:String format:Text'];
      DEFINE Study_start_date /"" display style=[just=c vjust=t cellwidth=2.818cm tagattr='type:String format:Text'];
      DEFINE Exchange_standards /"" display style=[just=c vjust=t cellwidth=3.212cm tagattr='type:String format:Text'];
      DEFINE terminology_standards /"" display style=[just=c vjust=t cellwidth=4.643cm tagattr='type:String format:Text'];
%end;
	compute before hline/ style=[font=("Times New Roman", 11pt, bold) ];
	 line @140 hline $100.;
	IF	INDEX(UPCASE(Study_design), "[MISSING]")	THEN
		CALL DEFINE(_COL_, "style", "STYLE=[BACKGROUD=YELLOW]");
	endcomp;
   RUN;
SASuserlot
Barite | Level 11

I created as macro  first part  defines displays the headings and variables of one group later part only display the variables of different groups.

jimbarbour
Meteorite | Level 14

Background appears to be misspelled.  We should correct that first.

	compute before hline/ style=[font=("Times New Roman", 11pt, bold) ];
	 line @140 hline $100.;
	IF	INDEX(UPCASE(Study_design), "[MISSING]")	THEN
		CALL DEFINE(_COL_, "style", "STYLE=[BACKGROUD=YELLOW]");
	endcomp;

And what is this code in the COMPUTE block doing?

	 line @140 hline $100.;

Jim

SASuserlot
Barite | Level 11

line is creating the header as hline variable in the excel sheet at row 4 for particular group and other places and and its alignment and row 3 is the overall heading for all studies

SASuserlot_0-1602125997627.png

 

SASuserlot
Barite | Level 11

tried with correct spelling , no change.

ballardw
Super User

For testing remove any macro elements and use a reduced data set, or just plain new data with just one or two variables.

get the color coding to work. Then build back to the bigger picture.

If the question involves specific VALUES of a variable one way is to consider a format.

 

Proc format;
value $mytextcolor
"Home" = 'pink'
"Street" = 'yellow'
;

data example;
   input row word $;
datalines;
1  Sometext
2  Home
3  .
4  Street
;

proc report data=example;
   columns row word;
   define word/display style={background=$mytextcolor.};
run;

You could use any of the SAS color naming conventions in the format.

And if your values are actually missing, instead placing a text value placed in them you could use two formats.

Proc format;
value $mytextcolor
"Home" = 'pink'
"Street" = 'yellow'
"Missing"     = 'red'
;
value $mytextvalue
" " = "Missing"
;
run;
data example;
   input row word $;
datalines;
1  Sometext
2  Home
3  .
4  Street
;

proc report data=example;
   columns row word;
   define word/display style={background=$mytextcolor.}
               format=$mytextvalue.
   ;
   
run;

The background color format uses the formatted value of the variable to determine the color.

 

You may have to share the ODS EXCEL destination code at some point.

SASuserlot
Barite | Level 11

your suggestion helpful but I still want to display the "Missing" my shells and color code when the shell work containing  this word.

Cynthia_sas
SAS Super FREQ

Hi:

  The point is that if the sample program works for you to produce color coding as per the program, then you should be able to make color coding work with your data. But it is your data that I have a question about:

Cynthia_sas_0-1602163328084.png

Is this really your data? It looks like your data has ODS ESCAPECHAR control strings (like ^n or line feed) inside the data value. In your screen shot are you showing 1 column's value or is Missing in one column and ^nControl in another column? What about the second line? Is that one column or 3 or 4 columns? Your data does not make sense. However IF your text value is one column's value and IF you only want the word MISSING to be highlighted then you need to work with inline formatting.

  But without some real sample data and a program that is not already macro-ized, you are only providing part of the picture. We'd also need your ODS EXCEL statements and to see ALL of the ODS EXCEL sub-options you're using. Your original post showed one value highlighted and in your data, I do not see the value you want highlighted.

  Based on your very first posting, I was able to generate this:

Cynthia_sas_0-1602164136702.png

 

using this code:


data fakedata;
  length id $2 variable1 $20 variable2 $20;
  infile datalines dlm=',' dsd;
  input id $ variable1 $ variable2 $;
datalines;
1a,continuous, continuous
2b,constant, continuous
3c,continuous, continuous
4d,viscous, viscous
5e,not now, not now
6f,constant, constant
7g,but not true, but not true
;
run;

proc format;
  value $bkfmt 'continuous' = 'yellow'
              other = 'white';
  value $forefmt 'continuous' = 'red'
                 other = 'black';
run;

ods excel file='c:\temp\usefmt_color.xlsx';
proc report data=fakedata;
  column id variable1 variable2;
  define id / order;
  define variable1 / display;
  define variable2 / display 
         style(column)={background=$bkfmt. color=$forefmt.};
run;
ods excel close;

  You now have 2 programs that work to produce color coding. If your program is not working as you expect, then either post some simplified code and some real data to the forums so others can understand the exact issue and then please explain what you want.

 

  Thanks for clarifying and providing more context and your exact data.

Cynthia

SASuserlot
Barite | Level 11

First I want to say thank you for looking into my post. I am fan of your ans and post. I still follow your post.

1. I provided my  complete proc report section, I providing the excel options used.

2.ods ESCAPECHAR = '^';
ods excel file='C:\Users\xxxu\Desktop\New folder\Guy\exa.xlsx' options(sheet_name='leveling'
 sheet_interval='none')

 

2  The screen shot is the data,   I used the^n so that every word after ^n display in next line in excel output

3. The string you seen in screen shot , is one row string, may be its came in the picture like that because I did not widen the width. they string is just made by concatenating the words, separated by "comma"

4. I am  not looking for the word to highlight. I am looking if any cell in excel or dataset contains the "missing" , I want to color that. its just to alert me in excel sheet some thing missing in that particular cell. I hope I given information you looking for. Thanks again following the post.

SASuserlot
Barite | Level 11

I really appreciate you taking take time and created a data set and show me the example. However  If you see  you fakedata set where you created dataset from excel, the second row is a string that contains the word "continuous constant" so it has to be highlighted or alert with some color. Where as in your dataset you created second row also continuous. so my question is how  create the format if you looking for a word in a string. 

ballardw
Super User

@SASuserlot wrote:

your suggestion helpful but I still want to display the "Missing" my shells and color code when the shell work containing  this word.


So which part does my example not cover???

 

You have not provided useable  data. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the </> icon or attached as text to show exactly what you have and that we can test code against.

 

And there is no way we can code against a picture that does not contain the actual values of variables in a data set.

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!

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
  • 31 replies
  • 2805 views
  • 9 likes
  • 4 in conversation