<?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 sas, perl regular expressions, variable creation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sas-perl-regular-expressions-variable-creation/m-p/120190#M259648</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am new to using perl regular expressions.&amp;nbsp; I have a variable, call it area, whose observations correspond to a 32 character string.&amp;nbsp; Within each observation there is a six digit numeric string contained in parenthesis.&amp;nbsp; I want to create a new variable, call it space, whose observations correspond to the the six digit numeric string from the original variable titled area.&amp;nbsp; Below is my first pass at coding.&amp;nbsp; With the sql approach, I am getting an error that says ' Function PRXPARSE requires at most 1 argument(s)'.&amp;nbsp;&amp;nbsp; Could someone help me out to correctly code the creation of the variable titled space?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table test as&lt;/P&gt;&lt;P&gt;select a.*, prxparse("/\(\d\d\d\d\d\d\)/" a.area) as space&lt;/P&gt;&lt;P&gt;from data1 a;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I also tried:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data test;&lt;/P&gt;&lt;P&gt;if _n_=1 then perl=prxparse("/\(\d\d\d\d\d\d\)\");&lt;/P&gt;&lt;P&gt;retain perl;&lt;/P&gt;&lt;P&gt;set data1;&lt;/P&gt;&lt;P&gt;space=prxmatch(perl,area);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 26 Jun 2013 19:46:03 GMT</pubDate>
    <dc:creator>jdub</dc:creator>
    <dc:date>2013-06-26T19:46:03Z</dc:date>
    <item>
      <title>sas, perl regular expressions, variable creation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-perl-regular-expressions-variable-creation/m-p/120190#M259648</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am new to using perl regular expressions.&amp;nbsp; I have a variable, call it area, whose observations correspond to a 32 character string.&amp;nbsp; Within each observation there is a six digit numeric string contained in parenthesis.&amp;nbsp; I want to create a new variable, call it space, whose observations correspond to the the six digit numeric string from the original variable titled area.&amp;nbsp; Below is my first pass at coding.&amp;nbsp; With the sql approach, I am getting an error that says ' Function PRXPARSE requires at most 1 argument(s)'.&amp;nbsp;&amp;nbsp; Could someone help me out to correctly code the creation of the variable titled space?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table test as&lt;/P&gt;&lt;P&gt;select a.*, prxparse("/\(\d\d\d\d\d\d\)/" a.area) as space&lt;/P&gt;&lt;P&gt;from data1 a;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I also tried:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data test;&lt;/P&gt;&lt;P&gt;if _n_=1 then perl=prxparse("/\(\d\d\d\d\d\d\)\");&lt;/P&gt;&lt;P&gt;retain perl;&lt;/P&gt;&lt;P&gt;set data1;&lt;/P&gt;&lt;P&gt;space=prxmatch(perl,area);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Jun 2013 19:46:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-perl-regular-expressions-variable-creation/m-p/120190#M259648</guid>
      <dc:creator>jdub</dc:creator>
      <dc:date>2013-06-26T19:46:03Z</dc:date>
    </item>
    <item>
      <title>Re: sas, perl regular expressions, variable creation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-perl-regular-expressions-variable-creation/m-p/120191#M259649</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Can you post sample data and sample results.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Personally I'd use the scan function instead.&lt;/P&gt;&lt;P&gt;Scan(space, 2, "()");&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Jun 2013 20:16:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-perl-regular-expressions-variable-creation/m-p/120191#M259649</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2013-06-26T20:16:14Z</dc:date>
    </item>
    <item>
      <title>Re: sas, perl regular expressions, variable creation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-perl-regular-expressions-variable-creation/m-p/120192#M259650</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This will match a number of any number of digits between parentheses within your string. If you want exclusively numbers of 6 digits, replace the + in the pattern with {6}&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data test;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;area = "abcd(123456)gdre";&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;prx = prxparse("/\((\d+)\)/o");&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;if prxmatch(prx, area) then space = prxposn(prx,1,area);&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;put (_all_) (=);&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PG&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Jun 2013 20:24:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-perl-regular-expressions-variable-creation/m-p/120192#M259650</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2013-06-26T20:24:31Z</dc:date>
    </item>
    <item>
      <title>Re: sas, perl regular expressions, variable creation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-perl-regular-expressions-variable-creation/m-p/120193#M259651</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;PRXCHANGE() makes more sense to me for this operation. &lt;/P&gt;&lt;P&gt;You can probably get a better regex from someone else, but this seems to work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;data test;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; area='there are 123456 counties';&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; space=prxchange("s/.*(\d\d\d\d\d\d).*/$1/",-1,area);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; put (_all_) (=);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Jun 2013 20:28:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-perl-regular-expressions-variable-creation/m-p/120193#M259651</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2013-06-26T20:28:31Z</dc:date>
    </item>
    <item>
      <title>Re: sas, perl regular expressions, variable creation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-perl-regular-expressions-variable-creation/m-p/120194#M259652</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;More efficient than regular expressions indeed. All depends on how &lt;EM&gt;regular&lt;/EM&gt; your data is :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data _null_;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;length space area $50;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;input area&amp;amp;;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;space = Scan(area, 2, "()");&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;put (_all_) (+2 =);&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;datalines;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;abcd(123456)gdre&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;(123456) It was there!&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;abcd(I'm not a number!) but I am (123456) &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;(Eh!) No number here!&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt; &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: terminal,monaco;"&gt;&amp;nbsp; space=123456&amp;nbsp;&amp;nbsp; area=abcd(123456)gdre&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: terminal,monaco;"&gt;&amp;nbsp; space=It was there!&amp;nbsp;&amp;nbsp; area=(123456) It was there!&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: terminal,monaco;"&gt;&amp;nbsp; space=I'm not a number!&amp;nbsp;&amp;nbsp; area=abcd(I'm not a number!) but I am (123456)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: terminal,monaco;"&gt;&amp;nbsp; space=No number here!&amp;nbsp;&amp;nbsp; area=(Eh!) No number here!&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PG&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Jun 2013 20:51:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-perl-regular-expressions-variable-creation/m-p/120194#M259652</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2013-06-26T20:51:05Z</dc:date>
    </item>
    <item>
      <title>Re: sas, perl regular expressions, variable creation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-perl-regular-expressions-variable-creation/m-p/120195#M259653</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;I agree that prxchange is a simpler operation to use in this case, however, when a pattern match is not satisfied it will return the originating input string.&amp;nbsp; Since you are looking for a number, we also may as well create the variable with the proper data type.&amp;nbsp; This help to solve the issue with prxchange returning the input string.&amp;nbsp; &lt;/SPAN&gt;&amp;nbsp; If you want to use scan then the data needs more prep to account for all the possibilities.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P&gt;&amp;nbsp; input text &amp;amp; $50. ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; * prxchange ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; prx1 = input( prxchange( 's/.*\((\d{6})\).*/$1/' , -1 , text ) , ?? best. ) ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; * prxmatch/prxposn ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if _n_ = 1 then pid = prxparse( '/\((\d{6})\)/o' ) ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; retain pid ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if prxmatch( pid , text ) then prx2 = input( prxposn( pid , 1 , text ) , ?? best. ) ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; * scan ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; scan = input( scan( compress( text , '()' , 'kd' ) , 2 , '()' , 'm' ) , ?? best. ) ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if missing(scan) then scan = input( scan( cat( ' ' , compress( text , '()' , 'kd' ) , ' ' ) , 2 , '()' ) , ?? best. ) ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if ^missing(scan) then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if scan ne mod( scan , 10**6 ) then scan = . ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; put (_all_) (/=) ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; cards;&lt;/P&gt;&lt;P&gt;abcd(123456)gdre&lt;/P&gt;&lt;P&gt;(123456) It was there!&lt;/P&gt;&lt;P&gt;abcd(I'm not a number!) but I am (123456)&lt;/P&gt;&lt;P&gt;(Eh!) No number here!&lt;/P&gt;&lt;P&gt;7 numbers (1234567) there...&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;text=abcd(123456)gdre&lt;/P&gt;&lt;P&gt;prx1=123456&lt;/P&gt;&lt;P&gt;pid=1&lt;/P&gt;&lt;P&gt;prx2=123456&lt;/P&gt;&lt;P&gt;scan=123456&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;text=(123456) It was there!&lt;/P&gt;&lt;P&gt;prx1=123456&lt;/P&gt;&lt;P&gt;pid=1&lt;/P&gt;&lt;P&gt;prx2=123456&lt;/P&gt;&lt;P&gt;scan=123456&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;text=abcd(I'm not a number!) but I am (123456)&lt;/P&gt;&lt;P&gt;prx1=123456&lt;/P&gt;&lt;P&gt;pid=1&lt;/P&gt;&lt;P&gt;prx2=123456&lt;/P&gt;&lt;P&gt;scan=123456&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;text=(Eh!) No number here!&lt;/P&gt;&lt;P&gt;prx1=.&lt;/P&gt;&lt;P&gt;pid=1&lt;/P&gt;&lt;P&gt;prx2=.&lt;/P&gt;&lt;P&gt;scan=.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;text=7 numbers (1234567) there...&lt;/P&gt;&lt;P&gt;prx1=.&lt;/P&gt;&lt;P&gt;pid=1&lt;/P&gt;&lt;P&gt;prx2=.&lt;/P&gt;&lt;P&gt;scan=.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 27 Jun 2013 14:21:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-perl-regular-expressions-variable-creation/m-p/120195#M259653</guid>
      <dc:creator>FriedEgg</dc:creator>
      <dc:date>2013-06-27T14:21:07Z</dc:date>
    </item>
  </channel>
</rss>

