I have dataset like below...
data ds;
do i=1 to 1000;
output;
end;
stop;
run;
Now i want output like
Using that dataset "i" value create 3 new columns(variables)..One Variable should contain Odd numbers and
One variable should contain Even numbers
Last variable should contain Prime numbers
How do write a programme ???please help us....
data want(drop=j);
array p {1000} _temporary_ (0 2:1000);
do i = 2 to 1000;
do j = i to 1000/i;
p [i*j] = 0;
end;
end;
do until (lr);
set ds end=lr;
call missing (Even, Odd, Prime);
if mod(i, 2) = 0 then Even = i;
else Odd = i;
if whichn(i, of p[*]) then prime = i;
output;
end;
run;
What if a number is both prime and odd?
Through that programme i values =1 2 3 4 ..........................1000 right
through that dataset i want create new dataset have three new variables
like
Odd_Number ( first variable name ) = 1 3 5 7 9 11 ................999
Even_Number (second variable name) = 2 4 6 8 10 12 ..................1000
Prime_Numbers (third variable name) = 2 3 5 7 11 13 ...........997
Like these i want output...I Hope you understand my doubt what i need in output dataset..
Please find a solution for that !!!!
data want(drop=j);
array p {1000} _temporary_ (0 2:1000);
do i = 2 to 1000;
do j = i to 1000/i;
p [i*j] = 0;
end;
end;
do until (lr);
set ds end=lr;
call missing (Even, Odd, Prime);
if mod(i, 2) = 0 then Even = i;
else Odd = i;
if whichn(i, of p[*]) then prime = i;
output;
end;
run;
Instead of creating redundant values, create flags:
%let max = 1000;
data have;
do i = 1 to &max.;
output;
end;
run;
data want;
set have;
length primes $&max.;
retain primes;
if mod(i,2) = 0
then do;
odd = 'N';
even = 'Y';
end;
else do;
odd = 'Y';
even = 'N';
end;
if substr(primes,i,1) = ' '
then prime = 'Y';
else prime = 'N';
index = i;
if i > 1 then do while (index <= &max.);
substr(primes,index,1) = 'Y';
index + i;
end;
drop primes index;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.