- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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 !!!
- Using a %do loop
- Using a recursive method
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
From the lessons in your course, you should be able to do your homework yourself.
Let us see what you tried.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data count;
var='countdown';
do i=n to 1 by -1;
put vari;
end;
stop;
run;
Could you please give solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
So next, do the same in a macro with a %DO loop and a macro parameter as starting value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I didn't get any idea how to do that
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Were you sleeping through the course lessons?
Converting a data step loop to a macro loop is dead simple (if you paid any attention).
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%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 ??
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
THANK YOU VERY MUCH
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
There is no recursion in that example.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content