<?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 a column in SAS Data Science</title>
    <link>https://communities.sas.com/t5/SAS-Data-Science/Return-the-word-after-and-before-a-keyword-in-a-column/m-p/431907#M6617</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm looking to create a column that contains the word 3 words before and 3 words directly after a keyword in my target column "Apple Description"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&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 market has apples and alot&amp;nbsp;of bananas&amp;nbsp; ==&amp;gt;&amp;nbsp; column 1 -&amp;gt;&amp;gt; "The market has" .&amp;nbsp; column 2 -&amp;gt;&amp;gt; "&lt;SPAN&gt;&amp;nbsp;and&amp;nbsp;&lt;/SPAN&gt;alot&lt;SPAN&gt;&amp;nbsp;of&amp;nbsp;&lt;/SPAN&gt;"&lt;/P&gt;&lt;P&gt;I love to eat a lot of apples ==&amp;gt; "a lot of"&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 like "apple" or "apple like" or "apple fruit" altogether.&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data want;
  length before after $200;
  string="a b c d e trigger f g h i j";
  do i=1 to countw(string," ");
    if scan(string,i," ")="trigger" then do;
      before=scan(string,i-5," ");
      after=scan(string,i+5," ");
      before2=scan(string,i-4," ");
      after2=scan(string,i+4," ");
    end;
  end;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 29 Jan 2018 19:00:17 GMT</pubDate>
    <dc:creator>shivamnegi92</dc:creator>
    <dc:date>2018-01-29T19:00:17Z</dc:date>
    <item>
      <title>Return the word after and before a keyword in a column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Return-the-word-after-and-before-a-keyword-in-a-column/m-p/431907#M6617</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm looking to create a column that contains the word 3 words before and 3 words directly after a keyword in my target column "Apple Description"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&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 market has apples and alot&amp;nbsp;of bananas&amp;nbsp; ==&amp;gt;&amp;nbsp; column 1 -&amp;gt;&amp;gt; "The market has" .&amp;nbsp; column 2 -&amp;gt;&amp;gt; "&lt;SPAN&gt;&amp;nbsp;and&amp;nbsp;&lt;/SPAN&gt;alot&lt;SPAN&gt;&amp;nbsp;of&amp;nbsp;&lt;/SPAN&gt;"&lt;/P&gt;&lt;P&gt;I love to eat a lot of apples ==&amp;gt; "a lot of"&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 like "apple" or "apple like" or "apple fruit" altogether.&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data want;
  length before after $200;
  string="a b c d e trigger f g h i j";
  do i=1 to countw(string," ");
    if scan(string,i," ")="trigger" then do;
      before=scan(string,i-5," ");
      after=scan(string,i+5," ");
      before2=scan(string,i-4," ");
      after2=scan(string,i+4," ");
    end;
  end;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jan 2018 19:00:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Return-the-word-after-and-before-a-keyword-in-a-column/m-p/431907#M6617</guid>
      <dc:creator>shivamnegi92</dc:creator>
      <dc:date>2018-01-29T19:00:17Z</dc:date>
    </item>
    <item>
      <title>Re: Return the word after and before a keyword in a column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Return-the-word-after-and-before-a-keyword-in-a-column/m-p/431918#M6618</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*fun stuff*/

data want;
string='The market has apples and alot of bananas';
length before after $50;
  do i=1 to countw(string," ");
      if  scan(string,i," ")='apples' then do;
	  do n=i-3 to i-1 ;
	  before=catx(' ',before,scan(string,n));
      end;
	
	  do n=i+1 to i+3;
	  after= catx(' ',after,scan(string,n));
		end;
		end;
	end;
drop n i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Jan 2018 19:27:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Return-the-word-after-and-before-a-keyword-in-a-column/m-p/431918#M6618</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-01-29T19:27:14Z</dc:date>
    </item>
    <item>
      <title>Re: Return the word after and before a keyword in a column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Return-the-word-after-and-before-a-keyword-in-a-column/m-p/431927#M6619</link>
      <description>&lt;P&gt;That is very helping. Thanks , that partially solved my problem. Suppose, instead of a string, I have to apply it to a column, how we can go ahead with this code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;By that, I mean, suppose instead of string, we have a dataframe&amp;nbsp;with a specific column we are targetting ,&amp;nbsp;suppose -- "Apple summary". Then if you wanna find out 3 words before and after a keyword -"Apple", And create 2 columns with --3 WORDS BEFORE "apple" and 3 words after apple, then how you will go ahead.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry, I have limited understanding about SAS.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Thanks again for quick reply.&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jan 2018 19:45:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Return-the-word-after-and-before-a-keyword-in-a-column/m-p/431927#M6619</guid>
      <dc:creator>shivamnegi92</dc:creator>
      <dc:date>2018-01-29T19:45:31Z</dc:date>
    </item>
  </channel>
</rss>

