BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Mohan_Reddy
Obsidian | Level 7
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....

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
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;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

What if a number is both prime and odd?

Mohan_Reddy
Obsidian | Level 7

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 !!!!

PeterClemmensen
Tourmaline | Level 20
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;
Kurt_Bremser
Super User

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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 2591 views
  • 0 likes
  • 3 in conversation