<?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 Index function inside Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Index-function-inside-Macro/m-p/801756#M315554</link>
    <description>&lt;P&gt;Why is this index not working?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Trying to search for a string inside a variable using index.&lt;/P&gt;&lt;P&gt;Index is in a datastep which is inside a macro.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%MACRO dev_macro;
	data class;
	set sashelp.class;

		%if %eval(%index(lowcase(name),'a')) &amp;gt; 0 %then %do;
			result=1;
		%end;
		%else %do;
			result=0;
		%end;


	run;
%mend dev_macro;
%dev_macro;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Please advise!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank You&lt;/P&gt;</description>
    <pubDate>Fri, 11 Mar 2022 22:47:53 GMT</pubDate>
    <dc:creator>david27</dc:creator>
    <dc:date>2022-03-11T22:47:53Z</dc:date>
    <item>
      <title>Index function inside Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Index-function-inside-Macro/m-p/801756#M315554</link>
      <description>&lt;P&gt;Why is this index not working?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Trying to search for a string inside a variable using index.&lt;/P&gt;&lt;P&gt;Index is in a datastep which is inside a macro.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%MACRO dev_macro;
	data class;
	set sashelp.class;

		%if %eval(%index(lowcase(name),'a')) &amp;gt; 0 %then %do;
			result=1;
		%end;
		%else %do;
			result=0;
		%end;


	run;
%mend dev_macro;
%dev_macro;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Please advise!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank You&lt;/P&gt;</description>
      <pubDate>Fri, 11 Mar 2022 22:47:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Index-function-inside-Macro/m-p/801756#M315554</guid>
      <dc:creator>david27</dc:creator>
      <dc:date>2022-03-11T22:47:53Z</dc:date>
    </item>
    <item>
      <title>Re: Index function inside Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Index-function-inside-Macro/m-p/801757#M315555</link>
      <description>&lt;P&gt;Macro functions do not see the values of data step variables. Also macro code is involved with writing code and resolves before the data step even executes. The code generated gets added to the data step before compiling.&lt;/P&gt;
&lt;P&gt;So maybe:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%MACRO dev_macro;
	data class;
	set sashelp.class;

		if index(lowcase(name),'a')) &amp;gt; 0 then do;
			result=1;
		end;
		else do;
			result=0;
		end;


	run;
%mend dev_macro;
%dev_macro;&lt;/PRE&gt;
&lt;P&gt;You don't really describe what the macro should do. So perhaps start with that description. As shown there is no reason to use macro coding at all.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Mar 2022 22:56:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Index-function-inside-Macro/m-p/801757#M315555</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-03-11T22:56:46Z</dc:date>
    </item>
    <item>
      <title>Re: Index function inside Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Index-function-inside-Macro/m-p/801758#M315556</link>
      <description>&lt;UL&gt;
&lt;LI&gt;%SYSFUNC() is required to use the LOWCASE function in macro code&lt;/LI&gt;
&lt;LI&gt;Not sure you need %EVAL there, and if you do, I suspect it needs to be around the comparison, not just the %index.&lt;/LI&gt;
&lt;LI&gt;Parameters provided to %INDEX() should not be quoted&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;You need non-macro logic to access the variable value of name which simplifies everything entirely.&amp;nbsp;&lt;/STRONG&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;You could simplify this further using the FIND() function but I suspect your root question is actually different.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO dev_macro;
	data class;
	set sashelp.class;

		if index(lowcase(name)), 'a') &amp;gt; 0 then do;
			result=1;
		end;
		else do;
			result=0;
		end;


	run;
%mend dev_macro;
%dev_macro;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/215179"&gt;@david27&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Why is this index not working?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Trying to search for a string inside a variable using index.&lt;/P&gt;
&lt;P&gt;Index is in a datastep which is inside a macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;%MACRO dev_macro;
	data class;
	set sashelp.class;

		%if %eval(%index(lowcase(name),'a')) &amp;gt; 0 %then %do;
			result=1;
		%end;
		%else %do;
			result=0;
		%end;


	run;
%mend dev_macro;
%dev_macro;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please advise!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank You&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Mar 2022 22:59:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Index-function-inside-Macro/m-p/801758#M315556</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-03-11T22:59:17Z</dc:date>
    </item>
    <item>
      <title>Re: Index function inside Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Index-function-inside-Macro/m-p/801770#M315564</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/215179"&gt;@david27&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Trying to search for a string inside a variable using index.&lt;/P&gt;
&lt;P&gt;Thank You&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then why didn't you use INDEX() in the code?&lt;/P&gt;
&lt;P&gt;Your current %INDEX() macro function call is searching for the&amp;nbsp;&lt;FONT face="courier new,courier"&gt;'a'&lt;/FONT&gt;&amp;nbsp;in the string &lt;FONT face="courier new,courier"&gt;lowcase(name)&lt;FONT face="arial,helvetica,sans-serif"&gt;which will return a 0 since there isn't even a one quote character in that string, let alone two of them around the letter a.&amp;nbsp; The %EVAL() macro function call will convert the 0 into a 0.&amp;nbsp; Then the implied %EVAL() will evaluate the comparison &lt;FONT face="courier new,courier"&gt;0 &amp;gt; 0&lt;/FONT&gt; which also return 0 which means FALSE so the %THEN block will be skipped and the %ELSE block will always execute.&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;So that means you requested that SAS run this data step:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class;
  set sashelp.class;
  result=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Instead of the data step you probably wanted.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class;
  set sashelp.class;
  result= ( 0 &amp;lt; index(lowcase(name),'a') );
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 12 Mar 2022 03:32:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Index-function-inside-Macro/m-p/801770#M315564</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-03-12T03:32:22Z</dc:date>
    </item>
    <item>
      <title>Re: Index function inside Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Index-function-inside-Macro/m-p/801798#M315584</link>
      <description>&lt;P&gt;Hello, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/215179"&gt;@david27&lt;/a&gt; , I agree with &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; there's no reason to use a macro here. He has shown how to program this using DATA step code only, you should do that.&lt;/P&gt;</description>
      <pubDate>Sat, 12 Mar 2022 10:37:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Index-function-inside-Macro/m-p/801798#M315584</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-03-12T10:37:14Z</dc:date>
    </item>
  </channel>
</rss>

