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

Hello SAS Friends, 

Using either SAS ver 9.4 or SAS Studio....

Is it possible to add highlights and font colors to a few key words within a text string, and not others within the same string? 

The entire text string resides within a single variable column.

 

For example in this sentence would like to highlight "Mostly sunny" in yellow and "shower" in purple font.

" Mostly sunny, with a high near 79. chance of an afternoon shower, moderate Southwest wind." (This comments board wont allow for coloring text, I think, so they're just BOLDED).

 

Attached is an example of the text output wanted (Want_text)

A sample SAS dataset (w_1_s_3_tmp)

The Proc Report code used for the SAS dataset (W_Tmp)

proc report data = 	W_1_s_3_TMP			/*headline*/  NOHEADER	style(header)=[font_size=0]  ;
Column 	Summary	;
define	Summary 	/	style(column)=[font_size=4.0 just=l cellwidth=6in	/*cellheight=0.25in*/
borderrightcolor=white	borderleftcolor=white		bordertopcolor=white	borderbottomcolor=white    backgroundcolor=white]	 	; 
run	;

 

I have not been able to find a way of doing this using SAS, and as a work around we've been exporting the Proc Report output as a HTML5 file, and editing by hand.

Thank you for your thoughts on how to do this all within SAS, it would save us allot of time!

R.

1 ACCEPTED SOLUTION

Accepted Solutions
Kathryn_SAS
SAS Employee

I have modified the data using a DATA step to add in-line formatting. 

data w_1_s_3_tmp;
length summary $500;
set w_1_s_3_tmp;
format summary $500.;
informat summary $500.;
if index(strip(summary),'Mostly sunny') gt 0 then do;
 summary=tranwrd(summary,'Mostly sunny','^S={background=yellow} Mostly sunny ^S={}');
end;
if index(summary,'shower') gt 0 then do;
 summary=tranwrd(summary,'shower','^S={foreground=purple font_weight=bold} shower ^S={}');
end;
run;

ods escapechar='^';
ods listing close;
ods html5 path='c:\temp' file='test.html';

proc report data = W_1_s_3_TMP NOHEADER ;
Column 	Summary	;
define	Summary 	/display style(column)=[font_size=4.0 just=l cellwidth=6in	/*cellheight=0.25in*/
borderrightcolor=white	borderleftcolor=white bordertopcolor=white borderbottomcolor=white 
backgroundcolor=white]	 	; 
run	;

ods _all_ close;
ods listing;

I have attached a picture of the output I get.

View solution in original post

3 REPLIES 3
Kathryn_SAS
SAS Employee

I have modified the data using a DATA step to add in-line formatting. 

data w_1_s_3_tmp;
length summary $500;
set w_1_s_3_tmp;
format summary $500.;
informat summary $500.;
if index(strip(summary),'Mostly sunny') gt 0 then do;
 summary=tranwrd(summary,'Mostly sunny','^S={background=yellow} Mostly sunny ^S={}');
end;
if index(summary,'shower') gt 0 then do;
 summary=tranwrd(summary,'shower','^S={foreground=purple font_weight=bold} shower ^S={}');
end;
run;

ods escapechar='^';
ods listing close;
ods html5 path='c:\temp' file='test.html';

proc report data = W_1_s_3_TMP NOHEADER ;
Column 	Summary	;
define	Summary 	/display style(column)=[font_size=4.0 just=l cellwidth=6in	/*cellheight=0.25in*/
borderrightcolor=white	borderleftcolor=white bordertopcolor=white borderbottomcolor=white 
backgroundcolor=white]	 	; 
run	;

ods _all_ close;
ods listing;

I have attached a picture of the output I get.

rmacarthur
Pyrite | Level 9

This is great !  Thank you Kathryn_SAS !

rmacarthur
Pyrite | Level 9

Here is the answer for the font color which works great.  For Highlight, will just include that from the 'Styles' offering.  Thanks: 

data W_1_S_3_TMP ;
  set DAT_1.W_1_S_3_TMP;
  length Summ_1 $95 ;
  Summ_1 = tranwrd(Summary,'Sunny','(*ESC*){style [fontweight=extra_bold color=yellow]Sunny}');
  format Summ_1;
run;

proc report data = 	W_1_s_3_TMP			/*headline*/  NOHEADER	style(header)=[font_size=0]  ;
Column 	Summ_1	;
define	Summ_1 	/	style(column)=[font_size=4.0 just=l cellwidth=6in	/*cellheight=0.25in*/
borderrightcolor=white	borderleftcolor=white		bordertopcolor=white	borderbottomcolor=white    backgroundcolor=white]	 	; 
run	;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 395 views
  • 2 likes
  • 2 in conversation