<?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: How Drop/Keep Rows in a Dataset Based on Sample Name Patterns? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-Drop-Keep-Rows-in-a-Dataset-Based-on-Sample-Name-Patterns/m-p/778154#M31494</link>
    <description>&lt;PRE&gt;data have;
input have $20.;
if prxmatch('/^[A-H]\-\d\d\d$/i',strip(have)) then matched=1;
cards;
A-095
IQP17-UM-1997 
;&lt;/PRE&gt;</description>
    <pubDate>Wed, 03 Nov 2021 12:00:24 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2021-11-03T12:00:24Z</dc:date>
    <item>
      <title>How Drop/Keep Rows in a Dataset Based on Sample Name Patterns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-Drop-Keep-Rows-in-a-Dataset-Based-on-Sample-Name-Patterns/m-p/778037#M31483</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Very new to SAS.&amp;nbsp; I have a data set with a variable/column called sample.&amp;nbsp; I have read in this dataset, but I would like to keep only specific rows that match a sample name pattern.&amp;nbsp; All of the ones that I would like to keep start with (A-H) and are then followed by a - and 3 numbers&amp;nbsp;&lt;/P&gt;&lt;P&gt;for example:&lt;/P&gt;&lt;P&gt;A-095&lt;/P&gt;&lt;P&gt;How do I keep only rows with samples in this format and drop other rows for example:&lt;/P&gt;&lt;P&gt;IQP17-UM-1997&amp;nbsp;&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;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Nov 2021 21:48:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-Drop-Keep-Rows-in-a-Dataset-Based-on-Sample-Name-Patterns/m-p/778037#M31483</guid>
      <dc:creator>SAS49</dc:creator>
      <dc:date>2021-11-02T21:48:57Z</dc:date>
    </item>
    <item>
      <title>Re: How Drop/Keep Rows in a Dataset Based on Sample Name Patterns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-Drop-Keep-Rows-in-a-Dataset-Based-on-Sample-Name-Patterns/m-p/778067#M31484</link>
      <description>&lt;P&gt;I'm sure people who know parsing functions can give you a shorter expression to do the trick.&amp;nbsp; In the meantime, this should work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   if ("A" &amp;lt;=: sample &amp;lt;= "H") and substr(sample, 2, 1) = "-" and
   length (sample) = 5 and input(substr(sample, 3), ?? 3.) &amp;gt; .;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Nov 2021 00:08:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-Drop-Keep-Rows-in-a-Dataset-Based-on-Sample-Name-Patterns/m-p/778067#M31484</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-11-03T00:08:03Z</dc:date>
    </item>
    <item>
      <title>Re: How Drop/Keep Rows in a Dataset Based on Sample Name Patterns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-Drop-Keep-Rows-in-a-Dataset-Based-on-Sample-Name-Patterns/m-p/778154#M31494</link>
      <description>&lt;PRE&gt;data have;
input have $20.;
if prxmatch('/^[A-H]\-\d\d\d$/i',strip(have)) then matched=1;
cards;
A-095
IQP17-UM-1997 
;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Nov 2021 12:00:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-Drop-Keep-Rows-in-a-Dataset-Based-on-Sample-Name-Patterns/m-p/778154#M31494</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-11-03T12:00:24Z</dc:date>
    </item>
    <item>
      <title>Re: How Drop/Keep Rows in a Dataset Based on Sample Name Patterns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-Drop-Keep-Rows-in-a-Dataset-Based-on-Sample-Name-Patterns/m-p/778161#M31495</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/405243"&gt;@SAS49&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n0bj9p4401w3n9n1gmv6tf**bleep**9m.htm" target="_blank" rel="noopener"&gt;PRXMATCH&lt;/A&gt;, one of the parsing functions mentioned by &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954" target="_blank" rel="noopener"&gt;Astounding&lt;/A&gt;&amp;nbsp;-- and already suggested by &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408" target="_blank" rel="noopener"&gt;Ksharp&lt;/A&gt;, as I've just seen -- you can specify a Perl regular expression to describe the search pattern.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;/&lt;STRONG&gt;^[A-H]-\d{3}$&lt;/STRONG&gt;/&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;Explanation (see&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0s9ilagexmjl8n1u7e1t1jfnzlk.htm" target="_blank" rel="noopener"&gt;Tables of Perl Regular Expression (PRX) Metacharacters&lt;/A&gt; for the documentation)&lt;FONT face="helvetica"&gt;:&lt;/FONT&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;FONT face="helvetica"&gt;&lt;STRONG&gt;^&lt;/STRONG&gt; means "beginning of the string"&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="helvetica"&gt;&lt;STRONG&gt;[A-H]&lt;/STRONG&gt; means one of the characters in the range "A", "B", ..., "H"&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="helvetica"&gt;&lt;STRONG&gt;-&lt;/STRONG&gt; is the hyphen&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="helvetica"&gt;&lt;STRONG&gt;\d&lt;/STRONG&gt;&amp;nbsp;means "digit," i.e. one of the characters in the range "0", "1", ..., "9"&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="helvetica"&gt;&lt;STRONG&gt;\d{3}&lt;/STRONG&gt; is a shorthand for &lt;STRONG&gt;\d\d\d&lt;/STRONG&gt;, i.e., three digits in a row&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="helvetica"&gt;&lt;STRONG&gt;$&lt;/STRONG&gt; means "end of the string"&lt;/FONT&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;FONT face="helvetica"&gt;This would be a suitable pattern for strings of length 5. However, your character variable must be longer to accommodate a string like "&lt;SPAN&gt;IQP17-UM-1997," which means that shorter values would be padded with trailing blanks. So we need to insert code for "zero or more blanks" (after \d{3}) into the regular expression or, simpler, trim the trailing blanks from the string (e.g., using the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n1io938ofitwnzn18e1hzel3u9ut.htm" target="_blank" rel="noopener"&gt;TRIM function&lt;/A&gt;) before starting the search, as shown below.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create sample data for demonstration */

data have;
input sample $char13.;
cards;
a-095
A-095
A-0951
 A-044
B-40
H-123
IQP17-UM-1997
C-1.3
D-1E3
E-.99
F--28
G-+10
CF-123
;

/* Select observations matching the desired pattern */

data want;
set have;
if prxmatch('/^[A-H]-\d{3}$/', trim(sample));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="helvetica"&gt;&lt;SPAN&gt;The PRXMATCH function returns the character position where the search pattern is found in the string -- necessarily 1 in your example -- and 0 if it is not found. Hence, the returned value is a suitable condition in a subsetting IF statement. The above code results in two observations in dataset WANT, as only "A-095" and "H-123" match the Perl regular expression.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Nov 2021 12:25:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-Drop-Keep-Rows-in-a-Dataset-Based-on-Sample-Name-Patterns/m-p/778161#M31495</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-11-03T12:25:41Z</dc:date>
    </item>
  </channel>
</rss>

