Quartz | Level 8

## macro do loop and recursive method

Hi Everyone

Goodmornin

macro function %countdown having a numeric parameter and producing the following output in the log :

Countdown 10...

Countdown 9...

Countdown 8...

Countdown 7...

Countdown 6...

Countdown 5...

Countdown 4...

Countdown 3...

Countdown 2...

Countdown 1...

BOOM !!!

1. Using a %do loop
2. Using a recursive method

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: macro do loop and recursive method

%macro countdown(num);
%do i = &num. %to 1 %by -1;
%put Countdown &i.;
%end;
%put Boom!;
%mend;

%countdown(10);

Study this closely, so you get a grasp on the very basics of macro programming.

Next, go searching for the concept of recursion in programming, and see how you can apply it to this task.

17 REPLIES 17
Super User

## Re: macro do loop and recursive method

From the lessons in your course, you should be able to do your homework yourself.

Let us see what you tried.

Quartz | Level 8

## Re: macro do loop and recursive method

data count;

var='countdown';
do i=n to 1 by -1;

put vari;
end;
stop;
run;

Super User

## Re: macro do loop and recursive method

So next, do the same in a macro with a %DO loop and a macro parameter as starting value.

Quartz | Level 8

## Re: macro do loop and recursive method

I didn't get any  idea how to do that

Super User

## Re: macro do loop and recursive method

Were you sleeping through the course lessons?

Converting a data step loop to a macro loop is dead simple (if you paid any attention).

Quartz | Level 8

## Re: macro do loop and recursive method

%macro countdown(ds,num);
var='Countdown';
%do i= 10 %to  1 by -1;
output;
end;
stop;
put vari;
run;
%macro;

%countdown(ds,10);
%put(countdown.. &i);

I didn't get output ??

Super User

## Re: macro do loop and recursive method

%macro countdown(num);
%do i = &num. %to 1 %by -1;
%put Countdown &i.;
%end;
%put Boom!;
%mend;

%countdown(10);

Study this closely, so you get a grasp on the very basics of macro programming.

Next, go searching for the concept of recursion in programming, and see how you can apply it to this task.

Quartz | Level 8

## Re: macro do loop and recursive method

THANK YOU VERY MUCH

Super User

## Re: macro do loop and recursive method

There is no recursion in that example.

Quartz | Level 8

## Re: macro do loop and recursive method

What is recursive method how to do recursive methof using bu above example
Super User

## Re: macro do loop and recursive method

Here is an introduction to the concept of recursion.

https://www.topcoder.com/thrive/articles/An%20Introduction%20to%20Recursion%20Part%20One

The SAS solution for your problems looks very much like the first example.

%macro countdown(start);
%if &start > 0 %then %do;
%put Countdown &start;
%countdown(%eval(&start-1))
%end;
%else %put BOOM !!!;
%mend countdown;
Rhodochrosite | Level 12

## Re: macro do loop and recursive method

Why by -1 in do loop?
Diamond | Level 26

## Re: macro do loop and recursive method

@David_Billa wrote:
Why by -1 in do loop?

If you want a loop to go from 10 to 9 to 8 to ..., what is the increment that gets you from 10 to 9?

--
Paige Miller
Super User

## Re: macro do loop and recursive method

Discussion stats
• 17 replies
• 2200 views
• 9 likes
• 6 in conversation