<?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: Regex: Negative lookbehind with variable length? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Regex-Negative-lookbehind-with-variable-length/m-p/844680#M333934</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/366713"&gt;@PharmlyDoc&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How about searching for the positive and negative words separately and then comparing character positions?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data text_prx(drop=re_: pos_:);
set text;
if _n_=1 then do; 
  re_i + prxparse('/(infarct)|(\bMI\b)|(myocardial infarction)/i'); 
  re_n + prxparse('/\bno\b|\bnegative\b/i'); 
end;
pos_i = prxmatch(re_i,string);
pos_n = prxmatch(re_n,string);
infarct = pos_i &amp;amp; not (0&amp;lt;pos_n&amp;lt;pos_i); 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(Of course, "infarct" is a substring of "myocardial infarction", but I assume that you may want to evaluate the matches, e.g., with &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n1lru1b4uoogqvn1ig446q4c6muu.htm" target="_blank" rel="noopener"&gt;PRXPOSN&lt;/A&gt;.)&lt;/P&gt;</description>
    <pubDate>Wed, 16 Nov 2022 18:20:54 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2022-11-16T18:20:54Z</dc:date>
    <item>
      <title>Regex: Negative lookbehind with variable length?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Regex-Negative-lookbehind-with-variable-length/m-p/844399#M333823</link>
      <description>&lt;P&gt;Are there alternatives to negative lookbehind regular expressions with variable lengths?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do not match if "no" or "negative" are before certain key words&amp;nbsp;&lt;CODE class=" language-sas"&gt;(infarct)|(MI)|(myocardial infarction).&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS returns the following error:&amp;nbsp;&lt;FONT color="#FF0000"&gt;&lt;SPAN&gt;Variable length lookbehind not implemented before HERE mark in regex &lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data text;
input string $60.;
cards;
Acute infarct.
Acute MI. 
Acute myocardial infarction. 
Extensive infarct found in the.
Massive infarct found in the.
No infarct. 
Negative infarct.
Negative for infarct.
No CT evidence of infarct.
No apparent evidence of infarct.
No apparent CT evidence of infarct. 
No evidence of infarct.
No evidence of infarct.
Evidence of infarct.
Evidence for infarct. 
;

/* Only Match: 
Acute infarct
Acute MI
Acute myocardial infarction
Extensive infarct found in the
Massive infarct found in the
evidence of infarct  
evidence for infarct
*/

data text_prx;
 set text;
 if _n_=1 then do; 
 retain re; 
 re = prxparse('/(?&amp;lt;!\bno\b.{0,225}|\bnegative\b.{0,225})((infarct)|(MI)|(myocardial infarction))/i'); 
 putlog 'ERROR: regex is malformed'; 
 stop; 
 end; 
 end;

 if prxmatch(re,string) then infarct=1; 
 else infarct=0;
 run;

proc print data=text_prx(drop=re);
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Nov 2022 15:37:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Regex-Negative-lookbehind-with-variable-length/m-p/844399#M333823</guid>
      <dc:creator>PharmlyDoc</dc:creator>
      <dc:date>2022-11-15T15:37:19Z</dc:date>
    </item>
    <item>
      <title>Re: Regex: Negative lookbehind with variable length?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Regex-Negative-lookbehind-with-variable-length/m-p/844680#M333934</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/366713"&gt;@PharmlyDoc&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How about searching for the positive and negative words separately and then comparing character positions?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data text_prx(drop=re_: pos_:);
set text;
if _n_=1 then do; 
  re_i + prxparse('/(infarct)|(\bMI\b)|(myocardial infarction)/i'); 
  re_n + prxparse('/\bno\b|\bnegative\b/i'); 
end;
pos_i = prxmatch(re_i,string);
pos_n = prxmatch(re_n,string);
infarct = pos_i &amp;amp; not (0&amp;lt;pos_n&amp;lt;pos_i); 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(Of course, "infarct" is a substring of "myocardial infarction", but I assume that you may want to evaluate the matches, e.g., with &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n1lru1b4uoogqvn1ig446q4c6muu.htm" target="_blank" rel="noopener"&gt;PRXPOSN&lt;/A&gt;.)&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2022 18:20:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Regex-Negative-lookbehind-with-variable-length/m-p/844680#M333934</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-11-16T18:20:54Z</dc:date>
    </item>
    <item>
      <title>Re: Regex: Negative lookbehind with variable length?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Regex-Negative-lookbehind-with-variable-length/m-p/844707#M333946</link>
      <description>&lt;P&gt;Note, if you need to know which of many possible substrings was matched, (using PRXPOSN) you should list the longer substrings first&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if _n_=1 then do; 
  re_i + prxparse('/\b(myocardial infarction|infarct|MI)\b/i'); 
  re_n + prxparse('/\bno\b|\bnegative\b/i'); 
end;
pos_i = prxmatch(re_i,string);
pos_n = prxmatch(re_n,string);
infarct = pos_i &amp;amp; not (0&amp;lt;pos_n&amp;lt;pos_i); 
if infarct then word = prxposn(re_i, 1, string);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2022 20:42:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Regex-Negative-lookbehind-with-variable-length/m-p/844707#M333946</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2022-11-16T20:42:05Z</dc:date>
    </item>
    <item>
      <title>Re: Regex: Negative lookbehind with variable length?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Regex-Negative-lookbehind-with-variable-length/m-p/844720#M333954</link>
      <description>&lt;P&gt;Thanks,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;, for chiming in. I was under the impression that "&lt;FONT face="courier new,courier"&gt;myocardial infarction&lt;/FONT&gt;" is matched first in strings like "&lt;FONT face="courier new,courier"&gt;Acute myocardial infarction&lt;/FONT&gt;" even if listed after "&lt;FONT face="courier new,courier"&gt;infarct&lt;/FONT&gt;" in the regular expression because it starts earlier in the string. However, if the regex was&amp;nbsp;&lt;FONT face="courier new,courier"&gt;/(infarction|infarct)/i&lt;/FONT&gt;, then the order of the two words would make a difference for PRXPOSN. (My guess that PRXPOSN might be involved somewhere else in the OP's code was just based on the way the regex was written.)&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2022 22:18:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Regex-Negative-lookbehind-with-variable-length/m-p/844720#M333954</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-11-16T22:18:05Z</dc:date>
    </item>
    <item>
      <title>Re: Regex: Negative lookbehind with variable length?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Regex-Negative-lookbehind-with-variable-length/m-p/844730#M333959</link>
      <description>&lt;P&gt;&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;Thanks for helping with this.&lt;/P&gt;
&lt;P&gt;Why use&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;re_i + prxparse('/ /');&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;instead of&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;re_i = prxparse('/ /');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would still use your method even if SAS provided a flavor of regex that allows for variable length negative lookbehind. This is an excellent workaround!!&lt;/P&gt;
&lt;P&gt;Yes, I've found the prxposn function helpful for seeing what is being captured/matched.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Nov 2022 00:19:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Regex-Negative-lookbehind-with-variable-length/m-p/844730#M333959</guid>
      <dc:creator>PharmlyDoc</dc:creator>
      <dc:date>2022-11-17T00:19:01Z</dc:date>
    </item>
    <item>
      <title>Re: Regex: Negative lookbehind with variable length?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Regex-Negative-lookbehind-with-variable-length/m-p/844759#M333968</link>
      <description>&lt;P&gt;You're welcome.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/366713"&gt;@PharmlyDoc&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Why use&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;re_i + prxparse('/ /');&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;instead of&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;re_i = prxparse('/ /');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Just to save the RETAIN statement. The &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm" target="_blank" rel="noopener"&gt;sum statement&lt;/A&gt; &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm#p0xvv5gnfsao7gn1x43qpv5dt6ic" target="_blank" rel="noopener"&gt;implies RETAIN&lt;/A&gt; and the result in &lt;FONT face="courier new,courier"&gt;re_i&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;re_n&lt;/FONT&gt;, respectively, is the same as with an assignment statement. (I learned this application of the sum statement to regex definitions from PGStats, years ago.)&lt;/P&gt;</description>
      <pubDate>Thu, 17 Nov 2022 08:20:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Regex-Negative-lookbehind-with-variable-length/m-p/844759#M333968</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-11-17T08:20:04Z</dc:date>
    </item>
  </channel>
</rss>

