<?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: Need to create a pattern in regular expression in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-pattern-in-regular-expression/m-p/722911#M224241</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set test4;
	if Sex='M' then	flag=ifc(ifc(first(Name)eq first(ID)   ,1,0) eq 1 and prxmatch("m/[a-zA-Z]\d{11}/oi",ID),'Valid','Invalid');
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                                          Obs     Name      Sex         ID          flag

                                           1     Alice       M     A10294354554    Valid  
                                           2     Barbara     M     C02943545549    Invalid
                                           3     Carol       F     C02943545543           
                                           4     Henry       M     H02943545540    Valid  
                                           5     James       M     K02943545545    Invalid
&lt;/PRE&gt;</description>
    <pubDate>Tue, 02 Mar 2021 16:51:13 GMT</pubDate>
    <dc:creator>r_behata</dc:creator>
    <dc:date>2021-03-02T16:51:13Z</dc:date>
    <item>
      <title>Need to create a pattern in regular expression</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-pattern-in-regular-expression/m-p/722877#M224221</link>
      <description>&lt;P&gt;Hi Team,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need a pattern of 1 character and 11 numeric values, but the challenge is the 1st character should be the first letter of name column postfix with 11 numeric values and sex='M'.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test4;
input Name $ Sex $ ID $12.;
datalines;
Alice M A10294354554
Barbara M C02943545549
Carol F C02943545543
Henry M H02943545540
James M K02943545545
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In the above sample I need to validate the ID column with a flag value 'Valid' or 'Invalid'.&lt;/P&gt;
&lt;P&gt;If the sex='M'&amp;nbsp; and ID value contains the first character of Name column and 11 numeric values then it's a valid ID else it's an invalid ID.&lt;/P&gt;
&lt;P&gt;I tried to take the first character of Name column into a macro variable and tried to write the program.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test;
data want;
set test4;
%if Sex='M' %then %do;
proc sql;
select strip(substr(name,1,1)) into :First_Char from test4;
quit;
%end;
match=prxmatch("/^&amp;amp;First_Char\d{11}$/",ID);&lt;BR /&gt;%if match&amp;gt;1 %then %do;&lt;BR /&gt;flag='Invalid';&lt;BR /&gt;%end; &lt;BR /&gt;%else %do;&lt;BR /&gt;flag='Valid';&lt;BR /&gt;%end;&lt;BR /&gt;run;
%mend;
%test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am not getting any match, though 'Alice' and 'Henry' ID are &lt;STRONG&gt;valid&lt;/STRONG&gt;, both IDs contains first character of name and 11 numbers beside sex='M' for both of them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please help me correct the logic.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Mar 2021 15:32:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-pattern-in-regular-expression/m-p/722877#M224221</guid>
      <dc:creator>rajdeep</dc:creator>
      <dc:date>2021-03-02T15:32:39Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a pattern in regular expression</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-pattern-in-regular-expression/m-p/722889#M224227</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13220"&gt;@rajdeep&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There's no need for macro coding. Even regular expressions can be avoided:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test5;
input Name $ Sex $ ID $12.;
datalines;
Alice M A10294354554
Barbara M C02943545549
Carol F C02943545543
Henry M H02943545540
James M K02943545545
Robert M R029435A5540
Timothy M T0294355540
;

data want;
set test5;
flag=(sex='M' &amp;amp; id=:first(name) &amp;amp; ~findc(id,,'kd',2));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;[Edit: Removed the redundant criterion &lt;FONT face="courier new,courier"&gt;length(id)=12&lt;/FONT&gt;.]&lt;/P&gt;
&lt;P&gt;I leave it to you to create character values &lt;FONT face="courier new,courier"&gt;'Valid'&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;'Invalid'&lt;/FONT&gt; from the 1s and 0s produced by the code above if needed. Personally, I prefer these 0-1 flags because they are easier to handle and character labels can be assigned with a simple format:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
value valflag
0='Invalid'
1='Valid';
run;

proc print data=want;
format flag valflag.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Mar 2021 16:22:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-pattern-in-regular-expression/m-p/722889#M224227</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-03-02T16:22:17Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a pattern in regular expression</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-pattern-in-regular-expression/m-p/722911#M224241</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set test4;
	if Sex='M' then	flag=ifc(ifc(first(Name)eq first(ID)   ,1,0) eq 1 and prxmatch("m/[a-zA-Z]\d{11}/oi",ID),'Valid','Invalid');
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                                          Obs     Name      Sex         ID          flag

                                           1     Alice       M     A10294354554    Valid  
                                           2     Barbara     M     C02943545549    Invalid
                                           3     Carol       F     C02943545543           
                                           4     Henry       M     H02943545540    Valid  
                                           5     James       M     K02943545545    Invalid
&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Mar 2021 16:51:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-pattern-in-regular-expression/m-p/722911#M224241</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2021-03-02T16:51:13Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a pattern in regular expression</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-pattern-in-regular-expression/m-p/722914#M224244</link>
      <description>&lt;P&gt;hi&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A class="trigger-hovercard" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733" target="_blank"&gt;FreelanceReinhard&lt;/A&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks a lot for the quick reply.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am just wondering what if the ID value length is not standard. Means, suppose one of the ID values has13 or 15 digit characters, but other values has 12 characters. Like below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;input Name $ Sex $ ID $13.;&lt;BR /&gt;datalines;&lt;BR /&gt;Alice M A102943545547&lt;BR /&gt;Barbara M C02943545549&lt;BR /&gt;Carol F C02943545543&lt;BR /&gt;Henry M H02943545540&lt;BR /&gt;James M K02943545545&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;In the above sample you can see Alice has 13 char ID value, but Henry is having 12 char value with a trailing space, so in that case Henry's ID value is valid. will your code work in that case!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;
&lt;DIV class="sas-author-username"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Tue, 02 Mar 2021 17:01:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-pattern-in-regular-expression/m-p/722914#M224244</guid>
      <dc:creator>rajdeep</dc:creator>
      <dc:date>2021-03-02T17:01:50Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a pattern in regular expression</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-pattern-in-regular-expression/m-p/722918#M224247</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13220"&gt;@rajdeep&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;hi&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A class="trigger-hovercard" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733" target="_blank" rel="noopener"&gt;FreelanceReinhard&lt;/A&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks a lot for the quick reply.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am just wondering what if the ID value length is not standard. Means, suppose one of the ID values has13 or 15 digit characters, but other values has 12 characters. Like below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;input Name $ Sex $ ID $13.;&lt;BR /&gt;datalines;&lt;BR /&gt;Alice M A102943545547&lt;BR /&gt;Barbara M C02943545549&lt;BR /&gt;Carol F C02943545543&lt;BR /&gt;Henry M H02943545540&lt;BR /&gt;James M K02943545545&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;In the above sample you can see Alice has 13 char ID value, but Henry is having 12 char value with a trailing space, so in that case Henry's ID value is valid. will your code work in that case!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;
&lt;DIV class="sas-author-username"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;With the amended structure of the input dataset (defined length of ID is now 13) the condition &lt;FONT face="courier new,courier"&gt;length(id)=12&lt;/FONT&gt; is no longer redundant and trailing blanks must be considered in the third argument of the FINDC function:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;data want;
set test5;
flag=(sex='M' &amp;amp; id=:first(name) &lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;&amp;amp; length(id)=12&lt;/FONT&gt;&lt;/STRONG&gt; &amp;amp; ~findc(id,,'kd&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;t&lt;/STRONG&gt;&lt;/FONT&gt;',2));
run;&lt;/FONT&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Mar 2021 17:21:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-pattern-in-regular-expression/m-p/722918#M224247</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-03-02T17:21:01Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a pattern in regular expression</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-pattern-in-regular-expression/m-p/722927#M224252</link>
      <description>&lt;P&gt;Wow, this is something great.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks a lot.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Mar 2021 17:35:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-pattern-in-regular-expression/m-p/722927#M224252</guid>
      <dc:creator>rajdeep</dc:creator>
      <dc:date>2021-03-02T17:35:36Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a pattern in regular expression</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-pattern-in-regular-expression/m-p/726527#M225754</link>
      <description>&lt;P&gt;Hi Team,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am want to output specific records based on condition, but it seems it's not happening for some reason.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data test;
input CCI_STATE $2. CCI_ID_NUMBER $14. CCI_ID_TYPE $21.;
datalines;
CA	CAID C6307718	DRIVERS LIC/STATE ID
CA	N31790267	    DRIVERS LIC/STATE ID
CA	A6118782	    DRIVERS LIC/STATE ID
CA	B3765104	    DRIVERS LIC/STATE ID
CA	Y5652061	    DRIVERS LIC/STATE ID
;
run;

data valid invalid;
set test;
if CCI_STATE='CA' and CCI_ID_Type='DRIVERS LIC/STATE ID' and prxmatch("/^(?&amp;lt;!\w)\w{1}\d{7}(?!\d)$/",compress(CCI_ID_Number, ,'s'))=0  then output invalid;
else  output valid;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;so, basically out of 5 records I would like to output the (1 alpha+7numeric) ID numbers to valid dataset and remaining 2 records to invalid dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;While running the code it's only outputting the 'N31790267' ID to invalid, not the 'CAID C6307718' ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if I am checking the prxmatch result for the particular&amp;nbsp;'CAID C6307718' ID it's giving 0 only, but it's not outputting the same record to invalid dataset based on the if condition.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would request please suggest something with regular expression facility.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 15 Mar 2021 20:17:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-pattern-in-regular-expression/m-p/726527#M225754</guid>
      <dc:creator>rajdeep</dc:creator>
      <dc:date>2021-03-15T20:17:53Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a pattern in regular expression</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-pattern-in-regular-expression/m-p/726563#M225775</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13220"&gt;@rajdeep&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would prefer a logic that requires certain criteria for a classification as "valid" and rejects everything &lt;EM&gt;else&lt;/EM&gt; as "invalid."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if ... and prxmatch('/^[A-Z]\d{7}$/',trim(CCI_ID_Number)) then output valid;
else output invalid;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To allow also lowercase letters (as in "&lt;FONT face="courier new,courier"&gt;a1234567&lt;/FONT&gt;") insert the &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt; modifier after the closing slash of the regular expression. Of course, the IF conditions regarding&amp;nbsp;&lt;FONT face="courier new,courier"&gt;CCI_STATE&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;CCI_ID_TYPE&lt;/FONT&gt; would need to be adapted if, e.g., data from other states were to be classified.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the future, please open a new thread for a new question. This improves the chances of getting a quick answer because only very few people will notice a late post in an old thread.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Mar 2021 22:14:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-pattern-in-regular-expression/m-p/726563#M225775</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-03-15T22:14:22Z</dc:date>
    </item>
  </channel>
</rss>

