<?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: PRXMATCH not work in nested loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PRXMATCH-not-work-in-nested-loop/m-p/799941#M314604</link>
    <description>&lt;P&gt;Why are you using REGEX for something that normal operators and functions can handle?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let exp1='I50';
%let exp2='I49';
%let exp3='I21';
%let exp4='I63';
%let exp5='R06' 'J95' 'J13' 'J14' 'J15' 'J16' 'J17' 'J18' ;
%let n=5;

data array2;
  set original;
  array dgns_array [*] AD_DGNS DGNS_CD01-DGNS_CD25; 
  array var [&amp;amp;n] ;
%do j=1 %to &amp;amp;n ;
  do i=1 to dim(dgns_array) until (var[&amp;amp;j]);
    var[&amp;amp;j] = dgns_array[i] in: (&amp;amp;&amp;amp;exp&amp;amp;j);
  end;
%end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 03 Mar 2022 19:23:31 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-03-03T19:23:31Z</dc:date>
    <item>
      <title>PRXMATCH not work in nested loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PRXMATCH-not-work-in-nested-loop/m-p/799715#M314472</link>
      <description>&lt;P&gt;Hi, I want to create some new binary variables based on whether part of the original variables of each observation has pattern matched values. (If anybody is familiar, I use the MedPAR data.) Each new variable has its own corresponding value. So I try to code this out with loop and arrary.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It works out when there is only one loop (create only one new variable).&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data array1;
set original;&lt;BR /&gt;&lt;BR /&gt;	exp1='/^I50/o';
	exp2='/^I49/o';
	exp3='/^I21/o';
	exp4='/^I63/o';
	exp5='/^R06|^J95|^J1[345678]/o';
	
	array exp [*] exp1-exp5; /*the expression to find and match to create each new variable*/
	array var [*] var1-var5;
	array dgns_array [*] AD_DGNS DGNS_CD01-DGNS_CD25; /*the original variables to scan and check*/ 
	&lt;BR /&gt;	var[2]=0; /*set the default value of the new variable*/&lt;BR /&gt;
		do i=1 to 26;
		if  prxmatch(exp[16], dgns_array[i])&amp;gt;0 then 
			do;
				var[2]=1;
				leave;
			end;
		end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, when I write the nested loop,all the new variables have the same value as the first one.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data array2;
set original;

    exp1='/^I50/o';
    exp2='/^I49/o';
    exp3='/^I21/o';
    exp4='/^I63/o';
    exp5='/^R06|^J95|^J1[345678]/o';
	
    array exp [*] exp1-exp5; /*the expression to find and match to create each new variable*/
    array var [*] var1-var5;
    array dgns_array [*] AD_DGNS DGNS_CD01-DGNS_CD25;  /*the original variables to scan and check*/ 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;    do n=1 to dim(var);&lt;BR /&gt;&amp;nbsp; &amp;nbsp;    var[n]=0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end;&lt;BR /&gt;    /*set the default value of the new variable*/&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;do j=1 to dim(var);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do i=1 to 26;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if &amp;nbsp;prxmatch(exp[j], dgns_array[i])&amp;gt;0 then &lt;BR /&gt;             do;&lt;BR /&gt;               var[j]=1;&lt;BR /&gt;               leave;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;  end;&lt;BR /&gt;        end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end;
run;&lt;/CODE&gt;&lt;CODE class=""&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2022-03-02_202958.PNG" style="width: 386px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/69093i50526E68CC77937D/image-size/large?v=v2&amp;amp;px=999" role="button" title="2022-03-02_202958.PNG" alt="2022-03-02_202958.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I have no idea why this didn't work. Any hint or advice of other functions to do the same thing will be appreciated.&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Mar 2022 01:35:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PRXMATCH-not-work-in-nested-loop/m-p/799715#M314472</guid>
      <dc:creator>rsjjj</dc:creator>
      <dc:date>2022-03-03T01:35:58Z</dc:date>
    </item>
    <item>
      <title>Re: PRXMATCH not work in nested loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PRXMATCH-not-work-in-nested-loop/m-p/799799#M314530</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/335711"&gt;@rsjjj&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities (as a first-time poster)!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The simplified example below works ...&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input (d1-d3) (:$4.);
cards;
I250 I350 I450
I390 I490 I590
I500 I600 I700
I470 I480 I490
;

data want(drop=i j n);
set have;
array exp[2] $25 ('/^I50/' '/^I49/');
array var[2];
array d[3];

do n=1 to dim(var);
  var[n]=0;
end;
  
do j=1 to dim(var);
  do i=1 to dim(d);
    if prxmatch(exp[j], d[i]) then do;
      var[j]=1;
      leave;
    end;
  end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;... but it fails in the way you've described when I insert the "o" option into the regular expressions (&lt;FONT face="courier new,courier"&gt;'/^I50/&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;o&lt;/FONT&gt;&lt;/STRONG&gt;' '/^I49/&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;o&lt;/FONT&gt;&lt;/STRONG&gt;'&lt;/FONT&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Conclusion: Remove the "o" option from your regular expressions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit:&lt;/P&gt;
&lt;P&gt;Explanation: With the "compile-once" behavior triggered by the "o" option (see &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n0bj9p4401w3n9n1gmv6tf**bleep**9m.htm#n0jba0adj9x3nyn1v61mcuuhk0xc" target="_blank" rel="noopener"&gt;Compiling a Perl Regular Expression&lt;/A&gt;) the regex compiled in the first call of the PRXMATCH function, i.e., &lt;FONT face="courier new,courier"&gt;'/^I50/o'&lt;/FONT&gt;, is used in &lt;EM&gt;all&lt;/EM&gt; subsequent calls of that function in the nested DO loops. Therefore, the pattern&amp;nbsp;&lt;FONT face="courier new,courier"&gt;'/^I49/'&lt;/FONT&gt;&amp;nbsp;is &lt;EM&gt;not&lt;/EM&gt;&amp;nbsp;searched for in observations 2 and 4 (where it is present), resulting in VAR2=0, and in the third observation (where&amp;nbsp;&lt;FONT face="courier new,courier"&gt;'/^I50/'&lt;/FONT&gt; is found) not only VAR1 is set to 1, but also VAR2.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Mar 2022 11:50:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PRXMATCH-not-work-in-nested-loop/m-p/799799#M314530</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-03-03T11:50:32Z</dc:date>
    </item>
    <item>
      <title>Re: PRXMATCH not work in nested loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PRXMATCH-not-work-in-nested-loop/m-p/799899#M314582</link>
      <description>Thank you so much! I've been stuck with this for several days but it didn't come to me that the issue is the "o" option. Trying to save the running time by using it at the beginning....</description>
      <pubDate>Thu, 03 Mar 2022 16:18:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PRXMATCH-not-work-in-nested-loop/m-p/799899#M314582</guid>
      <dc:creator>rsjjj</dc:creator>
      <dc:date>2022-03-03T16:18:54Z</dc:date>
    </item>
    <item>
      <title>Re: PRXMATCH not work in nested loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PRXMATCH-not-work-in-nested-loop/m-p/799941#M314604</link>
      <description>&lt;P&gt;Why are you using REGEX for something that normal operators and functions can handle?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let exp1='I50';
%let exp2='I49';
%let exp3='I21';
%let exp4='I63';
%let exp5='R06' 'J95' 'J13' 'J14' 'J15' 'J16' 'J17' 'J18' ;
%let n=5;

data array2;
  set original;
  array dgns_array [*] AD_DGNS DGNS_CD01-DGNS_CD25; 
  array var [&amp;amp;n] ;
%do j=1 %to &amp;amp;n ;
  do i=1 to dim(dgns_array) until (var[&amp;amp;j]);
    var[&amp;amp;j] = dgns_array[i] in: (&amp;amp;&amp;amp;exp&amp;amp;j);
  end;
%end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Mar 2022 19:23:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PRXMATCH-not-work-in-nested-loop/m-p/799941#M314604</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-03-03T19:23:31Z</dc:date>
    </item>
    <item>
      <title>Re: PRXMATCH not work in nested loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PRXMATCH-not-work-in-nested-loop/m-p/799970#M314608</link>
      <description>&lt;P&gt;Adding to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt; suggestion, you can compile only once by calling prxParse first (only once) and then call prxMatch with the pattern IDs:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ata want(drop=i j n);
set have;
array exp[2] $25 _temporary_ ('/^I50/' '/^I49/');
array rId {2} _temporary_;
array var[2];
array d[3];

if _n_ = 1 then do;
    do n = 1 to dim(rId);
        rId{n} = prxParse(exp{n});
        end;
    end;
    
do n=1 to dim(var);
    var[n]=0;
    end;
  
do j=1 to dim(var);
      do i=1 to dim(d);
            if prxmatch(rId[j], d[i]) then do;
                  var[j]=1;
                  leave;
                  end;
            end;
      end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Mar 2022 20:46:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PRXMATCH-not-work-in-nested-loop/m-p/799970#M314608</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2022-03-04T20:46:13Z</dc:date>
    </item>
    <item>
      <title>Re: PRXMATCH not work in nested loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PRXMATCH-not-work-in-nested-loop/m-p/800061#M314678</link>
      <description>Thanks! That's exactly what I tried to achieve at first</description>
      <pubDate>Fri, 04 Mar 2022 04:44:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PRXMATCH-not-work-in-nested-loop/m-p/800061#M314678</guid>
      <dc:creator>rsjjj</dc:creator>
      <dc:date>2022-03-04T04:44:47Z</dc:date>
    </item>
    <item>
      <title>Re: PRXMATCH not work in nested loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PRXMATCH-not-work-in-nested-loop/m-p/800062#M314679</link>
      <description>It's really an efficient method! Thanks for the advice</description>
      <pubDate>Fri, 04 Mar 2022 04:47:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PRXMATCH-not-work-in-nested-loop/m-p/800062#M314679</guid>
      <dc:creator>rsjjj</dc:creator>
      <dc:date>2022-03-04T04:47:08Z</dc:date>
    </item>
  </channel>
</rss>

