<?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 first number in string with more than 4 digits in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-first-number-in-string-with-more-than-4-digits/m-p/411082#M100478</link>
    <description>&lt;P&gt;Hello SAS experts..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have searched google for an answer to this question, but no luck..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to find the first number in a string that has more than 4 digits.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have this data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
string="fhwfwoeuh, 12314, fjweipfjwp 214155";output;
string="Hello, 112345 Joifhew, 12-08-2017";output;
string="12343 dfnweon 12.08.17";output;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and I want this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
firstnumber="12314";output;
firstnumber="112345";output;
firstnumber="12343";output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you in advance&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 07 Nov 2017 08:14:06 GMT</pubDate>
    <dc:creator>dCone</dc:creator>
    <dc:date>2017-11-07T08:14:06Z</dc:date>
    <item>
      <title>Find first number in string with more than 4 digits</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-number-in-string-with-more-than-4-digits/m-p/411082#M100478</link>
      <description>&lt;P&gt;Hello SAS experts..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have searched google for an answer to this question, but no luck..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to find the first number in a string that has more than 4 digits.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have this data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
string="fhwfwoeuh, 12314, fjweipfjwp 214155";output;
string="Hello, 112345 Joifhew, 12-08-2017";output;
string="12343 dfnweon 12.08.17";output;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and I want this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
firstnumber="12314";output;
firstnumber="112345";output;
firstnumber="12343";output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you in advance&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Nov 2017 08:14:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-number-in-string-with-more-than-4-digits/m-p/411082#M100478</guid>
      <dc:creator>dCone</dc:creator>
      <dc:date>2017-11-07T08:14:06Z</dc:date>
    </item>
    <item>
      <title>Re: Find first number in string with more than 4 digits</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-number-in-string-with-more-than-4-digits/m-p/411093#M100483</link>
      <description>&lt;P&gt;You can do this with a regular expression&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
string="fhwfwoeuh, 12314, fjweipfjwp 214155";output;
string="Hello, 112345 Joifhew, 12-08-2017";output;
string="12343 dfnweon 12.08.17";output;
string="978y 12343 dfnweon 12.08.17";output;
run;

data want(keep=string firstnumber);
	set have;
	if _n_=1 then prx=prxparse("/\d{5,}/");
	call prxsubstr(prx,string,start,length);
	if start &amp;gt; 0 then do;
		firstnumber=substr(string,start,length);
		output;
	end;
	retain prx;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I've added an extra record where the first occurrence is only 3 digits so it picks up the second occurrence in that string for testing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The regular expression \d{5,} looks for at least five&amp;nbsp;digits and call prxsubstr is used to extract those cases where a match is found&lt;/P&gt;</description>
      <pubDate>Tue, 07 Nov 2017 09:19:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-number-in-string-with-more-than-4-digits/m-p/411093#M100483</guid>
      <dc:creator>ChrisBrooks</dc:creator>
      <dc:date>2017-11-07T09:19:33Z</dc:date>
    </item>
    <item>
      <title>Re: Find first number in string with more than 4 digits</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-number-in-string-with-more-than-4-digits/m-p/411101#M100485</link>
      <description>&lt;P&gt;You can also do the same in pure SAS Base quite simply, by using a small trick:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  length firstnumber $20;
  firstnumber=scan(compress(string," ,","kd"),1," ,");
run;&lt;/PRE&gt;
&lt;P&gt;What I do here is to compress the string, removing all charcters except digits, space and comma (the k means keep rather than drop).&amp;nbsp; Thus I am left with a string with the numbers separated by commas or spaces.&amp;nbsp; Then I scan for the first one.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Nov 2017 09:46:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-number-in-string-with-more-than-4-digits/m-p/411101#M100485</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-11-07T09:46:44Z</dc:date>
    </item>
    <item>
      <title>Re: Find first number in string with more than 4 digits</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-number-in-string-with-more-than-4-digits/m-p/411104#M100486</link>
      <description>&lt;P&gt;Nice idea&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;&amp;nbsp;but wouldn't it give an incorrect answer for the final record in this data set?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
string="fhwfwoeuh, 12314, fjweipfjwp 214155";output;
string="Hello, 112345 Joifhew, 12-08-2017";output;
string="12343 dfnweon 12.08.17";output;
string="978y 12 dfnweon 12.08.17";output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Nov 2017 09:54:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-number-in-string-with-more-than-4-digits/m-p/411104#M100486</guid>
      <dc:creator>ChrisBrooks</dc:creator>
      <dc:date>2017-11-07T09:54:35Z</dc:date>
    </item>
    <item>
      <title>Re: Find first number in string with more than 4 digits</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-number-in-string-with-more-than-4-digits/m-p/411106#M100487</link>
      <description>&lt;P&gt;Yes, he would need to add any rule on top which is needed, in the case of the data you give:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  length firstnumber $20;
  firstnumber=scan(compress(string," ,","kd"),1," ,");
  if lengthn(firstnumber) &amp;lt; 4 then firstnumber="";
run;&lt;/PRE&gt;
&lt;P&gt;That would fix it, however if you need to take the date as well, or further conditions, then maybe scanning over each delimited word from the compress is the way to go:&lt;/P&gt;
&lt;PRE&gt;data want (drop=temp i);
  set have;
  length firstnumber temp $200;
  temp=compress(string," ,","kd");
  do i=1 to countw(temp," ,");
    if lengthn(scan(temp,1," ,")) &amp;gt; =4 and firstnumber="" then firstnumber=scan(temp,1," ,");
  end;
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Nov 2017 10:05:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-number-in-string-with-more-than-4-digits/m-p/411106#M100487</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-11-07T10:05:09Z</dc:date>
    </item>
  </channel>
</rss>

