Obsidian | Level 7

## The macro doesn't contain a DO loop. How does it loop in below example?

I cam across one of this MCQ's.

The macro doesn't have a DO loop so how does it create values of 3,2,1?

Plz explain the logic working behind this.

1 ACCEPTED SOLUTION

Accepted Solutions
SAS Super FREQ

## Re: The macro doesn't contain a DO loop. How does it loop in below example?

The "magic" is in the first line. The %EVAL macro will evaluate it's argument when the code is run.

So when n=4, the first line evaluates to
%EVAL( 4 + %mysum(3) ).

This causes the %MYSUM macro to be run with a new argument. This time the first line evaluates to (dropping the %EVAL call, for convenience)
3 + %mysum(2).

This continues recursively until the argument n=1, in which case the macro generates

1

Cumulatively, the macro generates an expression that is equivalent to

%EVAL(4 + %EVAL(3 + %EVAL(2 + 1)))
which is displayed in the log by using the %PUT call.

4 REPLIES 4
Super User

SAS Super FREQ

## Re: The macro doesn't contain a DO loop. How does it loop in below example?

The "magic" is in the first line. The %EVAL macro will evaluate it's argument when the code is run.

So when n=4, the first line evaluates to
%EVAL( 4 + %mysum(3) ).

This causes the %MYSUM macro to be run with a new argument. This time the first line evaluates to (dropping the %EVAL call, for convenience)
3 + %mysum(2).

This continues recursively until the argument n=1, in which case the macro generates

1

Cumulatively, the macro generates an expression that is equivalent to

%EVAL(4 + %EVAL(3 + %EVAL(2 + 1)))
which is displayed in the log by using the %PUT call.

Super User

## Re: The macro doesn't contain a DO loop. How does it loop in below example?

Curious which class this is from, can you share?  I don't see a lot of recursive macros.  I would consider it an advanced topic for the macro language (but I guess would be included in an introductory class for many languages).

BASUG is hosting free webinars ! Check out our recordings of past webinars: https://www.basug.org/videos. Be sure to subscribe to our email list for notification of future BASUG events.
Obsidian | Level 7