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

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 !!!

  1. Using a %do loop
  2. Using a recursive method

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
%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.

View solution in original post

17 REPLIES 17
pavank
Quartz | Level 8
data count;

var='countdown';
do i=n to 1 by -1;

put vari;
end;
stop;
run;

Could you please give solution

pavank
Quartz | Level 8

I didn't get any  idea how to do that 

pavank
Quartz | Level 8
%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 ??

Kurt_Bremser
Super User
%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.

pavank
Quartz | Level 8

THANK YOU VERY MUCH 

Tom
Super User Tom
Super User

There is no recursion in that example.

 

pavank
Quartz | Level 8
What is recursive method how to do recursive methof using bu above example
Tom
Super User Tom
Super User

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
Rhodochrosite | Level 12
Why by -1 in do loop?
PaigeMiller
Diamond | Level 26

@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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 17 replies
  • 1883 views
  • 9 likes
  • 6 in conversation