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.
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 ;
|