<?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 function not working in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Index-function-not-working/m-p/909964#M358884</link>
    <description>&lt;P&gt;Thank you AVO, much appreciated !&lt;/P&gt;</description>
    <pubDate>Sun, 31 Dec 2023 01:47:47 GMT</pubDate>
    <dc:creator>rmacarthur</dc:creator>
    <dc:date>2023-12-31T01:47:47Z</dc:date>
    <item>
      <title>Index function not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Index-function-not-working/m-p/909958#M358878</link>
      <description>&lt;P&gt;Hi SAS friends,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Am trying to use the &lt;STRONG&gt;index function&lt;/STRONG&gt; to locate a string variable&lt;EM&gt; "&lt;/EM&gt;&lt;CODE class=""&gt;&lt;EM&gt;MrgMT"&lt;/EM&gt;&amp;nbsp;&lt;/CODE&gt;in a longer string variable &lt;EM&gt;"&lt;CODE class=""&gt;Merge_term "&lt;/CODE&gt;&lt;/EM&gt;, but it is not working as expected.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have checked the source datasets for hidden characters and "purged" them with a compress function (used the "KAS" option to keep only alphanumeric characters and some symbols), and viewed them in note pad looking for bad characters, but still no luck.&amp;nbsp; &amp;nbsp;I've uploaded a sample SAS file with these thee variables.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the code&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data	lcl.Index_Prob		; set	lcl.&amp;amp;FFN._TMP	;
Where MrgMT in ("ALBUTEROL", "ACAMPROSATE") 	
and	
Merge_term in ("4MG OF ALBUTEROL", "ALBUTEROL", "ACAMPROSATE", "ACAMPROSATE (CAMPRAL)");

Merge_No	=	index(Merge_term, MrgMT)	;

keep MrgMT	Merge_term Merge_No	;

run	;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here is an example of the output, with comments:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;Merge_term&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;MrgMT&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Merge_No&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Should be&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4MG OF ALBUTEROL&lt;/TD&gt;&lt;TD&gt;ALBUTEROL&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ACAMPROSATE&lt;/TD&gt;&lt;TD&gt;ACAMPROSATE&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;OK !&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ACAMPROSATE&lt;/TD&gt;&lt;TD&gt;ALBUTEROL&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;OK !&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ACAMPROSATE (CAMPRAL)&lt;/TD&gt;&lt;TD&gt;ACAMPROSATE&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ALBUTEROL&lt;/TD&gt;&lt;TD&gt;ALBUTEROL&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;OK!&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions on why INDEX function is not working?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Dec 2023 01:01:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Index-function-not-working/m-p/909958#M358878</guid>
      <dc:creator>rmacarthur</dc:creator>
      <dc:date>2023-12-31T01:01:43Z</dc:date>
    </item>
    <item>
      <title>Re: Index function not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Index-function-not-working/m-p/909959#M358879</link>
      <description>&lt;P&gt;Nothing wrong with the INDEX function.&amp;nbsp; Be sure to use the TRIM function as in:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Merge_No	=	index(Merge_term, trim(MrgMT))	;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here's an illustration:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;30   data have;
31    text='aaa bbb ccc';
32    length longsrch $4;
33    length shrtsrch $3;
34    longsrch='ccc';
35    shrtsrch='ccc';
36    ixlong=index(text,longsrch);
37    ixshrt=index(text,shrtsrch);
38    ixtrim=index(text,trim(longsrch));
39    put (_all_) (=);
40   run;

text=aaa bbb ccc longsrch=ccc shrtsrch=ccc ixlong=0 ixshrt=9 ixtrim=9
NOTE: The data set WORK.HAVE has 1 observations and 6 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Dec 2023 01:09:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Index-function-not-working/m-p/909959#M358879</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-12-31T01:09:28Z</dc:date>
    </item>
    <item>
      <title>Re: Index function not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Index-function-not-working/m-p/909960#M358880</link>
      <description>&lt;P&gt;Hello.&lt;/P&gt;
&lt;P&gt;The documentation say:&lt;/P&gt;
&lt;P&gt;INDEX(source, excerpt)&lt;/P&gt;
&lt;P&gt;Both leading and trailing spaces are considered part of the excerpt argument. To remove trailing spaces, include the TRIM function with the excerpt variable inside the INDEX function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe you can try:&lt;/P&gt;
&lt;P&gt;Merge_No = index(Merge_term, trim(MrgMT));&lt;/P&gt;</description>
      <pubDate>Sun, 31 Dec 2023 01:23:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Index-function-not-working/m-p/909960#M358880</guid>
      <dc:creator>AVO339</dc:creator>
      <dc:date>2023-12-31T01:23:25Z</dc:date>
    </item>
    <item>
      <title>Re: Index function not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Index-function-not-working/m-p/909961#M358881</link>
      <description>&lt;P&gt;SAS uses fixed length character variables.&amp;nbsp; So short values are padded with spaces to fill out the variable to its complete length.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Normal equality comparisons (like in your WHERE statement) will ignore the trailing spaces.&amp;nbsp; But the INDEX function does not.&amp;nbsp; If you ask it to look for 'FRED&amp;nbsp; &amp;nbsp; ' in will not match strings that don't have all four spaces following the D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use TRIM() to remove the trailing spaces from the string you are asking INDEX to look for.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lcl.Index_Prob;
  set lcl.&amp;amp;FFN._TMP;
  where MrgMT in ("ALBUTEROL", "ACAMPROSATE")  
    and Merge_term in ("4MG OF ALBUTEROL", "ALBUTEROL", "ACAMPROSATE", "ACAMPROSATE (CAMPRAL)")
  ;
  Merge_No = index(Merge_term, trim(MrgMT));
  keep MrgMT Merge_term Merge_No ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 31 Dec 2023 01:26:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Index-function-not-working/m-p/909961#M358881</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-12-31T01:26:46Z</dc:date>
    </item>
    <item>
      <title>Re: Index function not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Index-function-not-working/m-p/909963#M358883</link>
      <description>&lt;P&gt;Thank you very much , and especially appreciate the explanation !&lt;/P&gt;</description>
      <pubDate>Sun, 31 Dec 2023 01:47:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Index-function-not-working/m-p/909963#M358883</guid>
      <dc:creator>rmacarthur</dc:creator>
      <dc:date>2023-12-31T01:47:16Z</dc:date>
    </item>
    <item>
      <title>Re: Index function not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Index-function-not-working/m-p/909964#M358884</link>
      <description>&lt;P&gt;Thank you AVO, much appreciated !&lt;/P&gt;</description>
      <pubDate>Sun, 31 Dec 2023 01:47:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Index-function-not-working/m-p/909964#M358884</guid>
      <dc:creator>rmacarthur</dc:creator>
      <dc:date>2023-12-31T01:47:47Z</dc:date>
    </item>
    <item>
      <title>Re: Index function not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Index-function-not-working/m-p/909965#M358885</link>
      <description>&lt;P&gt;Thank you Tom, and the explanation is very helpful, much appreciated !&lt;/P&gt;</description>
      <pubDate>Sun, 31 Dec 2023 01:48:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Index-function-not-working/m-p/909965#M358885</guid>
      <dc:creator>rmacarthur</dc:creator>
      <dc:date>2023-12-31T01:48:50Z</dc:date>
    </item>
  </channel>
</rss>

