<?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: index in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/index/m-p/800775#M315074</link>
    <description>Yes thank you all for the clear explanation!</description>
    <pubDate>Tue, 08 Mar 2022 02:48:14 GMT</pubDate>
    <dc:creator>HeatherNewton</dc:creator>
    <dc:date>2022-03-08T02:48:14Z</dc:date>
    <item>
      <title>index</title>
      <link>https://communities.sas.com/t5/SAS-Programming/index/m-p/800606#M314994</link>
      <description>&lt;PRE&gt;options nodate nostimer ls=78 ps=60;
data _null_;
   length a b $14;
   a='ABC.DEF (X=Y)';
   b='X=Y';
   q=index(a,b);
   w=index(a,trim(b));
   put q= w=;
run;&lt;/PRE&gt;&lt;P&gt;I dont understand the answer, why is q=0 and w=10?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 10:42:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/index/m-p/800606#M314994</guid>
      <dc:creator>HeatherNewton</dc:creator>
      <dc:date>2022-03-07T10:42:36Z</dc:date>
    </item>
    <item>
      <title>Re: index</title>
      <link>https://communities.sas.com/t5/SAS-Programming/index/m-p/800607#M314995</link>
      <description>&lt;P&gt;Always keep in mind that character variables are padded to their defined length with blanks, and in the first call of INDEX, those blanks are there in variable b and would need to also be present in a (which they are not, of course).&lt;/P&gt;
&lt;P&gt;In the second call to INDEX, the TRIM function removes the trailing blanks, and the remaining string is found.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 10:54:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/index/m-p/800607#M314995</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-03-07T10:54:18Z</dc:date>
    </item>
    <item>
      <title>Re: index</title>
      <link>https://communities.sas.com/t5/SAS-Programming/index/m-p/800608#M314996</link>
      <description>&lt;P&gt;This is Example 2 from the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.2/lefunctionsref/n0vxokxhv8lr84n10nrbnzp7gnba.htm" target="_self"&gt;Index Function Documentation&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   length a b $14;
   a='ABC.DEF (X=Y)';
   b='X=Y';
   q=index(a, b);
   w=index(a, trim(b));
   put q= w=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What you want to notice here is the Length Statement, giving both a and b a length of $14. Meaning that SAS pads both a and b with trailing blanks. The code below gives the same as above without the Length Statement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   a='ABC.DEF (X=Y) ';
   b='X=Y           ';
   q=index(a, b);
   w=index(a, trim(b));
   put q= w=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This means that in index(a, b) looks for the string 'X=Y&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;' (including trailing blanks) in the string&amp;nbsp;'ABC.DEF (X=Y) '. This returns zero because it's not there. Index(a, trim(b)) looks for&amp;nbsp;'X=Y' (without trailing blanks) in the same string. This returns 10.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 10:56:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/index/m-p/800608#M314996</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-03-07T10:56:31Z</dc:date>
    </item>
    <item>
      <title>Re: index</title>
      <link>https://communities.sas.com/t5/SAS-Programming/index/m-p/800610#M314997</link>
      <description>&lt;P&gt;Variable B has length 14. It has the value X=Y plus 11 blanks. This string of X=Y followed by 11 blanks does not appear in A.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you do the TRIM then the value is X=Y without any trailing blanks, which does appear in A.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To fix this use&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;length a $14;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or even remove the LENGTH statement entirely as it is not needed in this data set.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 10:58:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/index/m-p/800610#M314997</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-03-07T10:58:11Z</dc:date>
    </item>
    <item>
      <title>Re: index</title>
      <link>https://communities.sas.com/t5/SAS-Programming/index/m-p/800617#M315001</link>
      <description>&lt;P&gt;The secret is hidden in the length statement.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1646650688335.png" style="width: 1026px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/69210iE479BBDB8CE87844/image-dimensions/1026x116?v=v2" width="1026" height="116" role="button" title="Patrick_0-1646650688335.png" alt="Patrick_0-1646650688335.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Because this length statement defines b with a length of $14 the index() function will use the fully expanded value as below (blanks are also just characters):&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_1-1646650809648.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/69211i7C37D7069601D82D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_1-1646650809648.png" alt="Patrick_1-1646650809648.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Because this is not a match the index() function returns a value of zero.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you use the trim function then the index() function will see the following (trailing blanks removed):&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_3-1646650960406.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/69213i8BB728DEC21397C6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_3-1646650960406.png" alt="Patrick_3-1646650960406.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Now it's a match and that's why the index() function returns a value of 10.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does that explain it to you?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 11:07:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/index/m-p/800617#M315001</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-03-07T11:07:00Z</dc:date>
    </item>
    <item>
      <title>Re: index</title>
      <link>https://communities.sas.com/t5/SAS-Programming/index/m-p/800775#M315074</link>
      <description>Yes thank you all for the clear explanation!</description>
      <pubDate>Tue, 08 Mar 2022 02:48:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/index/m-p/800775#M315074</guid>
      <dc:creator>HeatherNewton</dc:creator>
      <dc:date>2022-03-08T02:48:14Z</dc:date>
    </item>
    <item>
      <title>Re: index</title>
      <link>https://communities.sas.com/t5/SAS-Programming/index/m-p/800778#M315076</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/416388"&gt;@HeatherNewton&lt;/a&gt;&amp;nbsp; - If the answers fix your problem, it's good practice to update the post as answered&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Mar 2022 03:18:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/index/m-p/800778#M315076</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-03-08T03:18:44Z</dc:date>
    </item>
  </channel>
</rss>

