<?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 Find pattern string and save it in a new column in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Find-pattern-string-and-save-it-in-a-new-column/m-p/726403#M28121</link>
    <description>&lt;P&gt;Hi there, I'm trying to create a new column with a given pattern found in another column.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My data looks like this:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dlm="¬";
  length SERIAID $20;
  input SERIAID $;
datalines;
L1CU 
L1UL1C 
L2C 
L2CL3U 
L2CU 
L3UC 
L3CZ 
L4C
L4CL5U 
L6U
;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to add a new column called "Level" where I add the existing pattern "L", "X" ( a number") and a "C". If this pattern does not exist (for example in the last row), that row should be removed from the dataset.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
set have;
level = scan(SERIALID, 'C');
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;But I get a column with only blanks.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone knows how to do this in a fast way? I'm new to SAS so I'm not sure which SAS functions is better for stringing.&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;</description>
    <pubDate>Mon, 15 Mar 2021 14:43:45 GMT</pubDate>
    <dc:creator>ACLAN</dc:creator>
    <dc:date>2021-03-15T14:43:45Z</dc:date>
    <item>
      <title>Find pattern string and save it in a new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Find-pattern-string-and-save-it-in-a-new-column/m-p/726403#M28121</link>
      <description>&lt;P&gt;Hi there, I'm trying to create a new column with a given pattern found in another column.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My data looks like this:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dlm="¬";
  length SERIAID $20;
  input SERIAID $;
datalines;
L1CU 
L1UL1C 
L2C 
L2CL3U 
L2CU 
L3UC 
L3CZ 
L4C
L4CL5U 
L6U
;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to add a new column called "Level" where I add the existing pattern "L", "X" ( a number") and a "C". If this pattern does not exist (for example in the last row), that row should be removed from the dataset.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
set have;
level = scan(SERIALID, 'C');
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;But I get a column with only blanks.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone knows how to do this in a fast way? I'm new to SAS so I'm not sure which SAS functions is better for stringing.&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;</description>
      <pubDate>Mon, 15 Mar 2021 14:43:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Find-pattern-string-and-save-it-in-a-new-column/m-p/726403#M28121</guid>
      <dc:creator>ACLAN</dc:creator>
      <dc:date>2021-03-15T14:43:45Z</dc:date>
    </item>
    <item>
      <title>Re: Find pattern string and save it in a new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Find-pattern-string-and-save-it-in-a-new-column/m-p/726441#M28125</link>
      <description>&lt;P&gt;SCAN looks for a delimited "word" by specified count position:&amp;nbsp; Scan('this is a list of words', 4) returns the fourth word: "list".&lt;/P&gt;
&lt;P&gt;So I am pretty sure your log would show some variation of ;&lt;/P&gt;
&lt;PRE&gt;NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).
      29:13
NOTE: Invalid numeric data, 'c' , at line 29 column 13.
NOTE: Argument 2 to function SCAN('abc',.) at line 29 column 6 is invalid.
&lt;/PRE&gt;
&lt;P&gt;which tells pretty clearly that SCAN is not what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is not clear exactly what you are searching for since L and C occur multiple times in multiple positions in some of those values.&lt;/P&gt;
&lt;P&gt;You should provide examples of what you expect the result for LEVEL should be for your example data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Mar 2021 16:10:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Find-pattern-string-and-save-it-in-a-new-column/m-p/726441#M28125</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-03-15T16:10:05Z</dc:date>
    </item>
    <item>
      <title>Re: Find pattern string and save it in a new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Find-pattern-string-and-save-it-in-a-new-column/m-p/726445#M28126</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;I actually forgot to give an example for the output. I want the new column named "level" to show the pattern "LXC", where X is a number, present in&amp;nbsp;SERIAID:&amp;nbsp;&lt;/P&gt;&lt;P&gt;L1C&lt;BR /&gt;L1C&lt;BR /&gt;L2C&lt;BR /&gt;L2C&lt;BR /&gt;L2C&lt;BR /&gt;NA (not existing)&lt;BR /&gt;L3C&lt;BR /&gt;L4C&lt;BR /&gt;L4C&lt;BR /&gt;NA&amp;nbsp;(not existing)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The rows where the pattern "LXC" does not exist (NA&amp;nbsp;(not existing)) should also be removed from the dataset.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Mar 2021 16:26:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Find-pattern-string-and-save-it-in-a-new-column/m-p/726445#M28126</guid>
      <dc:creator>ACLAN</dc:creator>
      <dc:date>2021-03-15T16:26:48Z</dc:date>
    </item>
    <item>
      <title>Re: Find pattern string and save it in a new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Find-pattern-string-and-save-it-in-a-new-column/m-p/726447#M28127</link>
      <description>&lt;P&gt;Pattern matching is best done with PERL regular expressions:&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 prxMatch("/L\d+C/", SERIAID);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PGStats_0-1615825674613.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55967i8F1A2C0032AFAD10/image-size/large?v=v2&amp;amp;px=999" role="button" title="PGStats_0-1615825674613.png" alt="PGStats_0-1615825674613.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Mar 2021 16:28:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Find-pattern-string-and-save-it-in-a-new-column/m-p/726447#M28127</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-03-15T16:28:09Z</dc:date>
    </item>
    <item>
      <title>Re: Find pattern string and save it in a new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Find-pattern-string-and-save-it-in-a-new-column/m-p/726453#M28128</link>
      <description>Thanks. This code is good to identify which rows have "/L\d+C/" but how can I sub string this pattern into a new column? My goal was to get a new column with L1C, L1C, L2C, L2C, L2C, L3C...</description>
      <pubDate>Mon, 15 Mar 2021 16:50:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Find-pattern-string-and-save-it-in-a-new-column/m-p/726453#M28128</guid>
      <dc:creator>ACLAN</dc:creator>
      <dc:date>2021-03-15T16:50:45Z</dc:date>
    </item>
    <item>
      <title>Re: Find pattern string and save it in a new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Find-pattern-string-and-save-it-in-a-new-column/m-p/726497#M28131</link>
      <description>&lt;P&gt;Then do:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
if not id then id + prxParse("/L\d+C/");
set have;
call prxSubstr(id, SERIAID, pos, len);
if pos then newColumn = substr(SERIAID, pos, len);
else delete;
drop id pos len;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PGStats_0-1615832847131.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55976iA5F7D7867D123353/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PGStats_0-1615832847131.png" alt="PGStats_0-1615832847131.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Mar 2021 18:27:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Find-pattern-string-and-save-it-in-a-new-column/m-p/726497#M28131</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-03-15T18:27:34Z</dc:date>
    </item>
  </channel>
</rss>

