DATA Step, Macro, Functions and more

Using regular expressions in proc format

Reply
Occasional Contributor
Posts: 6

Using regular expressions in proc format

Hi,

I would like to apply a format to a column such that:
- if it contains "+" then print it red
- if it contains "-" then print it blue
- otherwise print it black

It's supposed to be used to build a title from different columns.

I know that I can include colour formatting in proc report in such way for numeric values:
PROC FORMAT;
value visits 2.54-2.99 = orange 3.02-high = red other = black;
RUN;

Please let me know if you know any solution that solves this problem for text variables (using e.g. regular expressions).

Thanks,
Michal
Frequent Contributor
Posts: 81

Re: Using regular expressions in proc format

I'm not sure if this is possible in proc format.

How about in data step?

if index(value,'+') then color='red';
else if index(value,'-') then color='blue';
else color='black';
Occasional Contributor
Posts: 6

Re: Using regular expressions in proc format

Thanks for your reply.
Do you mean to add an extra column with color name?
Then the question is - how to apply this later when I would like to use this column value in title statement and would like it to be in a specyfic color format?

e.g.

data _null_;
set myset;
titl4 = "Patient: " || trim(put(subjid,???));
call symput('titl4',titl4);
run;

title4 %trim("titl4");
Valued Guide
Posts: 2,175

Re: Using regular expressions in proc format

> It's supposed to be used to build a title from
> different columns.

Michal
( aka joseph17)

I am not quite sure what this "title" means...
I think of a column title being the column heading....

since headings can be defined separately from the data it should be possible to derive special headings for each column.

How are you planning that this should be reported?

peterC
Regular Contributor
Posts: 241

Re: Using regular expressions in proc format

The TITLE statement has ods format options that work for HTML, RTF, and PRINTER destinations. One of them is the COLOR= option to set the title text color. Below is a macro that returns the option and a quoted text string based on the first character of the title. (It "eats up" the first char if it is either a plus(+) or a minus(-) char.)



It is easy to modify it so that it looks for the plus and minus signs in any place in the title text. But you have to make a design decision on how to handle the title with both the signs in.



   %*-- based on the first char, returns a color= option and quoted title text --*;


   %macro title(str);


      %local ch1 color;


      %let str = %superq(str);


      %if &str= %then %return


 


      %let ch1 = %qsysfunc(first(&str));


      %if &ch1 = %str(+) %then 


         %let color = red;


      %else %if &ch1 = %str(-) %then 


         %let color = blue;


      %else 


         %let color = black;


 


      %if &color ^= black %then %let str = %qsubstr(&str,2);


      


      %unquote(color=&color %qsysfunc(quote(&str)))


   %mend  title;


 


 


   /* check */


   options mprint;


   title %title(+title in red);


   /* on log


   MPRINT(TITLE):   color=red "title in red"


   */

Occasional Contributor
Posts: 6

Re: Using regular expressions in proc format

Thank you for this solution - it's almost what I want to get.
I use this TITLE statement in ODS PDF. What I want to get is to highlight only the value that comes from this column, not the whole title. As I mentioned, the title is combined of various columns and I want them to be printed in one line.

My header with titles looks like that:

V1: value1, V2: value2, ... /*title1*/
VN: valueN, VN+1: valueN+1, ... /*title2*/
...

I want, e.g. to print in red only "value2". Is it possible?
N/A
Posts: 0

Re: Using regular expressions in proc format

Have you thought of using ods escapechar with style options?

ods escapechar='^';

title1 "V1: value1, V2: ^{s value2}, ... ";

should do what you need I think?
Valued Guide
Posts: 2,175

Re: Using regular expressions in proc format

as a test to place more than one color in a title line, I ran the code
ods pdf file='test.pdf' style=default ;
proc print data= sashelp.class( obs=5) ;
title1 j=l 'test' h=4 j=c c=red 'middle' '-' c=b 'middle2' j=r c=orange 'right' ;
run;
ods pdf close ;
dm 'winexecfile ''test.pdf'' ' ;
This does show centered text 'middle-middle2' with the "middle-" in red and the "middle2" in blue.
J= provides justification (left/center/right) for the strings that follow, until the next J=
I think this should be easy to generate, once what strings need what colors can be defined.
Note that a title can only change between each PROC (or when relevant, between run-groups as in proc gplot, and probably between the reporting statements by PROC SQL).
peterC
Occasional Contributor
Posts: 6

Re: Using regular expressions in proc format

This helped!
Thank you very much for this clue.

the code that worked finally is:

ods escapechar='^';
title1 "V1: value1, V2: ^{style [foreground=red] value2}, ... ";

More details can be found here:
http://support.sas.com/documentation/cdl/en/odsug/61723/HTML/default/a002233270.htm

Thank you all for the answers.
Michal
Super User
Posts: 9,682

Re: Using regular expressions in proc format

Are you using proc report to make a traffic light?
[pre]
if findc(visit,'+') then call define(_col_,'style','style={}');
[/pre]



Ksharp
Ask a Question
Discussion stats
  • 9 replies
  • 328 views
  • 0 likes
  • 6 in conversation