<?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: why %index doesn't work in %do %while loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/why-index-doesn-t-work-in-do-while-loop/m-p/27734#M5129</link>
    <description>Sorry I am not so familiar with quote function. Could you tell me where should I quote?&lt;BR /&gt;
&lt;BR /&gt;
Thanks,&lt;BR /&gt;
Lu</description>
    <pubDate>Wed, 03 Nov 2010 20:46:12 GMT</pubDate>
    <dc:creator>lueryy2000</dc:creator>
    <dc:date>2010-11-03T20:46:12Z</dc:date>
    <item>
      <title>why %index doesn't work in %do %while loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-index-doesn-t-work-in-do-while-loop/m-p/27732#M5127</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
I have a dataset containing two groups of experiment data and I want to decide if the result is positive based on different criteria. Below is my data:&lt;BR /&gt;
&lt;BR /&gt;
data mydata;&lt;BR /&gt;
input var1  var2  var3  var4  MFI_var5 MFI_var6 MFI_var7;&lt;BR /&gt;
datalines;&lt;BR /&gt;
4.2 1.8  0.3 .  30 15 10  &lt;BR /&gt;
0.7 0.8 3.3 . 80 20 . &lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
var1 - var4 came from experiment1, MFI_var5 - MFI_var7 came from experiment2 (they have prefix of MFI). The object is if the value of experiment1 &amp;gt; 1.5 then it is positive, while for experiment2 it needs to be greater than 15 to be positive. And I will create indicators, whose values are 0 or 1, showing if it is positive or not. Below is my code:&lt;BR /&gt;
&lt;BR /&gt;
%MACRO mymacro(varLIST);&lt;BR /&gt;
   %local k a b;&lt;BR /&gt;
   DATA mydataout;&lt;BR /&gt;
      SET mydata;&lt;BR /&gt;
      %LET K=1;&lt;BR /&gt;
      %LET a=%SCAN(&amp;amp;varLIST,&amp;amp;K);&lt;BR /&gt;
	  %LET b=%INDEX(&amp;amp;a,'MFI_');&lt;BR /&gt;
	  %DO %WHILE (&amp;amp;a NE %STR() AND &amp;amp;b=0);&lt;BR /&gt;
         IF	&amp;amp;a GE 1.5 THEN expt1_&amp;amp;a._pos = 1;&lt;BR /&gt;
         %LET K=%EVAL(&amp;amp;K+1);&lt;BR /&gt;
         %LET a=%SCAN(&amp;amp;varLIST,&amp;amp;K);&lt;BR /&gt;
		 %LET b=%INDEX(&amp;amp;a,'MFI_');&lt;BR /&gt;
      %END;&lt;BR /&gt;
	  &lt;BR /&gt;
	  %DO %WHILE (&amp;amp;a NE %STR() AND &amp;amp;b=1);&lt;BR /&gt;
	     IF &amp;amp;a GT 15 THEN expt2_&amp;amp;a._pos = 1;&lt;BR /&gt;
		 %LET K=%EVAL(&amp;amp;K+1);&lt;BR /&gt;
         %LET a=%SCAN(&amp;amp;varLIST,&amp;amp;K);&lt;BR /&gt;
		 %LET b=%INDEX(&amp;amp;a,'MFI_');&lt;BR /&gt;
      %END;&lt;BR /&gt;
    RUN;&lt;BR /&gt;
%MEND;&lt;BR /&gt;
%mymacro(var1  var2  var3  var4  MFI_var5 MFI_var6 MFI_var7);&lt;BR /&gt;
&lt;BR /&gt;
The positive indicators are expt1_varname_pos for experiment1, and expt2_varname_pos for experiment2, here varname are: var1 - var4 and MFI_var5 - MFI_var7, I would prefer not change the varname, just dump them in the middle. Since variables from experiment2 have prefix MFI, so I used %index function to look for if 'MFI' appear in variable, it came from experiment2 and the cut off point for positive would be 15, otherwise it is 1.5. So I want to my final report would be following:&lt;BR /&gt;
&lt;BR /&gt;
var1	var2	var3	var4	MFI_var5	MFI_var6	MFI_var7	expt1_var1_pos	expt1_var2_pos	expt1_var3_pos	expt1_var4_pos	expt1_MFI_var5_pos	expt1_MFI_var6_pos	expt1_MFI_var7_pos&lt;BR /&gt;
4.2	1.8	0.3		30	15	10	1	1			1		&lt;BR /&gt;
0.7	0.8	3.3		80	20				1		1	1	&lt;BR /&gt;
&lt;BR /&gt;
However, when I ran my macro, &amp;amp;b always be 0 and sas still use cut off of 1.5 instead of 15 for experiment2. It gave the following incorrect result:&lt;BR /&gt;
var1	var2	var3	var4	MFI_var5	MFI_var6	MFI_var7	expt1_var1_pos	expt1_var2_pos	expt1_var3_pos	expt1_var4_pos	expt1_MFI_var5_pos	expt1_MFI_var6_pos	expt1_MFI_var7_pos&lt;BR /&gt;
4.2	1.8	0.3		30	15	10	1	1			1	1	1&lt;BR /&gt;
0.7	0.8	3.3		80	20				1		1	1	&lt;BR /&gt;
&lt;BR /&gt;
I have tons of such data so I prefer use macro to do this. Does anybody know why %index doesn't function here?&lt;BR /&gt;
&lt;BR /&gt;
Thanks in advance,&lt;BR /&gt;
Lu</description>
      <pubDate>Wed, 03 Nov 2010 20:12:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-index-doesn-t-work-in-do-while-loop/m-p/27732#M5127</guid>
      <dc:creator>lueryy2000</dc:creator>
      <dc:date>2010-11-03T20:12:32Z</dc:date>
    </item>
    <item>
      <title>Re: why %index doesn't work in %do %while loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-index-doesn-t-work-in-do-while-loop/m-p/27733#M5128</link>
      <description>Quote confusion!&lt;BR /&gt;
In macro processing quote marks are treated as part of the data. &lt;BR /&gt;
Use %str() function to protect  a string from being interpreted as potentially syntax&lt;BR /&gt;
 &lt;BR /&gt;
good luck&lt;BR /&gt;
peterC</description>
      <pubDate>Wed, 03 Nov 2010 20:40:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-index-doesn-t-work-in-do-while-loop/m-p/27733#M5128</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2010-11-03T20:40:26Z</dc:date>
    </item>
    <item>
      <title>Re: why %index doesn't work in %do %while loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-index-doesn-t-work-in-do-while-loop/m-p/27734#M5129</link>
      <description>Sorry I am not so familiar with quote function. Could you tell me where should I quote?&lt;BR /&gt;
&lt;BR /&gt;
Thanks,&lt;BR /&gt;
Lu</description>
      <pubDate>Wed, 03 Nov 2010 20:46:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-index-doesn-t-work-in-do-while-loop/m-p/27734#M5129</guid>
      <dc:creator>lueryy2000</dc:creator>
      <dc:date>2010-11-03T20:46:12Z</dc:date>
    </item>
    <item>
      <title>Re: why %index doesn't work in %do %while loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-index-doesn-t-work-in-do-while-loop/m-p/27735#M5130</link>
      <description>%LET b=%INDEX(&amp;amp;a,'MFI_');&lt;BR /&gt;
&lt;BR /&gt;
that is not likely to do what you want, because it is "the wrong kind of quoting". It is data step quoting which solves compiling syntax problems in a data step, but does not do what you were hoping in your macro&lt;BR /&gt;
MFI_ needs no quoting in a macro because the macro compiler has no difficulty identifying what it is - just an inactive string.&lt;BR /&gt;
Equally 'MFI_' is used by the macro compiler as a string, but I do not imagine you will ever see &amp;amp;a holding a value like 'MFI_'12345dfgh. Your macro fails because &lt;B&gt;your %index() is looking for single quotes within the value of &amp;amp;a&lt;/B&gt;&lt;BR /&gt;
Macro quoting protects macro values to ensure their contents are treated as syntax only when needed and not too early as in&lt;BR /&gt;
%if &amp;amp;state eq WA %then %...............&lt;BR /&gt;
what do you imagine is understood by this when &amp;amp;state holds  OR or NE&lt;BR /&gt;
Then macro quoting helps&lt;BR /&gt;
%if %str(&amp;amp;state) eq WA %then %...............&lt;BR /&gt;
might do what was really wanted.&lt;BR /&gt;
 &lt;BR /&gt;
Here your macro might work if those %index() calls contain no single quotes.&lt;BR /&gt;
 &lt;BR /&gt;
good luck&lt;BR /&gt;
peterC</description>
      <pubDate>Wed, 03 Nov 2010 22:02:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-index-doesn-t-work-in-do-while-loop/m-p/27735#M5130</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2010-11-03T22:02:10Z</dc:date>
    </item>
    <item>
      <title>Re: why %index doesn't work in %do %while loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-index-doesn-t-work-in-do-while-loop/m-p/27736#M5131</link>
      <description>Now I see the problem. It works well. I appreciated your explanation.&lt;BR /&gt;
&lt;BR /&gt;
Lu</description>
      <pubDate>Thu, 04 Nov 2010 12:50:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-index-doesn-t-work-in-do-while-loop/m-p/27736#M5131</guid>
      <dc:creator>lueryy2000</dc:creator>
      <dc:date>2010-11-04T12:50:45Z</dc:date>
    </item>
  </channel>
</rss>

