<?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: Like operator not behaving consistently by not always matching at beginning of value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/472510#M121165</link>
    <description>Unless anyone has a more automated way of doing it, I think I'm going to have to hardcode some values. I plan on removing IGA from the have_2 table and hardcoding the store_variation(s) into an if statement using the index function.</description>
    <pubDate>Fri, 22 Jun 2018 15:09:03 GMT</pubDate>
    <dc:creator>sas-inquirer</dc:creator>
    <dc:date>2018-06-22T15:09:03Z</dc:date>
    <item>
      <title>Like operator not behaving consistently by not always matching at beginning of value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/472258#M121064</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;I am losing it over what I think is basic straight forward code. I am joining have_1 which is a list of store names with have_2&amp;nbsp;which is a concordance between store name variations and base store names. When I run&amp;nbsp;the following code I get inconsistent results:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have_1;
infile datalines dlm = ':' truncover;
input store $CHAR50.;
datalines;
SUPER C SHAWNIGAN
 IGA
IGA
FRESON BROS. IGA
LOBLAW SUPERSTORE
SUPERSTORE LOBLAW
foodlang iga
IGA MARCHE
ROOTS BAYSHORE CENTRE
;
run;

data have_2;
infile datalines dlm = ':' truncover;
input base_store $ store_variation $;
datalines;
SUPER C:SUPER C
ROOTS:ROOTS
IGA:IGA 
LOBLAWS:LOBLAW 
;
run;


proc sql;
create table want as
select a.*, b.base_store
from have_1 a left join have_2 b
on upcase(a.store) like ('%'||b.store_variation||'%');
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Results:&lt;/P&gt;&lt;P&gt;store&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;base_store&lt;/P&gt;&lt;P&gt;SUPER C SHAWNIGAN&amp;nbsp; &amp;nbsp; &amp;nbsp;SUPER C&lt;/P&gt;&lt;P&gt;&amp;nbsp;IGA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IGA&lt;/P&gt;&lt;P&gt;IGA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; IGA&lt;/P&gt;&lt;P&gt;FRESON BROS. IGA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; IGA&lt;/P&gt;&lt;P&gt;foodlang IGA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IGA&lt;/P&gt;&lt;P&gt;SUPERSTORE LOBLAW&amp;nbsp; &amp;nbsp;LOBLAWS&lt;/P&gt;&lt;P&gt;LOBLAW SUPERSTORE&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;IGA MARCHE&lt;/P&gt;&lt;P&gt;ROOTS BAYSHORE CENTRE&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My question is, why does the code make a match consistently when the store_variation is on its own or at the end of the store value, but not when the store_variation is at the beginning. Also, why do you think it matched at the beginning for SUPER C SHAWNIGAN, but not LOBLAW SUPERSTORE, IGA MARCHE or ROOTS BAYSHORE CENTRE?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm puzzled and need to figure this out. Please help!&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jun 2018 19:17:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/472258#M121064</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2018-06-21T19:17:01Z</dc:date>
    </item>
    <item>
      <title>Re: Like operator not behaving consistently by not always matching at beginning of value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/472260#M121066</link>
      <description>&lt;P&gt;You have the wildcards at the beginning and end to allow more flexibility but nothing in the middle. If its more of a search, you can use the FIND/INDEX function instead.&amp;nbsp; Or you can try a COMPGED or some other function to do a fuzzy match.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jun 2018 19:28:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/472260#M121066</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-06-21T19:28:00Z</dc:date>
    </item>
    <item>
      <title>Re: Like operator not behaving consistently by not always matching at beginning of value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/472261#M121067</link>
      <description>&lt;P&gt;SAS variables are fixed length and padded with spaces.&amp;nbsp; You are adding a lot of spaces before the trailing % wildcard.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;upcase(a.store) like ('%'||b.store_variation||'%');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since you variable has the default length of $8 when store variation is 'IGA' then contain 'IGA&amp;nbsp; &amp;nbsp; &amp;nbsp;'.&lt;/P&gt;
&lt;P&gt;Don't include the spaces in the value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;upcase(a.store) like cats('%',b.store_variation,'%');&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Jun 2018 19:28:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/472261#M121067</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-06-21T19:28:25Z</dc:date>
    </item>
    <item>
      <title>Re: Like operator not behaving consistently by not always matching at beginning of value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/472265#M121069</link>
      <description>Hi Tom,&lt;BR /&gt;I like your solution. It certainly took care of the matching at beginning issue. Now assuming I had put a space before and after IGA in have_2, how could I make sure that SUPER C SHAWNIGAN only has one result SUPER C and not both SUPER C and IGA?</description>
      <pubDate>Thu, 21 Jun 2018 19:38:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/472265#M121069</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2018-06-21T19:38:12Z</dc:date>
    </item>
    <item>
      <title>Re: Like operator not behaving consistently by not always matching at beginning of value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/472510#M121165</link>
      <description>Unless anyone has a more automated way of doing it, I think I'm going to have to hardcode some values. I plan on removing IGA from the have_2 table and hardcoding the store_variation(s) into an if statement using the index function.</description>
      <pubDate>Fri, 22 Jun 2018 15:09:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/472510#M121165</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2018-06-22T15:09:03Z</dc:date>
    </item>
    <item>
      <title>Re: Like operator not behaving consistently by not always matching at beginning of value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/472515#M121166</link>
      <description>&lt;P&gt;For single word matches can you try FIND() or FINDW() instead?&lt;/P&gt;
&lt;P&gt;FINDW/INDEXW looks for a full word match, whereas FIND/INDEX will find embedded matches, such as in a word.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jun 2018 15:19:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/472515#M121166</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-06-22T15:19:05Z</dc:date>
    </item>
    <item>
      <title>Re: Like operator not behaving consistently by not always matching at beginning of value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/472523#M121167</link>
      <description>Hi Reeza,&lt;BR /&gt;I've tried STRIP with CONTAINS, FIND with TRIM, INDEX and FINDW but I found with each I did not get the desired result. With these, it takes care of not coding SUPER C SHAWNIGAN to IGA, but then neglects to code FRESON BROS. IGA to IGA. Thanks for your suggestion. I could use FIND/FINDW for the single word cases, but I would still need to hardcode if index(STORE," IGA " to map IGA to FRESON BROS. IGA.</description>
      <pubDate>Fri, 22 Jun 2018 15:40:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/472523#M121167</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2018-06-22T15:40:57Z</dc:date>
    </item>
    <item>
      <title>Re: Like operator not behaving consistently by not always matching at beginning of value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/472526#M121169</link>
      <description>&lt;P&gt;To me, treating the single word conditions differently would still categorize the IGA one.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unfortunately with this type of search each approach has it's pro/cons so you have to pick the best that works, or use multiple approaches.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Open Refine is not a bad tool to do this work either.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jun 2018 15:48:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/472526#M121169</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-06-22T15:48:39Z</dc:date>
    </item>
    <item>
      <title>Re: Like operator not behaving consistently by not always matching at beginning of value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/472568#M121184</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/75160"&gt;@sas-inquirer&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi Reeza,&lt;BR /&gt;I've tried STRIP with CONTAINS, FIND with TRIM, INDEX and FINDW but I found with each I did not get the desired result. With these, it takes care of not coding SUPER C SHAWNIGAN to IGA, but then neglects to code FRESON BROS. IGA to IGA. Thanks for your suggestion. I could use FIND/FINDW for the single word cases, but I would still need to hardcode if index(STORE," IGA " to map IGA to FRESON BROS. IGA.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you want to add spaces around the search term and&amp;nbsp;use the&amp;nbsp;&lt;STRONG&gt;INDEX()&lt;/STRONG&gt; function then make sure to also add spaces around the string you are searching so it will match at the ends of the string.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;index(cat(' ',string,' '),cat(' ',strip(word),' '))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You don't need to add the spaces if you use &lt;STRONG&gt;INDEXW()&lt;/STRONG&gt; instead. Another advantage of using INDEXW() is that it will also work for finding IGA in strings like 'BROS.IGA'.&amp;nbsp; You can use the optional third argument to identify what characters are used to indicate word boundaries.&amp;nbsp; The default&amp;nbsp;will&amp;nbsp;include punctuation like periods.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;indexw(string,strip(word))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It might even be easier to use &lt;STRONG&gt;FINDW()&lt;/STRONG&gt; instead.&amp;nbsp; Then you can use its modifiers to have it do case insensitive search and strip the blanks for you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;findw(string,word,,'spit')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jun 2018 18:59:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/472568#M121184</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-06-22T18:59:41Z</dc:date>
    </item>
    <item>
      <title>Re: Like operator not behaving consistently by not always matching at beginning of value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/473745#M121604</link>
      <description>Hi Tom,&lt;BR /&gt;Fantastic solutions! I am going with indexw. It worked like a charm. Thank you so much!!</description>
      <pubDate>Wed, 27 Jun 2018 14:17:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Like-operator-not-behaving-consistently-by-not-always-matching/m-p/473745#M121604</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2018-06-27T14:17:35Z</dc:date>
    </item>
  </channel>
</rss>

