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 (September 2008, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Fri, 26 Sep 2008 06:32:34 -0700
Reply-To:     Chris Jones <chrisj75@GMAIL.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Chris Jones <chrisj75@GMAIL.COM>
Organization: http://groups.google.com
Subject:      Re: hi all,
              I to find prime number using Macro program and using base SAS.
Comments: To: sas-l@uga.edu
Content-Type: text/plain; charset=ISO-8859-1

On 26 Sep, 14:15, Dirk Nachbar <dirk...@googlemail.com> wrote: > On Sep 26, 4:55 am, Satya <kondal4u...@gmail.com> wrote: > > > hi all > > I to find prime number from 1 to 20 using Macro program and using > > base SAS. > > I tried but that code is some how big . > > Bye. > > hi > > here is ths solution > > option mprint; > %macro prime(n); > data prime; > do i=1 to &n; > prime=1; > do j=2 to ceil(i/2); > if mod(i,j)=0 then prime=0; > end; > if prime=1 then output; > end; > run; > %mend; > %prime(20);

Alternative solution to find all primes up to a certain maximum:

%LET MAX = 500000 ;

data primes ; do number = 3 to &MAX ; /* does it divide by 2? */ if mod(number,2) ^= 0 then do ; /* loop through odd numbers >= 3 */ testnum = 3 ; limit = number ; do while (limit > testnum) ; if mod(number,testnum) = 0 then do ; limit = 0 ; /* force exit of while loop */

end ; else do ; limit = ceil(number/ testnum) ; testnum + 2 ; /* only need to check odd numbers */

end ;

end ;

if limit ^= 0 then output ; /* if limit ^= 0 we successfully exited loop */ end ; /* mod 2 */ end ; /* do */ run ;


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