LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (May 2005, week 1)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Mon, 2 May 2005 10:49:07 -0400
Reply-To:     Dianne Rhodes <RHODESD1@WESTAT.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Dianne Rhodes <RHODESD1@WESTAT.COM>
Subject:      Re: %FACT(or something like that) from SAS 82 Users guide.
Comments: To: "Fehd, Ronald J" <rjf2@CDC.GOV>
Content-Type: text/plain; charset="US-ASCII"

> > > > Does anyone still have the SAS 82 Users Guide? I recall an > interesting > > macro, %FACT may have been its name, that used recursion to compute > > factorials. > > > > I don't want to compute factorials by I may want to use > recursion or > > at least think about it. > > > > If anyone has access to this long forgotten text and has > time to post > > it here it would be interesting to me. As I recall it was just few > > lines. >

Sure 'nuff, Page 470 Chapter 15 Example 4 This macro computes the factorial (N!) of the value of macro parameter N. It illustrates recursive execution of a macro, that is, a macro calling itself.

%macro factorl(n); %if &n=0 %then 1; %else %if &n=1 %then 1 ; %else %eval(&n*%factorl(%eval(&n-1))) ; %mend factorl ; Here's the log:

1 options mprint symbolgen ; 2 3 %macro factorl(n); 4 %if &n=0 %then 1; 5 %else %if &n=1 %then 1 ; 6 %else %eval(&n*%factorl(%eval(&n-1))) ; 7 %mend factorl ; 8 9 %let number=%factorl(5) ; SYMBOLGEN: Macro variable N resolves to 5 SYMBOLGEN: Macro variable N resolves to 5 SYMBOLGEN: Macro variable N resolves to 5 SYMBOLGEN: Macro variable N resolves to 5 SYMBOLGEN: Macro variable N resolves to 4 SYMBOLGEN: Macro variable N resolves to 4 SYMBOLGEN: Macro variable N resolves to 4 SYMBOLGEN: Macro variable N resolves to 4 SYMBOLGEN: Macro variable N resolves to 3 SYMBOLGEN: Macro variable N resolves to 3 SYMBOLGEN: Macro variable N resolves to 3 2 The SAS System 10:47 Monday, May 2, 2005

SYMBOLGEN: Macro variable N resolves to 3 SYMBOLGEN: Macro variable N resolves to 2 SYMBOLGEN: Macro variable N resolves to 2 SYMBOLGEN: Macro variable N resolves to 2 SYMBOLGEN: Macro variable N resolves to 2 SYMBOLGEN: Macro variable N resolves to 1 SYMBOLGEN: Macro variable N resolves to 1 10 %put &number ; SYMBOLGEN: Macro variable NUMBER resolves to 120 120 NOTE: SAS Institute Inc., SAS Campus Drive, Cary, NC USA 27513-2414 NOTE: The SAS System used: real time 2.15 seconds cpu time 0.45 seconds

Dianne Louise Rhodes Westat


Back to: Top of message | Previous page | Main SAS-L page