<?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 Return the word after and before a keyword in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Return-the-word-after-and-before-a-keyword/m-p/408144#M99544</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm looking to create a variable that contains the word directly before and directly after a keyword.&lt;/P&gt;&lt;P&gt;For example, if the keyword I would like to search for is "Apple" then&lt;/P&gt;&lt;P&gt;The apple is green and red ==&amp;gt; "the apple is"&lt;/P&gt;&lt;P&gt;I love to eat a lot of apples ==&amp;gt; "of apples"&lt;/P&gt;&lt;P&gt;I like orange juice ==&amp;gt;&amp;nbsp; ""&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I would like to apply that to multiple keywords.&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;&lt;P&gt;CF&lt;/P&gt;</description>
    <pubDate>Fri, 27 Oct 2017 19:42:12 GMT</pubDate>
    <dc:creator>camfarrell25</dc:creator>
    <dc:date>2017-10-27T19:42:12Z</dc:date>
    <item>
      <title>Return the word after and before a keyword</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Return-the-word-after-and-before-a-keyword/m-p/408144#M99544</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm looking to create a variable that contains the word directly before and directly after a keyword.&lt;/P&gt;&lt;P&gt;For example, if the keyword I would like to search for is "Apple" then&lt;/P&gt;&lt;P&gt;The apple is green and red ==&amp;gt; "the apple is"&lt;/P&gt;&lt;P&gt;I love to eat a lot of apples ==&amp;gt; "of apples"&lt;/P&gt;&lt;P&gt;I like orange juice ==&amp;gt;&amp;nbsp; ""&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I would like to apply that to multiple keywords.&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;&lt;P&gt;CF&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2017 19:42:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Return-the-word-after-and-before-a-keyword/m-p/408144#M99544</guid>
      <dc:creator>camfarrell25</dc:creator>
      <dc:date>2017-10-27T19:42:12Z</dc:date>
    </item>
    <item>
      <title>Re: Return the word after and before a keyword</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Return-the-word-after-and-before-a-keyword/m-p/408150#M99548</link>
      <description>&lt;P&gt;How many keywords are you talking about? The below works for the example you provided, but it will certainly have to be modified to address all of your data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
    input @1 text $40.;

    prxExp = prxparse('/(\w{0,})(\sapples?\s)(\w{0,})/i');
    if prxmatch(prxExp, text) &amp;gt; 0 then do;
        want_var = prxposn(prxExp, 0, text);
    end;
datalines;
The apple is green and red
I love to eat a lot of apples
I like orange juice
;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Oct 2017 19:56:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Return-the-word-after-and-before-a-keyword/m-p/408150#M99548</guid>
      <dc:creator>collinelliot</dc:creator>
      <dc:date>2017-10-27T19:56:03Z</dc:date>
    </item>
    <item>
      <title>Re: Return the word after and before a keyword</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Return-the-word-after-and-before-a-keyword/m-p/408152#M99550</link>
      <description>&lt;P&gt;I have 5 keywords.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help!!&lt;BR /&gt;CF&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2017 19:58:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Return-the-word-after-and-before-a-keyword/m-p/408152#M99550</guid>
      <dc:creator>camfarrell25</dc:creator>
      <dc:date>2017-10-27T19:58:41Z</dc:date>
    </item>
    <item>
      <title>Re: Return the word after and before a keyword</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Return-the-word-after-and-before-a-keyword/m-p/408161#M99554</link>
      <description>&lt;P&gt;Five is small enough that you could probably just add them to the example I gave you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2017 20:21:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Return-the-word-after-and-before-a-keyword/m-p/408161#M99554</guid>
      <dc:creator>collinelliot</dc:creator>
      <dc:date>2017-10-27T20:21:10Z</dc:date>
    </item>
    <item>
      <title>Re: Return the word after and before a keyword</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Return-the-word-after-and-before-a-keyword/m-p/408162#M99555</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm sure there's a more elegant way, but if this meets your needs...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;prxExp &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;prxparse&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'/(\w{0,})(\sapples?\s|\skeyword2\s|\skeyword3\s)(\w{0,})/i'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2017 20:25:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Return-the-word-after-and-before-a-keyword/m-p/408162#M99555</guid>
      <dc:creator>collinelliot</dc:creator>
      <dc:date>2017-10-27T20:25:37Z</dc:date>
    </item>
    <item>
      <title>Re: Return the word after and before a keyword</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Return-the-word-after-and-before-a-keyword/m-p/408170#M99559</link>
      <description>&lt;P&gt;Can a phrase have more than 1 keyword?&amp;nbsp; If so, what do you want to do?&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2017 20:57:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Return-the-word-after-and-before-a-keyword/m-p/408170#M99559</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-10-27T20:57:55Z</dc:date>
    </item>
    <item>
      <title>Re: Return the word after and before a keyword</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Return-the-word-after-and-before-a-keyword/m-p/408214#M99586</link>
      <description>&lt;P&gt;I would go for:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
if not prxId then 
    prxId + prxparse("/(\w+\s+)?\b(apples?|cherr(y|ies)|bananas?)\b(\s+\w+)?/io");

infile datalines truncover;
input text $100.;
length extract $100;

start = 1; stop = -1;
call prxnext(prxId, start, stop, text, pos, len);
if pos = 0 then output;
do while (pos &amp;gt; 0);
    extract = substr(text, pos, len);
    output;
    call prxnext(prxId, start, stop, text, pos, len);
    end;
keep text extract;
datalines;
The apple is green and red
I love to eat a lot of apples
The  apple is green and red
I said "Apple".
I love to eat pineapples
APPLES, ORANGES, and BANANAS
I like orange juice
;

proc print; by text notsorted; id text; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 28 Oct 2017 04:08:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Return-the-word-after-and-before-a-keyword/m-p/408214#M99586</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-10-28T04:08:37Z</dc:date>
    </item>
    <item>
      <title>Re: Return the word after and before a keyword</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Return-the-word-after-and-before-a-keyword/m-p/408334#M99641</link>
      <description>&lt;PRE&gt;

data want;
input text $40.;
do i=1 to countw(text,' ');
 temp=scan(text,i,' '); 
 if find(temp,'apple','i') then do;
   want=catx(' ',scan(text,i-1,' '),temp,scan(text,i+1,' '));
 end;
end;
drop i temp;
datalines;
The apple is green and red
I love to eat a lot of apples
I like orange juice
;

&lt;/PRE&gt;</description>
      <pubDate>Sun, 29 Oct 2017 12:23:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Return-the-word-after-and-before-a-keyword/m-p/408334#M99641</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-10-29T12:23:54Z</dc:date>
    </item>
    <item>
      <title>Re: Return the word after and before a keyword</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Return-the-word-after-and-before-a-keyword/m-p/408554#M99759</link>
      <description>&lt;P&gt;I found a variation that works well - in the case that there's a period, comma or other non-text character, it also identifies it:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;prxExp1 = prxparse('/(\w{0,})(\W{1,})(word1|word2|word3|word4|word5)(\W{1,})(\w{0,})/i');&lt;BR /&gt;if prxmatch(prxExp1, text) &amp;gt; 0 then do;&lt;BR /&gt;string&amp;nbsp;= prxposn(prxExp1, 0, &lt;SPAN&gt;text&lt;/SPAN&gt;)&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2017 11:04:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Return-the-word-after-and-before-a-keyword/m-p/408554#M99759</guid>
      <dc:creator>camfarrell25</dc:creator>
      <dc:date>2017-10-30T11:04:37Z</dc:date>
    </item>
  </channel>
</rss>

