<?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: findw  function does not find the nth word of a string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/findw-function-does-not-find-the-nth-word-of-a-string/m-p/927086#M364870</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;There is something "weird" with the t modifier. I'm not sure if I'm just missing something or if there is something not working as it should when using the t modifier with a blank separated string. ...if feels as if the t modifier also removes the blank as defined word separator.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The &lt;FONT face="courier new,courier"&gt;t&lt;/FONT&gt; modifier does indeed trim the third argument (cf.&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/FINDW-Function/td-p/171304" target="_blank"&gt;https://communities.sas.com/t5/SAS-Procedures/FINDW-Function/td-p/171304&lt;/A&gt;). The simplest workaround might be to add an "odd" second delimiter to the character list which doesn't occur anywhere else:&lt;/P&gt;
&lt;PRE&gt;findwRes = findw("&amp;amp;allowedTeams",team,' &lt;STRONG&gt;§&lt;/STRONG&gt;','eit');&lt;/PRE&gt;
&lt;P&gt;Trimming the second argument with a function is, of course, the cleaner approach.&lt;/P&gt;</description>
    <pubDate>Sun, 05 May 2024 10:53:21 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2024-05-05T10:53:21Z</dc:date>
    <item>
      <title>findw  function does not find the nth word of a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/findw-function-does-not-find-the-nth-word-of-a-string/m-p/927063#M364862</link>
      <description>&lt;P&gt;Hi all, This one is quite frustrating. I want to return the Nth word of a string which matches (NOT case-sensitive)&amp;nbsp; my 'search term'.&lt;BR /&gt;Illustrated with example code using sashelp dataset.&lt;BR /&gt;My code :&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;*Create string which will be searched;
%let allowedTeams=montreal Cleveland ATLANTA houston Pittsburgh; 

*Test dataset; 
data work.baseballTest (drop=allowedT);
  set sashelp.baseball(keep=team );
  if _n_ &amp;lt; 20;

  allowedT='montreal Cleveland ATLANTA houston Pittsburgh';

  *Various styles of findw None of which appear to work;
  findwRes1 = findw("&amp;amp;allowedTeams",team,' ','ei');  
  findwRes2= findw(allowedT,team,' ','e i');  
  findwRes3= findw("montreal Cleveland ATLANTA houston Pittsburgh",team,' ','e i');  
  findwRes4= findw(upcase("&amp;amp;allowedTeams"),upcase(team),' ','E');  
  findwRes5= findw("&amp;amp;allowedTeams",team,' ','i');  
  findwRes6= findw("&amp;amp;allowedTeams",team);

  *Indexw locates the string. But extra work is needed to get the ;
  *number of the word in the string; 
  indexwRes= indexw(upcase("&amp;amp;allowedTeams"), upcase(team));
  if indexwres &amp;gt; 0 then do;
    indexwRes1= countw(substr("&amp;amp;allowedTeams", 1, indexwRes), ' ', 'i');
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;On running this code I attempt several different ways to get the FINDW to work for me (findwRres1-6) . Looking in the resultant dataset none of them do as they are all =0.&lt;BR /&gt;I am able to use a workaround using indexw and countw (as shown) which does work correctly&amp;nbsp; but is not as slick as the FINDW solution would be.&amp;nbsp; Can anyone suggest what is going wrong with my findw attempts?&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 May 2024 02:57:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/findw-function-does-not-find-the-nth-word-of-a-string/m-p/927063#M364862</guid>
      <dc:creator>robAs</dc:creator>
      <dc:date>2024-05-05T02:57:00Z</dc:date>
    </item>
    <item>
      <title>Re: findw  function does not find the nth word of a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/findw-function-does-not-find-the-nth-word-of-a-string/m-p/927064#M364863</link>
      <description>&lt;P&gt;Clarification.. I want the NUMBER of the word in the string, not the word itself&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 05 May 2024 02:59:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/findw-function-does-not-find-the-nth-word-of-a-string/m-p/927064#M364863</guid>
      <dc:creator>robAs</dc:creator>
      <dc:date>2024-05-05T02:59:07Z</dc:date>
    </item>
    <item>
      <title>Re: findw  function does not find the nth word of a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/findw-function-does-not-find-the-nth-word-of-a-string/m-p/927067#M364866</link>
      <description>&lt;P&gt;Please very explicitly provide what you expect for at least one observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps you want this:&lt;/P&gt;
&lt;PRE&gt;data work.baseballTest (drop=allowedT);
  set sashelp.baseball(keep=team );
  if _n_ &amp;lt; 20;

  allowedT='montreal Cleveland ATLANTA houston Pittsburgh';

  *Various styles of findw None of which appear to work;
  findwRes1 = findw("&amp;amp;allowedTeams",strip(team),' ','ei');  
  findwRes2= findw(allowedT,strip(team),' ','e i');  
  findwRes3= findw("montreal Cleveland ATLANTA houston Pittsburgh",strip(team),' ','e i');  
  findwRes4= findw(upcase("&amp;amp;allowedTeams"),upcase(strip(team)),' ','E');  
  findwRes5= findw("&amp;amp;allowedTeams",strip(team),' ','i');  
  findwRes6= findw("&amp;amp;allowedTeams",strip(team));

run;&lt;/PRE&gt;
&lt;P&gt;The default behavior expands the values of the variable Team from the data set to the defined length with spaces so the "team" is not found in your target string.&lt;/P&gt;</description>
      <pubDate>Sun, 05 May 2024 04:24:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/findw-function-does-not-find-the-nth-word-of-a-string/m-p/927067#M364866</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-05-05T04:24:34Z</dc:date>
    </item>
    <item>
      <title>Re: findw  function does not find the nth word of a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/findw-function-does-not-find-the-nth-word-of-a-string/m-p/927070#M364868</link>
      <description>&lt;P&gt;The following should return what you're after:&lt;/P&gt;
&lt;PRE&gt;  findwRes1 = findw("&amp;amp;allowedTeams",strip(team),' ','ei');  &lt;/PRE&gt;
&lt;P&gt;There is something "weird" with the t modifier. I'm not sure if I'm just missing something or if there is something not working as it should when using the t modifier with a blank separated string. ...if feels as if the t modifier also removes the blank as defined word separator.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 May 2024 06:23:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/findw-function-does-not-find-the-nth-word-of-a-string/m-p/927070#M364868</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-05-05T06:23:24Z</dc:date>
    </item>
    <item>
      <title>Re: findw  function does not find the nth word of a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/findw-function-does-not-find-the-nth-word-of-a-string/m-p/927086#M364870</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;There is something "weird" with the t modifier. I'm not sure if I'm just missing something or if there is something not working as it should when using the t modifier with a blank separated string. ...if feels as if the t modifier also removes the blank as defined word separator.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The &lt;FONT face="courier new,courier"&gt;t&lt;/FONT&gt; modifier does indeed trim the third argument (cf.&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/FINDW-Function/td-p/171304" target="_blank"&gt;https://communities.sas.com/t5/SAS-Procedures/FINDW-Function/td-p/171304&lt;/A&gt;). The simplest workaround might be to add an "odd" second delimiter to the character list which doesn't occur anywhere else:&lt;/P&gt;
&lt;PRE&gt;findwRes = findw("&amp;amp;allowedTeams",team,' &lt;STRONG&gt;§&lt;/STRONG&gt;','eit');&lt;/PRE&gt;
&lt;P&gt;Trimming the second argument with a function is, of course, the cleaner approach.&lt;/P&gt;</description>
      <pubDate>Sun, 05 May 2024 10:53:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/findw-function-does-not-find-the-nth-word-of-a-string/m-p/927086#M364870</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-05-05T10:53:21Z</dc:date>
    </item>
    <item>
      <title>Re: findw  function does not find the nth word of a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/findw-function-does-not-find-the-nth-word-of-a-string/m-p/927088#M364871</link>
      <description>Thank you, yes, adding the strip( ) function does the trick in each of the above and now gives the desired results!</description>
      <pubDate>Sun, 05 May 2024 11:28:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/findw-function-does-not-find-the-nth-word-of-a-string/m-p/927088#M364871</guid>
      <dc:creator>robAs</dc:creator>
      <dc:date>2024-05-05T11:28:22Z</dc:date>
    </item>
  </channel>
</rss>

