<?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: Strange find behavior with scan/find in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Strange-find-behavior-with-scan-find/m-p/933947#M367300</link>
    <description>&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 27 Jun 2024 18:37:13 GMT</pubDate>
    <dc:creator>JStein</dc:creator>
    <dc:date>2024-06-27T18:37:13Z</dc:date>
    <item>
      <title>Strange find behavior with scan/find</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Strange-find-behavior-with-scan-find/m-p/933922#M367287</link>
      <description>&lt;P&gt;I have a program with a list of strings delimited by "|" that I want to look up in a string variable column. I'm getting some odd behavior that I don't understand. Below is a code example:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data birds;

	/* list of words */
	all_the_words = 'BIRD|DOG|CAT';
	length word_from_list $100.;

	/* pull the first word from all_the_words */
	do i = 1 to 1;
		word_from_list = strip(scan(all_the_words, i, '|'));
	end;

	put "word_from_list: " word_from_list;

	/* assert that word_from_list is the same */
	words_the_same = (word_from_list = "BIRD");
	
	/* text in which we look up BIRD */
	text_for_lookup = "BIRD (text here)";

	/* find results for word_from_list */
	found_it = find(text_for_lookup, word_from_list);

	/* find results for literal "BIRD" */
	found_it_2 = find(text_for_lookup, "BIRD");

run;&lt;/CODE&gt;&lt;CODE class=""&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm finding that despite &lt;STRONG&gt;words_the_same&lt;/STRONG&gt; being 1, I'm getting different results for &lt;STRONG&gt;found_it&lt;/STRONG&gt; and &lt;STRONG&gt;found_it_2.&amp;nbsp;&lt;/STRONG&gt;Attached is the output dataset from this code run. I'm curious why these give different results and how to make it so that word_from_list gets found.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jun 2024 16:35:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Strange-find-behavior-with-scan-find/m-p/933922#M367287</guid>
      <dc:creator>JStein</dc:creator>
      <dc:date>2024-06-27T16:35:02Z</dc:date>
    </item>
    <item>
      <title>Re: Strange find behavior with scan/find</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Strange-find-behavior-with-scan-find/m-p/933927#M367291</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; When I used this modification of your code, I got the same result for both finds:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_0-1719508581251.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/97932i8B2F571FEE21FC62/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_0-1719508581251.png" alt="Cynthia_sas_0-1719508581251.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;With the literal string you bounded the string to 4 characters for the comparison. However you defined WORD_FROM_LIST to have a length of 100 and so even though you don't see it in the LOG, the variable WORD_FROM_LIST is padded out to the full length and BIRD in the TEXT_FOR_LOOKUP variable is not equal to the variable WORD_FROM_LIST because of the trailing spaces.&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jun 2024 17:18:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Strange-find-behavior-with-scan-find/m-p/933927#M367291</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2024-06-27T17:18:35Z</dc:date>
    </item>
    <item>
      <title>Re: Strange find behavior with scan/find</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Strange-find-behavior-with-scan-find/m-p/933945#M367298</link>
      <description>&lt;P&gt;SAS character variables are fixed length (padded with spaces).&amp;nbsp; So make sure that you don't include the spaces in the search term.&amp;nbsp; FIND() has an option to do that for you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	found_it = find(text_for_lookup, word_from_list, 't');
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also there is no need to remove trailing spaces before assigning a value to a variable. SAS will just add them back to fill out the variable.&amp;nbsp; So there is no need to use the STRIP() function.&amp;nbsp; Instead just use LEFT() to remove leading spaces.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    word_from_list = left(scan(all_the_words, i, '|'));
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Jun 2024 18:33:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Strange-find-behavior-with-scan-find/m-p/933945#M367298</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-06-27T18:33:44Z</dc:date>
    </item>
    <item>
      <title>Re: Strange find behavior with scan/find</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Strange-find-behavior-with-scan-find/m-p/933947#M367300</link>
      <description>&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jun 2024 18:37:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Strange-find-behavior-with-scan-find/m-p/933947#M367300</guid>
      <dc:creator>JStein</dc:creator>
      <dc:date>2024-06-27T18:37:13Z</dc:date>
    </item>
  </channel>
</rss>

