DATA Step, Macro, Functions and more

macro loops within if conditions

Reply
Regular Contributor
Regular Contributor
Posts: 166

macro loops within if conditions

Guys ,

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.

My problem is that i donot wish write the put statement twice in my query

%macro test(condition =);

%put "this is just a test";

%mend;
%test(condition = a)
%test(condition = b)

i donot wish to do this:::::
%macro test(condition =);
%if "&condition" = "a" %then %do i=1 %to 10;
%put "this is just a test";
%end;

%if "&condition" = "b" %then %do i=1 %to 1;
%put "this is just a test";
%end;

%mend;
%test(condition = a)
%test(condition = b)

can someone help with this..
Super Contributor
Super Contributor
Posts: 3,174

Re: macro loops within if conditions

Use another SAS macro to code (once) your repeat-logic, then invoke that macro from within your two %DO loops.

Scott Barry
SBBWorks, Inc.
SAS Employee
Posts: 19

Re: macro loops within if conditions

Set a count variable to 10 if condition=a, and to 1 if condition=b. Then use the variable in the %do statement.
Here is one possible solution:

%macro test(condition =);
%if "&condition"="a" %then %let count=10;
%else %if "&condition" = "b" %then %let count=1;

%do i=1 %to &count;
%put "this is just a test";
%end;
%mend;
SAS Super FREQ
Posts: 8,743

Re: macro loops within if conditions

Hi:
Instead of hard-coding the 10 and the 1, you can test the value of &CONDITION and create a new macro variable (perhaps, &STOP) which you could then use in your %DO loop. Something like what's shown in the log below.

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.

cynthia
[pre]
384 %macro test(condition =);
385
386 %put *********** STARTING Macro;
387
388 ** create STOP macro variable value;
389 %if &condition = a %then %do; %let stop = 10; %end;
390 %else %if &condition = b %then %do; %let stop = 1; %end;
391 %else %do;
392 %put INVALID Condition value: &condition;
393 %let stop = 0;
394 %end;
395
396 %put condition = &condition and stop = &stop;
397
398 %do i = 1 %to &stop;
399 %put i=&i stop=&stop "this is just a test";
400 %end;
401 %put ********** End of DO Loop;
402 %put **********;
403 %mend test;
404 %test(condition = a)
*********** STARTING Macro
condition = a and stop = 10
i=1 stop=10 "this is just a test"
i=2 stop=10 "this is just a test"
i=3 stop=10 "this is just a test"
i=4 stop=10 "this is just a test"
i=5 stop=10 "this is just a test"
i=6 stop=10 "this is just a test"
i=7 stop=10 "this is just a test"
i=8 stop=10 "this is just a test"
i=9 stop=10 "this is just a test"
i=10 stop=10 "this is just a test"
********** End of DO Loop
**********
405 %test(condition = b)
*********** STARTING Macro
condition = b and stop = 1
i=1 stop=1 "this is just a test"
********** End of DO Loop
**********
406 %test(condition = c)
*********** STARTING Macro
INVALID Condition value: c
condition = c and stop = 0
********** End of DO Loop
**********

[/pre]
Regular Contributor
Regular Contributor
Posts: 166

Re: macro loops within if conditions

Scott,
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.
Why didn't i think of this.

Martha / Cynthia ,
Thanks a lot.
Valued Guide
Posts: 2,175

Re: macro loops within if conditions

> Guys ,
>
> 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.
>
> My problem is that i donot wish write the put
> statement twice in my query
>
> %macro test(condition =);
>
> %put "this is just a test";
>
> %mend;
> %test(condition = a)
> %test(condition = b)
>
> i donot wish to do this:::::
> %macro test(condition =);
> %if "&condition" = "a" %then %do i=1 %to 10;

Did you consider this approach
* preliminary code executed before the macro process is invoked ;
******* prepare a lookup from strings created by the &condition, to numbers ;
proc format ; invalue condi "a" = 10 "b" = 1 .... other = 0 ;
run ;

%* and now use that lookup ;
%do i=1 %to %sysfunc( inputN( &condition, condi )) ;

> %put "this is just a test";
> %end;
>
>
> %mend;
> %test(condition = a)
> %test(condition = b)
>
> can someone help with this..

peterC
Ask a Question
Discussion stats
  • 5 replies
  • 164 views
  • 0 likes
  • 5 in conversation