<?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: Extract information between strings in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321279#M70912</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/120811"&gt;@may0423&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Perl Regular Expressions offer a powerfull syntax when it comes to searching for patterns in a string.&lt;/P&gt;
&lt;P&gt;It needs a bit of skill-up time before you can make use of it - but it's very worthwhile learning.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input locat $50.;
	datalines;
&amp;lt;dt&amp;gt;location&amp;lt;/dt&amp;gt;&amp;lt;dd&amp;gt;Mapple grove,MN&amp;lt;/dd&amp;gt; 
&amp;lt;dt&amp;gt;location&amp;lt;/dt&amp;gt;&amp;lt;dd&amp;gt;New york,NY&amp;lt;/dd&amp;gt; 
&amp;lt;dt&amp;gt;location&amp;lt;/dt&amp;gt;&amp;lt;dd&amp;gt;New york,NY&amp;lt;/dd&amp;gt; 
&amp;lt;dt&amp;gt;location&amp;lt;/dt&amp;gt;&amp;lt;dd&amp;gt;Austin, TX&amp;lt;/dd&amp;gt; 
;
run;

data want(drop=_:);
  set have;
  length address $40;
  retain _prxid;
  if _n_=1 then _prxid=prxparse('/&amp;lt;dt&amp;gt;location&amp;lt;\/dt&amp;gt;&amp;lt;dd&amp;gt;(.*)&amp;lt;\/dd&amp;gt;/oi');
  if prxmatch(_prxid,locat) then 
    address=prxposn(_prxid, 1, locat);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lefunctionsref/69762/HTML/default/viewer.htm#n1lru1b4uoogqvn1ig446q4c6muu.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lefunctionsref/69762/HTML/default/viewer.htm#n1lru1b4uoogqvn1ig446q4c6muu.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lefunctionsref/63354/HTML/default/viewer.htm#p0s9ilagexmjl8n1u7e1t1jfnzlk.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lefunctionsref/63354/HTML/default/viewer.htm#p0s9ilagexmjl8n1u7e1t1jfnzlk.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 27 Dec 2016 10:41:49 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2016-12-27T10:41:49Z</dc:date>
    <item>
      <title>Extract information between strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321238#M70897</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have no idea how to extract information between strings.... for example I need the information&amp;nbsp;&lt;SPAN&gt;Maple Grove, MN between&lt;/SPAN&gt; &amp;lt;dt&amp;gt;Location&amp;lt;/dt&amp;gt; and &amp;lt;/dd&amp;gt;. (original info: &amp;lt;dt&amp;gt;Location&amp;lt;/dt&amp;gt; &amp;lt;dd&amp;gt;Maple Grove, MN&amp;lt;/dd&amp;gt;)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 27 Dec 2016 04:42:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321238#M70897</guid>
      <dc:creator>may0423</dc:creator>
      <dc:date>2016-12-27T04:42:11Z</dc:date>
    </item>
    <item>
      <title>Re: Extract information between strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321242#M70900</link>
      <description>&lt;P&gt;You gave sample data as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;&lt;SPAN&gt;&amp;lt;dt&amp;gt;Location&amp;lt;/dt&amp;gt; &amp;lt;dd&amp;gt;Maple Grove, MN&amp;lt;/dd&amp;gt;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;assuming your data is in format:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;STRONG&gt;&amp;lt;dt&lt;/STRONG&gt;&amp;gt;xxxxx&lt;STRONG&gt;&amp;lt;/dt&amp;gt;&amp;nbsp;&amp;lt;dd&amp;gt;&lt;/STRONG&gt;yyyyyyyyy&lt;STRONG&gt;&amp;lt;/dd&amp;gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;then you can extract your info by:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; pos1 = index(string, '&amp;lt;dd&amp;gt;') + 4;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; pos2 =&amp;nbsp;&lt;SPAN&gt;index(string, '&amp;lt;/dd&amp;gt;');&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; info = substr(string,pos1, pos2-pos1 +1);&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Dec 2016 05:24:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321242#M70900</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-12-27T05:24:42Z</dc:date>
    </item>
    <item>
      <title>Re: Extract information between strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321246#M70903</link>
      <description>&lt;P&gt;thank you &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384" target="_self"&gt;&lt;SPAN class="login-bold"&gt;Shmuel&lt;/SPAN&gt;&lt;/A&gt;&amp;nbsp;for the immediate response! but the location format is not fixed. here are&amp;nbsp;other examples:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;lt;dt&amp;gt;Location&amp;lt;/dt&amp;gt; &amp;lt;dd&amp;gt;Austin, TX&amp;lt;/dd&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;lt;dt&amp;gt;Location&amp;lt;/dt&amp;gt; &amp;lt;dd&amp;gt;MD&amp;lt;/dd&amp;gt;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Dec 2016 05:55:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321246#M70903</guid>
      <dc:creator>may0423</dc:creator>
      <dc:date>2016-12-27T05:55:44Z</dc:date>
    </item>
    <item>
      <title>Re: Extract information between strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321257#M70910</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/120811"&gt;@may0423&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;SPAN class="login-bold"&gt;&lt;/SPAN&gt;&lt;/a&gt;&amp;nbsp;for the immediate response! but the location format is not fixed. here are&amp;nbsp;other examples:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;lt;dt&amp;gt;Location&amp;lt;/dt&amp;gt; &amp;lt;dd&amp;gt;Austin, TX&amp;lt;/dd&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;lt;dt&amp;gt;Location&amp;lt;/dt&amp;gt; &amp;lt;dd&amp;gt;MD&amp;lt;/dd&amp;gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;s code will work in both examples, it does not use any fixed positions. Just try it.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Dec 2016 08:02:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321257#M70910</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-12-27T08:02:18Z</dc:date>
    </item>
    <item>
      <title>Re: Extract information between strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321269#M70911</link>
      <description>&lt;P&gt;You could have used the SCAN function to extract the data based on delimiters, But SCAN function doesn't support 'word' delimiters. So you can try changing the delimitersor the &amp;lt;tags&amp;gt; and convert them to some symbols using tranwrd function and then can extract the information required.&lt;/P&gt;
&lt;P&gt;Try running the below code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data old;
	input locat $50.;
	datalines;
&amp;lt;dt&amp;gt;location&amp;lt;/dt&amp;gt;&amp;lt;dd&amp;gt;Mapple grove,MN&amp;lt;/dd&amp;gt; 
&amp;lt;dt&amp;gt;location&amp;lt;/dt&amp;gt;&amp;lt;dd&amp;gt;New york,NY&amp;lt;/dd&amp;gt; 
&amp;lt;dt&amp;gt;location&amp;lt;/dt&amp;gt;&amp;lt;dd&amp;gt;New york,NY&amp;lt;/dd&amp;gt; 
&amp;lt;dt&amp;gt;location&amp;lt;/dt&amp;gt;&amp;lt;dd&amp;gt;Austin, TX&amp;lt;/dd&amp;gt; 
;
run;

Data new;
	set old;
	length column1 $50. column2 $50.;
	column1=scanq(tranwrd(tranwrd(locat,'&amp;lt;dt&amp;gt;','^'),'&amp;lt;/dt&amp;gt;','^'),1,'^');
	column2=scanq(tranwrd(tranwrd(locat,'&amp;lt;dd&amp;gt;','#'),'&amp;lt;/dd&amp;gt;','#'),2,'#');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;output:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/6504iF292F3B7F71631B0/image-size/original?v=v2&amp;amp;px=-1" border="0" alt="output" title="output" /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Dec 2016 09:03:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321269#M70911</guid>
      <dc:creator>mnjtrana</dc:creator>
      <dc:date>2016-12-27T09:03:28Z</dc:date>
    </item>
    <item>
      <title>Re: Extract information between strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321279#M70912</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/120811"&gt;@may0423&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Perl Regular Expressions offer a powerfull syntax when it comes to searching for patterns in a string.&lt;/P&gt;
&lt;P&gt;It needs a bit of skill-up time before you can make use of it - but it's very worthwhile learning.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input locat $50.;
	datalines;
&amp;lt;dt&amp;gt;location&amp;lt;/dt&amp;gt;&amp;lt;dd&amp;gt;Mapple grove,MN&amp;lt;/dd&amp;gt; 
&amp;lt;dt&amp;gt;location&amp;lt;/dt&amp;gt;&amp;lt;dd&amp;gt;New york,NY&amp;lt;/dd&amp;gt; 
&amp;lt;dt&amp;gt;location&amp;lt;/dt&amp;gt;&amp;lt;dd&amp;gt;New york,NY&amp;lt;/dd&amp;gt; 
&amp;lt;dt&amp;gt;location&amp;lt;/dt&amp;gt;&amp;lt;dd&amp;gt;Austin, TX&amp;lt;/dd&amp;gt; 
;
run;

data want(drop=_:);
  set have;
  length address $40;
  retain _prxid;
  if _n_=1 then _prxid=prxparse('/&amp;lt;dt&amp;gt;location&amp;lt;\/dt&amp;gt;&amp;lt;dd&amp;gt;(.*)&amp;lt;\/dd&amp;gt;/oi');
  if prxmatch(_prxid,locat) then 
    address=prxposn(_prxid, 1, locat);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lefunctionsref/69762/HTML/default/viewer.htm#n1lru1b4uoogqvn1ig446q4c6muu.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lefunctionsref/69762/HTML/default/viewer.htm#n1lru1b4uoogqvn1ig446q4c6muu.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lefunctionsref/63354/HTML/default/viewer.htm#p0s9ilagexmjl8n1u7e1t1jfnzlk.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lefunctionsref/63354/HTML/default/viewer.htm#p0s9ilagexmjl8n1u7e1t1jfnzlk.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Dec 2016 10:41:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321279#M70912</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-12-27T10:41:49Z</dc:date>
    </item>
    <item>
      <title>Re: Extract information between strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321297#M70916</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* T100910 Extract or remove all text between two strings  */

inspired by
http://stackoverflow.com/questions/9721052/r-extract-part-of-string

and

https://goo.gl/KYV45R
https://communities.sas.com/t5/Base-SAS-Programming/Extract-information-between-strings/m-p/321238

also posted to SAS-L a couple of times&lt;BR /&gt;
HAVE
====

ROGER DEANGELIS L15 NOVEMBER 21, 1963 49 SPACKENKILL ROAD

WANT
====

ROGER DEANGELIS 49 SPACKENKILL ROAD

OR

NOVEMBER 21, 1963


DETAILS
                    REMOVE ONLY
               ---------------------
ROGER DEANGELIS L15 NOVEMBER 21, 1963 49 SPACKENKILL ROAD
               ----------------------

                     KEEP ONLY
                    ------------------
ROGER DEANGELIS L15 NOVEMBER 21, 1963 49 SPACKENKILL ROAD
                    ------------------

SOLUTIONS


 ____
|  _ \ ___ _ __ ___   _____   _____
| |_) / _ \ '_ ` _ \ / _ \ \ / / _ \
|  _ &amp;lt;  __/ | | | | | (_) \ V /  __/
|_| \_\___|_| |_| |_|\___/ \_/ \___|


data _null_;
     retain compiled_pattern;
     length cleantxt $96;
     input txt &amp;amp; $100.;
     if _n_ =1 then do;
         compiled_pattern = prxparse('s/L15(.+?)49//');
     end;
     call prxchange(compiled_pattern,-1,txt,cleantxt);
     put cleantxt;
cards4;
ROGER DEANGELIS L15 NOVEMBER 21, 1963 49 SPACKENKILL ROAD
GREGG DEANGELIS L15 NOVEMBER 18, 1963 49 PENSACOLA L ROAD
;;;;
run;quit;
 _  __
| |/ /___  ___ _ __
| ' // _ \/ _ \ '_ \
| . \  __/  __/ |_) |
|_|\_\___|\___| .__/
              |_|

data _null_;
     retain compiled_pattern;
     length cleantext $96;
     input txt &amp;amp; $100.;
     if _n_ =1 then do;
         compiled_pattern = prxparse('s/L15(.+?)49//');
     end;
     if prxmatch(compiled_pattern, txt) then cleantext = prxposn(compiled_pattern, 1, txt);
     put cleantext;
cards4;
ROGER DEANGELIS L15 NOVEMBER 21, 1963 49 SPACKENKILL ROAD
GREGG DEANGELIS L15 NOVEMBER 18, 1963 49 PENSACOLA L ROAD
;;;;
run;quit;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Dec 2016 15:26:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321297#M70916</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2016-12-27T15:26:34Z</dc:date>
    </item>
    <item>
      <title>Re: Extract information between strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321312#M70918</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384" target="_blank"&gt;@Shmuel&lt;/A&gt;'s code works!&amp;nbsp;I might type in something wrong last night while adjusting the code........&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you both &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Dec 2016 18:44:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321312#M70918</guid>
      <dc:creator>may0423</dc:creator>
      <dc:date>2016-12-27T18:44:19Z</dc:date>
    </item>
    <item>
      <title>Re: Extract information between strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321313#M70919</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am very new to SAS. Thank you for the notes! It's getting interesting&amp;nbsp;&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Dec 2016 18:48:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321313#M70919</guid>
      <dc:creator>may0423</dc:creator>
      <dc:date>2016-12-27T18:48:45Z</dc:date>
    </item>
    <item>
      <title>Re: Extract information between strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321314#M70920</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/34466"&gt;@mnjtrana&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much for the code!&lt;/P&gt;</description>
      <pubDate>Tue, 27 Dec 2016 18:50:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-information-between-strings/m-p/321314#M70920</guid>
      <dc:creator>may0423</dc:creator>
      <dc:date>2016-12-27T18:50:41Z</dc:date>
    </item>
  </channel>
</rss>

