<?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: Macro Do Loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-Do-Loop/m-p/716474#M221436</link>
    <description>&lt;P&gt;This is NOT a macro problem. Just use an ARRAY in the data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   array sev severity_1 - severity_3 ;
   data_invalid = "N";
   do index=1 to dim(sev) while ( data_invalid = "N");
     if sev[index] = "WARNING" then data_invalid = "Y"; 
   end;
   drop index;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 03 Feb 2021 15:01:09 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-02-03T15:01:09Z</dc:date>
    <item>
      <title>Macro Do Loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Do-Loop/m-p/716464#M221431</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying do have a do loop within a macro but can't seem to get it to work&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the data I have...&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input&amp;nbsp;id&amp;nbsp;$ severity_1 $&amp;nbsp;severity_2 $&amp;nbsp;severity_3 $;
datalines;
001 WARNING N/A N/A&amp;nbsp;
002 N/A N/A N/A&amp;nbsp;
003 N/A WARNING N/A
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;what I want to do...&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
if severity_1 = "WARNING" or severity_2 = "WARNING" or severity_3 = "WARNING" then data_invalid = "Y"; else data_invalid = "N";
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;the reason I want a do loop is because there will be 100 severity columns&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;what I have so far (that does not seem to work)...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro data_invalid();
data want2;
set have;
%let i=1;
%do %while (i &amp;lt; 3);
%if severity_&amp;amp;i = "WARNING" %then data_invalid = "Y" %else data_invalid = "N"
%end;
run;
%mend;
%data_invalid();

proc freq data = want2;
table data_invalid /missing;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;the error &amp;nbsp;I get is "variable data_invalid not found"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Help?&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2021 14:43:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Do-Loop/m-p/716464#M221431</guid>
      <dc:creator>twenty7</dc:creator>
      <dc:date>2021-02-03T14:43:32Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Do Loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Do-Loop/m-p/716472#M221434</link>
      <description>&lt;P&gt;You don't need a macro but use a whichc() function&amp;nbsp; on an array:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data invalid;
 set have;
      array sx {} $ severity_: ;   /* or severity_1 - severity_3 */
     if whichc(sx(*)) = "WARNING" then data_invalid = "y"; else data_invalid = "N";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2021 15:08:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Do-Loop/m-p/716472#M221434</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-02-03T15:08:55Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Do Loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Do-Loop/m-p/716474#M221436</link>
      <description>&lt;P&gt;This is NOT a macro problem. Just use an ARRAY in the data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   array sev severity_1 - severity_3 ;
   data_invalid = "N";
   do index=1 to dim(sev) while ( data_invalid = "N");
     if sev[index] = "WARNING" then data_invalid = "Y"; 
   end;
   drop index;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Feb 2021 15:01:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Do-Loop/m-p/716474#M221436</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-03T15:01:09Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Do Loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Do-Loop/m-p/716475#M221437</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;You don't need a macro but use a whichc() function&amp;nbsp; on an array:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data invalid;
 set have;
      array sx {} severiity_: ;   /* or severity_1 - severity_3 */
     if whichc(sx(*)) = "WARNING" then data_invalid = "y"; else data_invalid = "N";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Correction on syntax of the WHICHC() function.&amp;nbsp; Also another advantage of using WHICHC() for this is you don't need to define the array.&amp;nbsp; Just use the variable list in the function call.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data invalid;
  set have;
  if whichc("WARNING", of severity_:) then data_invalid = "Y";
  else data_invalid = "N";
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Feb 2021 15:05:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Do-Loop/m-p/716475#M221437</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-03T15:05:25Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Do Loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Do-Loop/m-p/716477#M221438</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt; 's answer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Given that macro are not needed, there are several reasons your macro doesn't work :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- You use macro %if...%then...%else but should use regular if...then...else as the macro versions&lt;/P&gt;
&lt;P&gt;only make text comparisons. So the condition severity_&amp;amp;i = "WARNING"&amp;nbsp; will compare two strings&lt;/P&gt;
&lt;P&gt;(for &amp;amp;i.=2, for instance),&amp;nbsp; severity_2 (wo quotes) and "WARNING" (with quotes) and will never be true&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- You repeat the test for each value of &amp;amp;i. so that only the last test will influence the final value of invalid_data.&lt;/P&gt;
&lt;P&gt;You should generate a series of tests separated by "or".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- You forgot a semi-colon after %then data_invalid = "Y"&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, you can use a simple %do loop :&lt;/P&gt;
&lt;P&gt;%do i=1 %to 3;&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2021 15:08:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Do-Loop/m-p/716477#M221438</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2021-02-03T15:08:34Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Do Loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Do-Loop/m-p/716478#M221439</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2021 15:10:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Do-Loop/m-p/716478#M221439</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-02-03T15:10:49Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Do Loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Do-Loop/m-p/716485#M221443</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/91983"&gt;@twenty7&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively you can use the IN operator with an array:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
array s[*] sev:;
data_invalid='WARNING' in s;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that this creates a &lt;EM&gt;numeric&lt;/EM&gt;, 0-1 coded variable &lt;FONT face="courier new,courier"&gt;data_invalid&lt;/FONT&gt;, which is more convenient than a character variable in most applications. Apply a format if you need to see "N" and "Y" in your output.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2021 15:17:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Do-Loop/m-p/716485#M221443</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-02-03T15:17:37Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Do Loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Do-Loop/m-p/716523#M221462</link>
      <description>&lt;P&gt;thanks once again&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2021 16:23:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Do-Loop/m-p/716523#M221462</guid>
      <dc:creator>twenty7</dc:creator>
      <dc:date>2021-02-03T16:23:31Z</dc:date>
    </item>
  </channel>
</rss>

