<?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: FIND  an expression in a character variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/FIND-an-expression-in-a-character-variable/m-p/644018#M192305</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/325644"&gt;@Noomen&lt;/a&gt;&amp;nbsp; No worries at all. Assuming you have posted a representative sample for us, the solution seems rather simple as the following. Please see if this helps.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have;
texto="DURING UNLOADING, IT WAS FOUND THAT A TRUCK with UN3098 (HYDROGEN PEROXIDESPILLED)"; output;
texto="While in transit, product UN1258 spilled"; output; run;

data want;
 set have;
 product=prxchange('s/(.*)(UN\d{4})(.*)/$2/i', -1, texto);
run;

proc print noobs;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="l header" scope="col"&gt;texto&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;product&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;DURING UNLOADING, IT WAS FOUND THAT A TRUCK with UN3098 (HYDROGEN PEROXIDESPILLED)&lt;/TD&gt;
&lt;TD class="l data"&gt;UN3098&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;While in transit, product UN1258 spilled&lt;/TD&gt;
&lt;TD class="l data"&gt;UN1258&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 29 Apr 2020 16:30:21 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2020-04-29T16:30:21Z</dc:date>
    <item>
      <title>FIND  an expression in a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FIND-an-expression-in-a-character-variable/m-p/643407#M192011</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I want to extract (from a variable called textvar) the following expression : the two letters UN followed by 4 numbers.&amp;nbsp;This means from 'UN0001' to "UN9999".&lt;/P&gt;&lt;P&gt;I need help with the prx because I found examples are about numbers or letters but not both !&lt;/P&gt;&lt;P&gt;data want ; set have ;&lt;BR /&gt;num = prxchange('&amp;nbsp;&amp;nbsp; ', textvar);&lt;BR /&gt;newvar=num;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help.&lt;/P&gt;</description>
      <pubDate>Mon, 27 Apr 2020 19:37:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FIND-an-expression-in-a-character-variable/m-p/643407#M192011</guid>
      <dc:creator>Noomen</dc:creator>
      <dc:date>2020-04-27T19:37:45Z</dc:date>
    </item>
    <item>
      <title>Re: FIND  an expression in a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FIND-an-expression-in-a-character-variable/m-p/643409#M192012</link>
      <description>&lt;P&gt;Is it anywhere in the text, or is it always in a particular location in the text?&lt;/P&gt;</description>
      <pubDate>Mon, 27 Apr 2020 19:47:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FIND-an-expression-in-a-character-variable/m-p/643409#M192012</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-04-27T19:47:24Z</dc:date>
    </item>
    <item>
      <title>Re: FIND  an expression in a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FIND-an-expression-in-a-character-variable/m-p/643412#M192013</link>
      <description>&lt;PRE&gt;&lt;BR /&gt;data have;&lt;BR /&gt;text='UN0001';&lt;BR /&gt;output;&lt;BR /&gt;text="UN9999";&lt;BR /&gt;output;&lt;BR /&gt;run;&lt;BR /&gt;/*With UN as prefix, so hardcoded the letters UN followed by digits*/&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;num=prxchange('s/UN(\d{4})/$1/', -1, text);&lt;BR /&gt;run;&lt;BR /&gt;/*or*/&lt;BR /&gt;/*any 2 letters as prefix followed by numbers*/&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;num=prxchange('s/[a-z]{2}(\d{4})/$1/i', -1, text);&lt;BR /&gt;run;&lt;BR /&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="l header" scope="col"&gt;text&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;num&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;UN0001&lt;/TD&gt;
&lt;TD class="l data"&gt;0001&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;UN9999&lt;/TD&gt;
&lt;TD class="l data"&gt;9999&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Mon, 27 Apr 2020 20:00:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FIND-an-expression-in-a-character-variable/m-p/643412#M192013</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-04-27T20:00:18Z</dc:date>
    </item>
    <item>
      <title>Re: FIND  an expression in a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FIND-an-expression-in-a-character-variable/m-p/643422#M192018</link>
      <description>&lt;P&gt;It could be anywhere.&lt;/P&gt;</description>
      <pubDate>Mon, 27 Apr 2020 21:02:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FIND-an-expression-in-a-character-variable/m-p/643422#M192018</guid>
      <dc:creator>Noomen</dc:creator>
      <dc:date>2020-04-27T21:02:09Z</dc:date>
    </item>
    <item>
      <title>Re: FIND  an expression in a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FIND-an-expression-in-a-character-variable/m-p/644013#M192300</link>
      <description>&lt;P&gt;Hello, I apologize, I was busy so I couldn't give you feedback sooner.&lt;/P&gt;&lt;P&gt;Your solution is close but It is not exactly what I am looking for.&lt;/P&gt;&lt;P&gt;The product ID written as UNXXXX has to be copied form the variable texto to&amp;nbsp; product, as shown below.&lt;/P&gt;&lt;P&gt;Variable texto stays the same.&lt;/P&gt;&lt;P&gt;Thank you all.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA have;&lt;BR /&gt;texto="DURING UNLOADING, IT WAS FOUND THAT A TRUCK with UN3098 (HYDROGEN PEROXIDESPILLED)"; output;&lt;BR /&gt;texto="While in transit, product UN1258 spilled"; output; run;&lt;/P&gt;&lt;P&gt;DATA want ;&lt;BR /&gt;texto="DURING UNLOADING, IT WAS FOUND THAT A TRUCK with UN3098 (HYDROGEN PEROXIDE)"; product="UN3098"; output;&lt;BR /&gt;texto="While in transit, product UN1258 spilled"; product="UN1258";output; run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 16:11:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FIND-an-expression-in-a-character-variable/m-p/644013#M192300</guid>
      <dc:creator>Noomen</dc:creator>
      <dc:date>2020-04-29T16:11:55Z</dc:date>
    </item>
    <item>
      <title>Re: FIND  an expression in a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FIND-an-expression-in-a-character-variable/m-p/644018#M192305</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/325644"&gt;@Noomen&lt;/a&gt;&amp;nbsp; No worries at all. Assuming you have posted a representative sample for us, the solution seems rather simple as the following. Please see if this helps.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have;
texto="DURING UNLOADING, IT WAS FOUND THAT A TRUCK with UN3098 (HYDROGEN PEROXIDESPILLED)"; output;
texto="While in transit, product UN1258 spilled"; output; run;

data want;
 set have;
 product=prxchange('s/(.*)(UN\d{4})(.*)/$2/i', -1, texto);
run;

proc print noobs;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="l header" scope="col"&gt;texto&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;product&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;DURING UNLOADING, IT WAS FOUND THAT A TRUCK with UN3098 (HYDROGEN PEROXIDESPILLED)&lt;/TD&gt;
&lt;TD class="l data"&gt;UN3098&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;While in transit, product UN1258 spilled&lt;/TD&gt;
&lt;TD class="l data"&gt;UN1258&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 16:30:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FIND-an-expression-in-a-character-variable/m-p/644018#M192305</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-04-29T16:30:21Z</dc:date>
    </item>
  </channel>
</rss>

