turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- How to pick prime,even and odd numbers using datas...

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-05-2017 12:57 AM

How to pick prime,even and odd numbers using datasteps and do loops? Atleast 0 to 50?

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to rajeshalwayswel

11-05-2017 02:51 AM

For odd/even use the mod() function.

For prime numbers I'd use an array and do something like

```
array primes{50} 1;
do i1 = 2 to 50;
if primes{i1}
then do;
i2 = 2 * i1;
do while (i2 lt 50);
primes{i2} = 0;
i2 + i1;
end;
end;
end;
```

After that, all elements where the index is a prime number contain a 1.

You can replace all 50's with a macro variable to make the code flexible.

---------------------------------------------------------------------------------------------

Maxims of Maximally Efficient SAS Programmers

How to convert datasets to data steps

How to post code

Maxims of Maximally Efficient SAS Programmers

How to convert datasets to data steps

How to post code

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KurtBremser

11-05-2017 08:58 AM

When I exceute the code we have missing values in the dataset..array primes{50} 1; when we using 1 in this place I'm getting error...Is any thing need to change in it..

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to rajeshalwayswel

11-05-2017 09:40 PM - edited 11-05-2017 09:51 PM

A working version of the program from @KurtBremser.

```
data _null_;
array PRIMES{1:50} (50*1);
do I1 = 2 to 50;
if PRIMES{I1} then do;
I2 = 2 * I1;
do while (I2 lt 50);
PRIMES{I2} = 0; putlog I1= I2= PRIMES[I2]=;
I2 + I1;
end;
end;
end;
run;
```

which is a slightly more efficient version of

```
data _null_;
array PRIMES{1:50} (50*1);
do I1 = 2 to 50;
do I2 = I1 to 50/I1;
PRIMES{I1*I2} = 0; putlog I1= I2= PRIMES[I1*I2]=;
end;
end;
run;
```

I added the intermediate values in the log for you to see the logic.

Note that Kurt's loops need to be tweaked at bit as they flag 50 as a prime.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ChrisNZ

11-06-2017 02:26 AM

my initial code was written without access to SAS from @home, therefore the "something like". It was meant to show the basic algorithm for creating Eratosthenes's sieve.

A working version (syntactically and semantically, thanks to @ChrisNZ):

```
data primes (keep=primenumber);
array primes{50} 3 _temporary_ (50*1);
do i1 = 2 to 50;
if primes{i1}
then do;
i2 = 2 * i1;
do while (i2 le 50);
primes{i2} = 0;
i2 + i1;
end;
end;
end;
do primenumber = 1 to 50;
if primes{primenumber} then output;
end;
run;
```

---------------------------------------------------------------------------------------------

Maxims of Maximally Efficient SAS Programmers

How to convert datasets to data steps

How to post code

Maxims of Maximally Efficient SAS Programmers

How to convert datasets to data steps

How to post code

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to rajeshalwayswel

11-05-2017 05:21 AM

Calling @Rick_SAS

Here is an example.

https://blogs.sas.com/content/iml/2010/09/17/a-prime-number-sieve.html

and use MOD() to get odd or even number.