BookmarkSubscribeRSS Feed
NN
Quartz | Level 8 NN
Quartz | Level 8
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..
5 REPLIES 5
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Use another SAS macro to code (once) your repeat-logic, then invoke that macro from within your two %DO loops.

Scott Barry
SBBWorks, Inc.
martha_sas
SAS Employee
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;
Cynthia_sas
SAS Super FREQ
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]
NN
Quartz | Level 8 NN
Quartz | Level 8
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.
Peter_C
Rhodochrosite | Level 12
> 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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 994 views
  • 0 likes
  • 5 in conversation