<?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: Extracting the word before and after a specific index word in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extracting-the-word-before-and-after-a-specific-index-word/m-p/375708#M276514</link>
    <description>&lt;P&gt;Here is one way:&lt;/P&gt;
&lt;PRE&gt;data have;
  informat string $80.;
  input string &amp;amp;;
  cards;
now is the time this must stop
Now hear this and do it quickly
;

data want;
  set have;
  WORD_BEFORE = scan(string,findw(string,"this",' ',"e")-1," ");
  WORD_AFTER = scan(string,findw(string,"this",' ',"e")+1," ");
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 13 Jul 2017 14:40:20 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2017-07-13T14:40:20Z</dc:date>
    <item>
      <title>Extracting the word before and after a specific index word</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-the-word-before-and-after-a-specific-index-word/m-p/375698#M276512</link>
      <description>Hi 
I have a string from which i need to extract the word before and after a specific index word (trigger).
The string looks like this

word word word word word WORD_BEFORE trigger WORD_AFTER word word word word 

I can extract WORD_AFTER using the following code:

WORD_AFTER = scan(substr(string,index(string,"trigger")),2);

However, i cant seem to get the code right to extract the WORD_BEFORE, any suggestions please?
Kind regards</description>
      <pubDate>Thu, 13 Jul 2017 14:14:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-the-word-before-and-after-a-specific-index-word/m-p/375698#M276512</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2017-07-13T14:14:15Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting the word before and after a specific index word</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-the-word-before-and-after-a-specific-index-word/m-p/375702#M276513</link>
      <description>&lt;P&gt;Note this assumes only one occurence of the word, otherwise you will get the last one:&lt;/P&gt;
&lt;PRE&gt;data want;
  length before after $200;
  string="word word word word word WORD_BEFORE trigger WORD_AFTER word word word word";
  do i=1 to countw(string," ");
    if scan(string,i," ")="trigger" then do;
      before=scan(string,i-1," ");
      after=scan(string,i+1," ");
    end;
  end;
run;

&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Jul 2017 14:26:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-the-word-before-and-after-a-specific-index-word/m-p/375702#M276513</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-07-13T14:26:12Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting the word before and after a specific index word</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-the-word-before-and-after-a-specific-index-word/m-p/375708#M276514</link>
      <description>&lt;P&gt;Here is one way:&lt;/P&gt;
&lt;PRE&gt;data have;
  informat string $80.;
  input string &amp;amp;;
  cards;
now is the time this must stop
Now hear this and do it quickly
;

data want;
  set have;
  WORD_BEFORE = scan(string,findw(string,"this",' ',"e")-1," ");
  WORD_AFTER = scan(string,findw(string,"this",' ',"e")+1," ");
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jul 2017 14:40:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-the-word-before-and-after-a-specific-index-word/m-p/375708#M276514</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-07-13T14:40:20Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting the word before and after a specific index word</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-the-word-before-and-after-a-specific-index-word/m-p/375753#M276515</link>
      <description>&lt;P&gt;Another way&lt;/P&gt;
&lt;PRE&gt;data have;
  string="word word word word word WORD_BEFORE trigger WORD_AFTER word word word word";
word_before=  prxchange('s/(.+)(WORD_BEFORE)(.+)/$2/', -1, string);
word_after=  prxchange('s/(.+)(WORD_AFTER)(.+)/$2/', -1, string);

run;
&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Jul 2017 16:37:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-the-word-before-and-after-a-specific-index-word/m-p/375753#M276515</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2017-07-13T16:37:47Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting the word before and after a specific index word</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-the-word-before-and-after-a-specific-index-word/m-p/375809#M276516</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the trigger value is not present then this code would generate word_before=last-word-of-string, and word_after=first-word-of-string, with no attendent NOTEs on the log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if the trigger appears as the first word, then word_before generates a note,&amp;nbsp; Or if the word appears only as the last word, then word_after generates a note.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If these condition are to be avoided, I'd recommend a minor alteration of &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;'s response:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=ix);
  set have;
  ix=findw(string,"stop",' ','e');
  if ix&amp;gt;1              then WORD_BEFORE = scan(string,ix-1," ");
  if ix&amp;lt;countw(string) and ix^=0 then WORD_AFTER = scan(string,ix+1," ");
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Jul 2017 19:13:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-the-word-before-and-after-a-specific-index-word/m-p/375809#M276516</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-07-13T19:13:36Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting the word before and after a specific index word</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-the-word-before-and-after-a-specific-index-word/m-p/531728#M276517</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;This is great! Thank you.&lt;/P&gt;&lt;P&gt;I am wondering how you would do this iteratively. I am trying to parse the word before a trigger word, but sometimes the trigger word appears more than once. I need to be able to pull it each time the trigger word appears. The word before is different every time, but the trigger is the same.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;something like:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="display: inline !important; float: none; background-color: #eaeaea; color: #333333; font-family: monospace,monospace; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-spacing: 0px; word-wrap: normal;"&gt;&amp;nbsp;string="word word word word word WORD_BEFORE_1 trigger WORD_AFTER word word word word &lt;SPAN style="background-color: #eaeaea; color: #333333; display: inline; float: none; font-family: monospace,monospace; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; line-height: 16.8px; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-spacing: 0px; word-wrap: normal;"&gt;WORD_BEFORE_2 trigger WORD_AFTER WORD_BEFORE_3 trigger WORD_AFTER&lt;/SPAN&gt;";&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="display: inline !important; float: none; background-color: #eaeaea; color: #333333; font-family: monospace,monospace; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-spacing: 0px; word-wrap: normal;"&gt;&lt;SPAN style="background-color: #eaeaea; color: #333333; display: inline; float: none; font-family: monospace,monospace; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; line-height: 16.8px; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-spacing: 0px; word-wrap: normal;"&gt;WORD_BEFORE&lt;/SPAN&gt;_1=&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="display: inline !important; float: none; background-color: #eaeaea; color: #333333; font-family: monospace,monospace; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-spacing: 0px; word-wrap: normal;"&gt;&lt;SPAN style="background-color: #eaeaea; color: #333333; display: inline; float: none; font-family: monospace,monospace; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; line-height: 16.8px; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-spacing: 0px; word-wrap: normal;"&gt;WORD_BEFORE&lt;/SPAN&gt;_2=&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="display: inline !important; float: none; background-color: #eaeaea; color: #333333; font-family: monospace,monospace; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-spacing: 0px; word-wrap: normal;"&gt;&lt;SPAN style="background-color: #eaeaea; color: #333333; display: inline; float: none; font-family: monospace,monospace; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; line-height: 16.8px; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-spacing: 0px; word-wrap: normal;"&gt;WORD_BEFORE_3=&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="display: inline !important; float: none; background-color: #eaeaea; color: #333333; font-family: monospace,monospace; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-spacing: 0px; word-wrap: normal;"&gt;etc. &lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Jan 2019 17:32:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-the-word-before-and-after-a-specific-index-word/m-p/531728#M276517</guid>
      <dc:creator>DR_Jorg</dc:creator>
      <dc:date>2019-01-31T17:32:32Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting the word before and after a specific index word</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-the-word-before-and-after-a-specific-index-word/m-p/531904#M276518</link>
      <description>&lt;P&gt;It is a good idea to start a new topic, provided test data and required output.&amp;nbsp; This topic is closed, and over a year old.&amp;nbsp; I would suggest a simple loop, i=1 to scan(your_string), and check each word within the loop.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 07:54:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-the-word-before-and-after-a-specific-index-word/m-p/531904#M276518</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-02-01T07:54:27Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting the word before and after a specific index word</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-the-word-before-and-after-a-specific-index-word/m-p/531984#M276519</link>
      <description>&lt;P&gt;Thanks, I posted it as a new topic here:&lt;/P&gt;&lt;P&gt;&lt;A href="https://communities.sas.com/t5/New-SAS-User/Iteratively-extracting-the-word-before-a-specific-index-word/m-p/531982#M5952" target="_blank"&gt;https://communities.sas.com/t5/New-SAS-User/Iteratively-extracting-the-word-before-a-specific-index-word/m-p/531982#M5952&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 13:55:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-the-word-before-and-after-a-specific-index-word/m-p/531984#M276519</guid>
      <dc:creator>DR_Jorg</dc:creator>
      <dc:date>2019-02-01T13:55:37Z</dc:date>
    </item>
  </channel>
</rss>

