<?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 macro loops within if conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/macro-loops-within-if-conditions/m-p/56626#M12121</link>
    <description>Guys ,&lt;BR /&gt;
&lt;BR /&gt;
in the below macro i want my put statement to run 10 times if condition= a is satisfied and just once if condition = b is satisfied.&lt;BR /&gt;
&lt;BR /&gt;
My problem is that i donot wish write the put statement twice in my query&lt;BR /&gt;
&lt;BR /&gt;
%macro test(condition =);&lt;BR /&gt;
&lt;BR /&gt;
%put "this is just a test";&lt;BR /&gt;
&lt;BR /&gt;
%mend;&lt;BR /&gt;
%test(condition = a)&lt;BR /&gt;
%test(condition = b)&lt;BR /&gt;
&lt;BR /&gt;
i donot wish to do this:::::&lt;BR /&gt;
%macro test(condition =);&lt;BR /&gt;
%if "&amp;amp;condition" = "a" %then %do i=1 %to 10;&lt;BR /&gt;
%put "this is just a test";&lt;BR /&gt;
%end;&lt;BR /&gt;
&lt;BR /&gt;
%if "&amp;amp;condition" = "b" %then %do i=1 %to 1;&lt;BR /&gt;
%put "this is just a test";&lt;BR /&gt;
%end;&lt;BR /&gt;
&lt;BR /&gt;
%mend;&lt;BR /&gt;
%test(condition = a)&lt;BR /&gt;
%test(condition = b)&lt;BR /&gt;
&lt;BR /&gt;
can someone help with this..</description>
    <pubDate>Wed, 28 Jul 2010 11:42:03 GMT</pubDate>
    <dc:creator>NN</dc:creator>
    <dc:date>2010-07-28T11:42:03Z</dc:date>
    <item>
      <title>macro loops within if conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loops-within-if-conditions/m-p/56626#M12121</link>
      <description>Guys ,&lt;BR /&gt;
&lt;BR /&gt;
in the below macro i want my put statement to run 10 times if condition= a is satisfied and just once if condition = b is satisfied.&lt;BR /&gt;
&lt;BR /&gt;
My problem is that i donot wish write the put statement twice in my query&lt;BR /&gt;
&lt;BR /&gt;
%macro test(condition =);&lt;BR /&gt;
&lt;BR /&gt;
%put "this is just a test";&lt;BR /&gt;
&lt;BR /&gt;
%mend;&lt;BR /&gt;
%test(condition = a)&lt;BR /&gt;
%test(condition = b)&lt;BR /&gt;
&lt;BR /&gt;
i donot wish to do this:::::&lt;BR /&gt;
%macro test(condition =);&lt;BR /&gt;
%if "&amp;amp;condition" = "a" %then %do i=1 %to 10;&lt;BR /&gt;
%put "this is just a test";&lt;BR /&gt;
%end;&lt;BR /&gt;
&lt;BR /&gt;
%if "&amp;amp;condition" = "b" %then %do i=1 %to 1;&lt;BR /&gt;
%put "this is just a test";&lt;BR /&gt;
%end;&lt;BR /&gt;
&lt;BR /&gt;
%mend;&lt;BR /&gt;
%test(condition = a)&lt;BR /&gt;
%test(condition = b)&lt;BR /&gt;
&lt;BR /&gt;
can someone help with this..</description>
      <pubDate>Wed, 28 Jul 2010 11:42:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loops-within-if-conditions/m-p/56626#M12121</guid>
      <dc:creator>NN</dc:creator>
      <dc:date>2010-07-28T11:42:03Z</dc:date>
    </item>
    <item>
      <title>Re: macro loops within if conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loops-within-if-conditions/m-p/56627#M12122</link>
      <description>Use another SAS macro to code (once) your repeat-logic, then invoke that macro from within your two %DO loops.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Wed, 28 Jul 2010 14:19:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loops-within-if-conditions/m-p/56627#M12122</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-07-28T14:19:18Z</dc:date>
    </item>
    <item>
      <title>Re: macro loops within if conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loops-within-if-conditions/m-p/56628#M12123</link>
      <description>Set a count variable to 10 if condition=a, and to 1 if condition=b. Then use the variable in the %do statement.&lt;BR /&gt;
Here is one possible solution:&lt;BR /&gt;
&lt;BR /&gt;
%macro test(condition =);&lt;BR /&gt;
%if "&amp;amp;condition"="a" %then %let count=10;&lt;BR /&gt;
%else %if "&amp;amp;condition" = "b" %then %let count=1;&lt;BR /&gt;
&lt;BR /&gt;
%do i=1 %to &amp;amp;count;&lt;BR /&gt;
 %put "this is just a test";&lt;BR /&gt;
%end;&lt;BR /&gt;
%mend;</description>
      <pubDate>Wed, 28 Jul 2010 14:19:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loops-within-if-conditions/m-p/56628#M12123</guid>
      <dc:creator>martha_sas</dc:creator>
      <dc:date>2010-07-28T14:19:49Z</dc:date>
    </item>
    <item>
      <title>Re: macro loops within if conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loops-within-if-conditions/m-p/56629#M12124</link>
      <description>Hi:&lt;BR /&gt;
  Instead of hard-coding the 10 and the 1, you can test the value of &amp;amp;CONDITION and create a new macro variable (perhaps, &amp;amp;STOP) which you could then use in your %DO loop. Something like what's shown in the log below. &lt;BR /&gt;
 &lt;BR /&gt;
  Since you said that this is just a test macro program, it's not clear what the final macro program will be. However, do note that there is a HUGE difference between a macro %IF and a DATA step IF statement -- and if you eventually want to generate DATA step code with your macro program, that difference will become very relevant to your coding and debugging efforts. Also note that there is a difference between a macro %PUT statement (which does not require the text strings to be quoted) and a DATA step PUT statement (which does require text strings and character constants to be quoted).  Reading up on the SAS Macro Facility will be very helpful to your coding efforts.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia&lt;BR /&gt;
[pre]&lt;BR /&gt;
384  %macro test(condition =);&lt;BR /&gt;
385&lt;BR /&gt;
386    %put *********** STARTING Macro;&lt;BR /&gt;
387&lt;BR /&gt;
388    ** create STOP macro variable value;&lt;BR /&gt;
389    %if &amp;amp;condition = a %then %do; %let stop = 10; %end;&lt;BR /&gt;
390    %else %if &amp;amp;condition = b %then %do; %let stop = 1; %end;&lt;BR /&gt;
391    %else %do;&lt;BR /&gt;
392        %put INVALID Condition value: &amp;amp;condition;&lt;BR /&gt;
393        %let stop = 0;&lt;BR /&gt;
394    %end;&lt;BR /&gt;
395&lt;BR /&gt;
396    %put condition = &amp;amp;condition  and  stop = &amp;amp;stop;&lt;BR /&gt;
397&lt;BR /&gt;
398    %do i = 1 %to &amp;amp;stop;&lt;BR /&gt;
399        %put i=&amp;amp;i stop=&amp;amp;stop "this is just a test";&lt;BR /&gt;
400    %end;&lt;BR /&gt;
401    %put ********** End of DO Loop;&lt;BR /&gt;
402    %put **********;&lt;BR /&gt;
403  %mend test;&lt;BR /&gt;
404  %test(condition = a)&lt;BR /&gt;
*********** STARTING Macro&lt;BR /&gt;
condition = a  and  stop = 10&lt;BR /&gt;
i=1 stop=10 "this is just a test"&lt;BR /&gt;
i=2 stop=10 "this is just a test"&lt;BR /&gt;
i=3 stop=10 "this is just a test"&lt;BR /&gt;
i=4 stop=10 "this is just a test"&lt;BR /&gt;
i=5 stop=10 "this is just a test"&lt;BR /&gt;
i=6 stop=10 "this is just a test"&lt;BR /&gt;
i=7 stop=10 "this is just a test"&lt;BR /&gt;
i=8 stop=10 "this is just a test"&lt;BR /&gt;
i=9 stop=10 "this is just a test"&lt;BR /&gt;
i=10 stop=10 "this is just a test"&lt;BR /&gt;
********** End of DO Loop&lt;BR /&gt;
**********&lt;BR /&gt;
405  %test(condition = b)&lt;BR /&gt;
*********** STARTING Macro&lt;BR /&gt;
condition = b  and  stop = 1&lt;BR /&gt;
i=1 stop=1 "this is just a test"&lt;BR /&gt;
********** End of DO Loop&lt;BR /&gt;
**********&lt;BR /&gt;
406  %test(condition = c)&lt;BR /&gt;
*********** STARTING Macro&lt;BR /&gt;
INVALID Condition value: c&lt;BR /&gt;
condition = c  and  stop = 0&lt;BR /&gt;
********** End of DO Loop&lt;BR /&gt;
**********&lt;BR /&gt;
        &lt;BR /&gt;
[/pre]</description>
      <pubDate>Wed, 28 Jul 2010 14:31:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loops-within-if-conditions/m-p/56629#M12124</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-07-28T14:31:36Z</dc:date>
    </item>
    <item>
      <title>Re: macro loops within if conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loops-within-if-conditions/m-p/56630#M12125</link>
      <description>Scott,&lt;BR /&gt;
Thanks for your reply. I was trying to find out if it was possible to achieve this without having to create another Macro. Which both Martha and Cynthia have shown to be quite simple. &lt;BR /&gt;
Why didn't i think of this.&lt;BR /&gt;
&lt;BR /&gt;
Martha / Cynthia ,&lt;BR /&gt;
Thanks a lot.</description>
      <pubDate>Wed, 28 Jul 2010 15:38:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loops-within-if-conditions/m-p/56630#M12125</guid>
      <dc:creator>NN</dc:creator>
      <dc:date>2010-07-28T15:38:56Z</dc:date>
    </item>
    <item>
      <title>Re: macro loops within if conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loops-within-if-conditions/m-p/56631#M12126</link>
      <description>&amp;gt; Guys ,&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; in the below macro i want my put statement to run 10&lt;BR /&gt;
&amp;gt; times if condition= a is satisfied and just once if&lt;BR /&gt;
&amp;gt; condition = b is satisfied.&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; My problem is that i donot wish write the put&lt;BR /&gt;
&amp;gt; statement twice in my query&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; %macro test(condition =);&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; %put "this is just a test";&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; %mend;&lt;BR /&gt;
&amp;gt; %test(condition = a)&lt;BR /&gt;
&amp;gt; %test(condition = b)&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; i donot wish to do this:::::&lt;BR /&gt;
&amp;gt; %macro test(condition =);&lt;BR /&gt;
&amp;gt; %if "&amp;amp;condition" = "a" %then %do i=1 %to 10;&lt;BR /&gt;
&lt;BR /&gt;
Did you consider this approach&lt;BR /&gt;
* preliminary code executed before the macro process is invoked ;&lt;BR /&gt;
******* prepare a lookup from strings created by the &amp;amp;condition, to numbers ;&lt;BR /&gt;
proc format ;     invalue condi  "a" = 10 "b" = 1 .... other = 0 ;&lt;BR /&gt;
run ;&lt;BR /&gt;
&lt;BR /&gt;
%* and now use that lookup  ;&lt;BR /&gt;
%do i=1 %to %sysfunc( inputN( &amp;amp;condition, condi ))  ;&lt;BR /&gt;
&lt;BR /&gt;
&amp;gt; %put "this is just a test";&lt;BR /&gt;
&amp;gt; %end;&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; %mend;&lt;BR /&gt;
&amp;gt; %test(condition = a)&lt;BR /&gt;
&amp;gt; %test(condition = b)&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; can someone help with this..&lt;BR /&gt;
 &lt;BR /&gt;
peterC</description>
      <pubDate>Fri, 30 Jul 2010 11:51:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loops-within-if-conditions/m-p/56631#M12126</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2010-07-30T11:51:16Z</dc:date>
    </item>
  </channel>
</rss>

