BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Alass006
Fluorite | Level 6

Hello there,

 

I am reaching out for sas question and wondering if you can assist me in explaining the right anwer.

 

The following SAS program is submitted:

%macro mysum(n);

%if &n > 1 %then %eval(&n + %mysum(%eval(&n-1)));

%else &n;

%mend;

%put %mysum(4);

 

Which output is written to the log?

A. 10

B. 4+3+2+1

C. 7

D. A character operand was found in the %EVAL function or %IF condition where a numeric operand is required.

 

correct_answer = "A"

 

I would go for "C."  I have access to enterprise guide but nothing is written to the  log.

1 ACCEPTED SOLUTION

Accepted Solutions
Urban_Science
Quartz | Level 8

I think you are getting 7 because you are not following the recursion all the way through.  Notice that in the %if statement that %mysum is called again, except with &n -1. Think about what %mysum(3) returns.  

 

Also, unless you have settings to prevent %put from outputting to the log, I think the answer is getting buried in the line execution numbers.  This is what mine looks like after being ran:

1 %_eg_hidenotesandsource;
5 %_eg_hidenotesandsource;
31
32 %macro mysum(n);
33
34 %if &n > 1 %then %eval(&n + %mysum(%eval(&n-1)));
35
36 %else &n;
37
38 %mend;
39
40 %put %mysum(4);
10
41
42 %_eg_hidenotesandsource;
57
58
59 %_eg_hidenotesandsource;

Notice that 10 is in there right between line 40 and 41.

 

Something that I do when I am debugging my macros is to add log notes when the macro is called.  For example: 

%macro mysum(n);

%put mysum called: n=&n;

%if &n > 1 %then %eval(&n + %mysum(%eval(&n-1)));

%else &n;

%mend;

%put %mysum(4);

you can then see it being called 4 total times in the log: 

42 %put %mysum(4);
mysum called: n=4
mysum called: n=3
mysum called: n=2
mysum called: n=1
10

43

 

View solution in original post

4 REPLIES 4
Urban_Science
Quartz | Level 8

I think you are getting 7 because you are not following the recursion all the way through.  Notice that in the %if statement that %mysum is called again, except with &n -1. Think about what %mysum(3) returns.  

 

Also, unless you have settings to prevent %put from outputting to the log, I think the answer is getting buried in the line execution numbers.  This is what mine looks like after being ran:

1 %_eg_hidenotesandsource;
5 %_eg_hidenotesandsource;
31
32 %macro mysum(n);
33
34 %if &n > 1 %then %eval(&n + %mysum(%eval(&n-1)));
35
36 %else &n;
37
38 %mend;
39
40 %put %mysum(4);
10
41
42 %_eg_hidenotesandsource;
57
58
59 %_eg_hidenotesandsource;

Notice that 10 is in there right between line 40 and 41.

 

Something that I do when I am debugging my macros is to add log notes when the macro is called.  For example: 

%macro mysum(n);

%put mysum called: n=&n;

%if &n > 1 %then %eval(&n + %mysum(%eval(&n-1)));

%else &n;

%mend;

%put %mysum(4);

you can then see it being called 4 total times in the log: 

42 %put %mysum(4);
mysum called: n=4
mysum called: n=3
mysum called: n=2
mysum called: n=1
10

43

 

Alass006
Fluorite | Level 6
Thank you much !
ballardw
Super User

I don't use EG but you might try changing the final %put to this:

%put NOTE: Final value of mysum macro is: %mysum(4);

 

 

so the result stands out more. If the log uses different appearance for NOTE: WARNING: and ERROR: entries it is likely the result will have those appearance options.

raawan
Calcite | Level 5

WHEN IS IT GOOD TO COMBINE MULTIPLE SAS DATASETS BY MATCHING KEY VALUES??

WITH INDEXES ON KEY VALUES OR WITHOUT INDEXES ON KEY VALUES

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!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 2061 views
  • 5 likes
  • 4 in conversation