<?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: LENGTH Statement causes INDEX Function to return an incorrect result in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/LENGTH-Statement-causes-INDEX-Function-to-return-an-incorrect/m-p/112692#M23280</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks a lot for the fast responses.&lt;/P&gt;&lt;P&gt;They have been very helpful.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 15 Aug 2013 22:33:39 GMT</pubDate>
    <dc:creator>Anon</dc:creator>
    <dc:date>2013-08-15T22:33:39Z</dc:date>
    <item>
      <title>LENGTH Statement causes INDEX Function to return an incorrect result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LENGTH-Statement-causes-INDEX-Function-to-return-an-incorrect/m-p/112689#M23277</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This seems like something that must have been dealt with before, but I can't find any information related with the problem.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Taking the example from the SAS Language Dictionary to illustrate the use of the INDEX Function, if I run the following code, everything goes fine: the value of x is set to 10.&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P&gt;&amp;nbsp; a='ABC.DEF (X=Y)';&lt;/P&gt;&lt;P&gt;&amp;nbsp; b='X=Y';&lt;/P&gt;&lt;P&gt;&amp;nbsp; x=index(a,b);&lt;/P&gt;&lt;P&gt;&amp;nbsp; put x;&lt;/P&gt;&lt;P&gt; run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, if I add as the very first statement a LENGTH statement for the variable b, assigning it a declared length of 4 (or higher), then running this code returns a value of 0 for x:&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P&gt;&amp;nbsp; length b $ 4;&lt;/P&gt;&lt;P&gt;&amp;nbsp; a='ABC.DEF (X=Y)';&lt;/P&gt;&lt;P&gt;&amp;nbsp; b='X=Y';&lt;/P&gt;&lt;P&gt;&amp;nbsp; x=index(a,b);&lt;/P&gt;&lt;P&gt;&amp;nbsp; put x;&lt;/P&gt;&lt;P&gt; run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Can anyone explain me why this happens, and how I can search for b in a if the declared length of b exceeds its actual length?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks in advance,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-iwin&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Aug 2013 22:15:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LENGTH-Statement-causes-INDEX-Function-to-return-an-incorrect/m-p/112689#M23277</guid>
      <dc:creator>Anon</dc:creator>
      <dc:date>2013-08-15T22:15:13Z</dc:date>
    </item>
    <item>
      <title>Re: LENGTH Statement causes INDEX Function to return an incorrect result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LENGTH-Statement-causes-INDEX-Function-to-return-an-incorrect/m-p/112690#M23278</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Many character operations pad variables to the max length, which appears to be the case here.&lt;/P&gt;&lt;P&gt;Use&lt;/P&gt;&lt;P&gt;x=index(a, strip(b));&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Aug 2013 22:23:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LENGTH-Statement-causes-INDEX-Function-to-return-an-incorrect/m-p/112690#M23278</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2013-08-15T22:23:52Z</dc:date>
    </item>
    <item>
      <title>Re: LENGTH Statement causes INDEX Function to return an incorrect result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LENGTH-Statement-causes-INDEX-Function-to-return-an-incorrect/m-p/112691#M23279</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt; index function apparently does not trim trailing blanks from the value of a variable used as a parameter. That behavior is probably what should be expected.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; In your first step, the data compiletr creates b with a length of 3 and there are no blanks assigned in it; so index works as expected.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When you define a length of 4 or more to b, the value used in index is right padded with blanks.&amp;nbsp; So when length = 5, the string 'X=Y&amp;nbsp; ' is what the index function sees.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can test this by adding an explicit blank in your first case.&amp;nbsp; Index will not find the string.&amp;nbsp; Or by using compress or trim functions on b within the index function in the second example.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Aug 2013 22:30:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LENGTH-Statement-causes-INDEX-Function-to-return-an-incorrect/m-p/112691#M23279</guid>
      <dc:creator>LarryWorley</dc:creator>
      <dc:date>2013-08-15T22:30:42Z</dc:date>
    </item>
    <item>
      <title>Re: LENGTH Statement causes INDEX Function to return an incorrect result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LENGTH-Statement-causes-INDEX-Function-to-return-an-incorrect/m-p/112692#M23280</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks a lot for the fast responses.&lt;/P&gt;&lt;P&gt;They have been very helpful.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Aug 2013 22:33:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LENGTH-Statement-causes-INDEX-Function-to-return-an-incorrect/m-p/112692#M23280</guid>
      <dc:creator>Anon</dc:creator>
      <dc:date>2013-08-15T22:33:39Z</dc:date>
    </item>
  </channel>
</rss>

