SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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