<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: SAS character format based on string patterns. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-character-format-based-on-string-patterns/m-p/560165#M156559</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12887"&gt;@ErikLund_Jensen&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could define a format from a user-defined function where the function assigns the colors using PRXMATCH:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc fcmp outlib=work.funcs.test;
function nuvcolor(s $) $8;
  color=
    if prxmatch('/\D{1,3}$/o',trim(s)) then 'cxffcccc'
    else if prxmatch('/\D{3}1$/o',trim(s)) then 'cxccccff'
    else 'cxffffff';
  return(color);
endsub;
run;

options cmplib=work.funcs;

proc format;
value $bgcolnuv (default=8)
other=[nuvcolor()];
run;

data test;
set have;
c=put(Nuv, $bgcolnuv.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, in my experience these "fancy" formats tend to cause unexpected errors or even crashes of a SAS session. Indeed, while developing the above code one SAS session crashed and once I got the below error message (from a similar test DATA step as above):&lt;/P&gt;
&lt;PRE&gt;ERROR:  An exception has been encountered.
Please contact technical support and provide them with the following traceback information:

The SAS task name is [DATASTEP]
ERROR:  Read Access Violation DATASTEP
Exception occurred at (0BEADE59)
Task Traceback
Address   Frame     (DBGHELP API Version 4.0 rev 5)
000000000BEADE59  0000000006F3E150  sasdsxp:tkvercn1+0xFCE19
000000000BE77C7E  0000000006F3E1A0  sasdsxp:tkvercn1+0xC6C3E
000000000BDB1914  0000000006F3E560  sasdsxp:tkvercn1+0x8D4
000000000B531187  0000000006F3E568  uwuprxma:tkvercn1+0x147&lt;/PRE&gt;
&lt;P&gt;So, I would be hesitant to use this in production code.&lt;/P&gt;</description>
    <pubDate>Mon, 20 May 2019 14:32:57 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2019-05-20T14:32:57Z</dc:date>
    <item>
      <title>SAS character format based on string patterns.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-character-format-based-on-string-patterns/m-p/560099#M156528</link>
      <description>&lt;P&gt;Hi all&lt;/P&gt;
&lt;P&gt;I have requestors who want SAS data sets exported to XLSX with cell background colors depending on data values. I have a data set variable with some 3500 different alphanumeric strings, which should have background-color set after the following rules:&lt;/P&gt;
&lt;P&gt;1-3 alphabetic chars only : red.&lt;BR /&gt;3 alphabetic chars + 1 (digit): blue&lt;BR /&gt;all other : white.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I use a format as shown in the test code below, where the format is built from actual data with all input values to be set to red or blue listed. It is a working solution, but I would prefer a permanent format with defined rules independent on actual data. Unfortunately SAS does not support the following syntax. As fas as I know, SAS allows REGEXP in informats only, and it can only be used with other formats as labels, not constant values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
	 value $bgcolnuv
		 '/\D{1,3}$/' (REGEXP) = 'cxffcccc'
		 '/\D{3}1$/' (REGEXP) =  'cxccccff'
		 other = 'cxffffff';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;But I need something to to the same, and I think there &lt;U&gt;has&lt;/U&gt; to be a smarter solution than my "brute-force-format". So I hope someone knows of a way to define such a format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Test code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* sample data;
data have;
	input Nuv $4.;
	cards;
HG
AHR
CFG1
CBH3
BBB2
1217
1611
;
run;

* Format to set background color;
proc sql;
	create table cval as
		select distinct Nuv
		from have;
quit;

data _null_; set cval end=eof;
	if _N_ = 1 then call execute('proc format; value $bgcolnuv');
	if prxmatch('/\D{1,3}$/',trim(Nuv)) then call execute(Nuv || '= "cxffcccc"');
	else if prxmatch('/\D{3}1$/',trim(Nuv)) then call execute(Nuv || '= "cxccccff"');
	if eof then call execute('other="cxffffff"; run;');
run;

ods listing close;
ods excel file="c:\temp\test.xlsx";
proc print data=have ;
	var nuv / style={background=$bgcolnuv.};
run;
ods excel close;
ods listing;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="excel.gif" style="width: 154px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/29643i7FFB2360F38063CF/image-size/large?v=v2&amp;amp;px=999" role="button" title="excel.gif" alt="excel.gif" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 May 2019 11:31:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-character-format-based-on-string-patterns/m-p/560099#M156528</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-05-20T11:31:57Z</dc:date>
    </item>
    <item>
      <title>Re: SAS character format based on string patterns.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-character-format-based-on-string-patterns/m-p/560165#M156559</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12887"&gt;@ErikLund_Jensen&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could define a format from a user-defined function where the function assigns the colors using PRXMATCH:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc fcmp outlib=work.funcs.test;
function nuvcolor(s $) $8;
  color=
    if prxmatch('/\D{1,3}$/o',trim(s)) then 'cxffcccc'
    else if prxmatch('/\D{3}1$/o',trim(s)) then 'cxccccff'
    else 'cxffffff';
  return(color);
endsub;
run;

options cmplib=work.funcs;

proc format;
value $bgcolnuv (default=8)
other=[nuvcolor()];
run;

data test;
set have;
c=put(Nuv, $bgcolnuv.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, in my experience these "fancy" formats tend to cause unexpected errors or even crashes of a SAS session. Indeed, while developing the above code one SAS session crashed and once I got the below error message (from a similar test DATA step as above):&lt;/P&gt;
&lt;PRE&gt;ERROR:  An exception has been encountered.
Please contact technical support and provide them with the following traceback information:

The SAS task name is [DATASTEP]
ERROR:  Read Access Violation DATASTEP
Exception occurred at (0BEADE59)
Task Traceback
Address   Frame     (DBGHELP API Version 4.0 rev 5)
000000000BEADE59  0000000006F3E150  sasdsxp:tkvercn1+0xFCE19
000000000BE77C7E  0000000006F3E1A0  sasdsxp:tkvercn1+0xC6C3E
000000000BDB1914  0000000006F3E560  sasdsxp:tkvercn1+0x8D4
000000000B531187  0000000006F3E568  uwuprxma:tkvercn1+0x147&lt;/PRE&gt;
&lt;P&gt;So, I would be hesitant to use this in production code.&lt;/P&gt;</description>
      <pubDate>Mon, 20 May 2019 14:32:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-character-format-based-on-string-patterns/m-p/560165#M156559</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-05-20T14:32:57Z</dc:date>
    </item>
    <item>
      <title>Re: SAS character format based on string patterns.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-character-format-based-on-string-patterns/m-p/560214#M156592</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So there &lt;U&gt;was&lt;/U&gt; another solution. I wasn't aware that a function could be used in a format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code didn't crash when I tried it on my Windows laptop or linux Grid, but as it seems to do so now an then, I will follow your advise and not use it in a production environment, so I will continue to use my temporary format, even if it seems a clumsy solution. It also spares me from documenting and maintaining the extra function and format job.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am not finished with this use of functions. I have a similar problem with overdue dates, and maybe it will be more stable with numeric arguments and aritmethics only, so I am very grateful that you took the time to write that program for me,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 May 2019 16:23:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-character-format-based-on-string-patterns/m-p/560214#M156592</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-05-20T16:23:16Z</dc:date>
    </item>
    <item>
      <title>Re: SAS character format based on string patterns.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-character-format-based-on-string-patterns/m-p/560218#M156594</link>
      <description>&lt;P&gt;You can define a format from data using the CNTLIN= option on PROC FORMAT instead of generating lines of code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table NUV_FMT as
  select distinct 
  '$BGCOLNUV' as fmtname
  ,nuv as start
  ,case when prxmatch('/\D{1,3}$/',trim(Nuv)) then 'cxffcccc'
        when prxmatch('/\D{3}1$/',trim(Nuv)) then 'cxccccff'
        else 'cxffffff'
   end as label
   from have
   order by 1,2
;
quit;

proc format cntlin=nuv_fmt;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 May 2019 16:41:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-character-format-based-on-string-patterns/m-p/560218#M156594</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-20T16:41:48Z</dc:date>
    </item>
  </channel>
</rss>

