BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
POOJA_J
Obsidian | Level 7

POOJA_J_0-1697546347258.png

 

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
Rick_SAS
SAS Super FREQ

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.

View solution in original post

4 REPLIES 4
Rick_SAS
SAS Super FREQ

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.

Quentin
Super User

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 Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
POOJA_J
Obsidian | Level 7
I searched in google - MCQ questions for Advanced SAS certification.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 441 views
  • 4 likes
  • 4 in conversation