<?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: keep all the words before a certain word in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/keep-all-the-words-before-a-certain-word/m-p/808330#M318737</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Sorry I have a question again. I tried to add one more delimiter "to become".&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data test;&lt;BR /&gt;set have;&lt;BR /&gt;&amp;nbsp;loc1=findw(description,'and');&lt;BR /&gt;&amp;nbsp;loc2=findw(description,'but');&lt;BR /&gt;loc3=findw(description,'to become');&lt;BR /&gt;if loc1 and loc2 then loc=min(loc1, loc2);&lt;BR /&gt;&amp;nbsp;if loc1 and loc3 then loc=min(loc1, loc3);&lt;BR /&gt;if loc2 and loc3 then loc=min(loc2, loc3);&lt;BR /&gt;else if loc1 then loc=loc1;&lt;BR /&gt;else if loc2 then loc=loc2;&lt;BR /&gt;&amp;nbsp;else if loc3 then loc=loc3;&lt;BR /&gt;if loc then description_1=substrn(description,1,loc-1);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But, it does not work anymore. loc dose not return me the smallest number possible when comparing loc1 and loc2. What should I do? Thank you.&lt;/P&gt;</description>
    <pubDate>Mon, 18 Apr 2022 15:12:07 GMT</pubDate>
    <dc:creator>Jarvin99</dc:creator>
    <dc:date>2022-04-18T15:12:07Z</dc:date>
    <item>
      <title>keep all the words before a certain word</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-all-the-words-before-a-certain-word/m-p/808306#M318730</link>
      <description>&lt;P&gt;I have long strings such as "James E Gooding will step down as Chairman &lt;STRONG&gt;and&lt;/STRONG&gt; remain as Financial Expert of the Audit Committee" and "Sushi Danooo Kronick steps down as Chairman &lt;STRONG&gt;but&lt;/STRONG&gt; remains as Member of the Investment and Finance Committee".&lt;/P&gt;&lt;P&gt;I want to keep all the words before the &lt;STRONG&gt;first&lt;/STRONG&gt; word "and" or "but".&lt;/P&gt;&lt;P&gt;So, the results I want are "James E Gooding will step down as Chairman" and "Sushi Danooo Kronick steps down as Chairman".&lt;/P&gt;&lt;P&gt;I have tried scan and substr, but for scan, it seems there is a limit for extracting words. Only parts of the strings were copied. For substr,&amp;nbsp; it does not work as the log said the word types were changed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2022 13:53:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-all-the-words-before-a-certain-word/m-p/808306#M318730</guid>
      <dc:creator>Jarvin99</dc:creator>
      <dc:date>2022-04-18T13:53:32Z</dc:date>
    </item>
    <item>
      <title>Re: keep all the words before a certain word</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-all-the-words-before-a-certain-word/m-p/808308#M318731</link>
      <description>&lt;P&gt;I wouldn't be surprised if I'm overlooking a more concise solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
var = 'James E Gooding will step down as Chairman and remain as Financial Expert of the Audit Committee'; output;
var = 'Sushi Danooo Kronick steps down as Chairman but remains as Member of the Investment and Finance Committee'; output;
;
run;

data want;
	set have;
	want = trim(substr(var, 1, prxmatch('/and|but/', var) - 1));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="maguiremq_0-1650290578482.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/70518iB30F4C86935DC8D8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="maguiremq_0-1650290578482.png" alt="maguiremq_0-1650290578482.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2022 14:04:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-all-the-words-before-a-certain-word/m-p/808308#M318731</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2022-04-18T14:04:40Z</dc:date>
    </item>
    <item>
      <title>Re: keep all the words before a certain word</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-all-the-words-before-a-certain-word/m-p/808309#M318732</link>
      <description>Hi, may I know why substr(description,1,find(description,'and','i')-1) does not work? What is the difference between find and prxmatch? Thank you so much.</description>
      <pubDate>Mon, 18 Apr 2022 14:13:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-all-the-words-before-a-certain-word/m-p/808309#M318732</guid>
      <dc:creator>Jarvin99</dc:creator>
      <dc:date>2022-04-18T14:13:58Z</dc:date>
    </item>
    <item>
      <title>Re: keep all the words before a certain word</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-all-the-words-before-a-certain-word/m-p/808311#M318734</link>
      <description>&lt;P&gt;You want FINDW() to look for a WORD.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  loc1=findw(string,'and','i');
  loc2=findw(string,'but','i');
  if loc1 and loc2 then loc=min(loc1,loc2);
  else if loc1 then loc=loc1;
  else if loc2 then loc=loc2;
  if loc then want=substrn(string,1,loc-1);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Apr 2022 14:17:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-all-the-words-before-a-certain-word/m-p/808311#M318734</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-18T14:17:13Z</dc:date>
    </item>
    <item>
      <title>Re: keep all the words before a certain word</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-all-the-words-before-a-certain-word/m-p/808314#M318735</link>
      <description>I see. Thank you for pointing out my mistake.</description>
      <pubDate>Mon, 18 Apr 2022 14:34:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-all-the-words-before-a-certain-word/m-p/808314#M318735</guid>
      <dc:creator>Jarvin99</dc:creator>
      <dc:date>2022-04-18T14:34:36Z</dc:date>
    </item>
    <item>
      <title>Re: keep all the words before a certain word</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-all-the-words-before-a-certain-word/m-p/808330#M318737</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Sorry I have a question again. I tried to add one more delimiter "to become".&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data test;&lt;BR /&gt;set have;&lt;BR /&gt;&amp;nbsp;loc1=findw(description,'and');&lt;BR /&gt;&amp;nbsp;loc2=findw(description,'but');&lt;BR /&gt;loc3=findw(description,'to become');&lt;BR /&gt;if loc1 and loc2 then loc=min(loc1, loc2);&lt;BR /&gt;&amp;nbsp;if loc1 and loc3 then loc=min(loc1, loc3);&lt;BR /&gt;if loc2 and loc3 then loc=min(loc2, loc3);&lt;BR /&gt;else if loc1 then loc=loc1;&lt;BR /&gt;else if loc2 then loc=loc2;&lt;BR /&gt;&amp;nbsp;else if loc3 then loc=loc3;&lt;BR /&gt;if loc then description_1=substrn(description,1,loc-1);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But, it does not work anymore. loc dose not return me the smallest number possible when comparing loc1 and loc2. What should I do? Thank you.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2022 15:12:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-all-the-words-before-a-certain-word/m-p/808330#M318737</guid>
      <dc:creator>Jarvin99</dc:creator>
      <dc:date>2022-04-18T15:12:07Z</dc:date>
    </item>
    <item>
      <title>Re: keep all the words before a certain word</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-all-the-words-before-a-certain-word/m-p/808335#M318740</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/415810"&gt;@Jarvin99&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi, may I know why substr(description,1,find(description,'and','i')-1) does not work? &lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;FIND will find the string even when embedded in a&amp;nbsp; word:&lt;/P&gt;
&lt;PRE&gt;data example; 
   string ="brand name information and other information";
   x = find(string,'and');
run;&lt;/PRE&gt;
&lt;P&gt;Which returns 3 for the found position in the middle of "brand".&lt;/P&gt;
&lt;P&gt;Sometimes you get away with it depending on the data but a relatively common string like "and" is in many words.&lt;/P&gt;
&lt;P&gt;FINDW will restrict to things on "word" boundaries, which can be specified by additional parameters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hint: when you say "does not work" it often is a good idea to provide a working example and we can indicate what was found instead of what you expected.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2022 15:44:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-all-the-words-before-a-certain-word/m-p/808335#M318740</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-04-18T15:44:32Z</dc:date>
    </item>
    <item>
      <title>Re: keep all the words before a certain word</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-all-the-words-before-a-certain-word/m-p/808354#M318752</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/415810"&gt;@Jarvin99&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Sorry I have a question again. I tried to add one more delimiter "to become".&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data test;&lt;BR /&gt;set have;&lt;BR /&gt;&amp;nbsp;loc1=findw(description,'and');&lt;BR /&gt;&amp;nbsp;loc2=findw(description,'but');&lt;BR /&gt;loc3=findw(description,'to become');&lt;BR /&gt;if loc1 and loc2 then loc=min(loc1, loc2);&lt;BR /&gt;&amp;nbsp;if loc1 and loc3 then loc=min(loc1, loc3);&lt;BR /&gt;if loc2 and loc3 then loc=min(loc2, loc3);&lt;BR /&gt;else if loc1 then loc=loc1;&lt;BR /&gt;else if loc2 then loc=loc2;&lt;BR /&gt;&amp;nbsp;else if loc3 then loc=loc3;&lt;BR /&gt;if loc then description_1=substrn(description,1,loc-1);&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But, it does not work anymore. loc dose not return me the smallest number possible when comparing loc1 and loc2. What should I do? Thank you.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Your IF conditions are wrong when there are three instead of two search terms.&lt;/P&gt;
&lt;P&gt;Perhaps a different algorithm would be easier to extend when there are more than two search terms.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  set have;
  loc=.;

  loc_next=findw(description,'and');
  if loc_next then loc=min(loc,loc_next);

  loc_next=findw(description,'but');
  if loc_next then loc=min(loc,loc_next);

  loc_next=findw(description,'to become');
  if loc_next then loc=min(loc,loc_next);

  if loc&amp;gt;1 then description_1=substr(description,1,loc-1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2022 17:01:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-all-the-words-before-a-certain-word/m-p/808354#M318752</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-18T17:01:38Z</dc:date>
    </item>
  </channel>
</rss>

