Post by Dirk NachbarPost by Satyahi 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 ;