Discussion:
hi all, I to find prime number using Macro program and using base SAS.
Satya
2008-09-26 03:55:32 UTC
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.
Dirk Nachbar
2008-09-26 13:15:01 UTC
Post by Satya
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);
Chris Jones
2008-09-26 13:32:34 UTC
Post by Dirk Nachbar
Post by Satya
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 ;
g***@gmail.com
2018-11-03 15:07:55 UTC