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 !!!
%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.
From the lessons in your course, you should be able to do your homework yourself.
Let us see what you tried.
data count;
var='countdown';
do i=n to 1 by -1;
put vari;
end;
stop;
run;
Could you please give solution
So next, do the same in a macro with a %DO loop and a macro parameter as starting value.
I didn't get any idea how to do that
Were you sleeping through the course lessons?
Converting a data step loop to a macro loop is dead simple (if you paid any attention).
%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 ??
%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.
THANK YOU VERY MUCH
There is no recursion in that example.
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;
@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?
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.