🔒 This topic is solved and locked.
Need further help from the community? Please
sign in and ask a new question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 02-26-2020 04:15 AM
(3056 views)
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....
- Tags:
- SAS Community
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
4 REPLIES 4
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
What if a number is both prime and odd?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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 !!!!
- Tags:
- SAS Community
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;