Solved
New Contributor
Posts: 2

# Divisibility in SAS

I am fairly new to SAS and need to figure out a program to find numbers less than 1000 that are divisible by 3 or 5.  I know that this will be very simple, but since I am so new, I cannot figure it out.  Can anyone help me out?

Accepted Solutions
Solution
‎08-04-2015 10:16 AM
Super User
Posts: 9,599

## Re: Divisibility in SAS

Hi,

Mod() function returns the remainder of a division.  You can then check if this is > 0.

data want;

do i=1 to 1000;

if mod(i,3)=0 then flag_3="Y";

else flag_3="N";

if mod(i,5)=0 then flag_5="Y";

else flag_5="N";

output;

end;

run;

All Replies
Occasional Contributor
Posts: 11

## Re: Divisibility in SAS

Try this as a start:

data divide;

do i = 1 to 1000;

div3 = (mod(i, 3) = 0);

div5 = (mod(i, 5) = 0);

output;

end;

run;

Hth, Eric

Solution
‎08-04-2015 10:16 AM
Super User
Posts: 9,599

## Re: Divisibility in SAS

Hi,

Mod() function returns the remainder of a division.  You can then check if this is > 0.

data want;

do i=1 to 1000;

if mod(i,3)=0 then flag_3="Y";

else flag_3="N";

if mod(i,5)=0 then flag_5="Y";

else flag_5="N";

output;

end;

run;

New Contributor
Posts: 2

## Re: Divisibility in SAS

Great, thanks!  How about being able to find the sum of all of the numbers that are divisible by both?  I can do that by hand, but I am sure there is an easier way to do that in SAS, correct?

Super User
Posts: 9,599

## Re: Divisibility in SAS

If you need to do this then probably best to change FLAG_3 and FLAG_5 to something easier to group by:

data want;

do i=1 to 1000;

if mod(i,3)=0 then do;

flag=3;

output;

end;

if mod(i,5)=0 then do;

flag=5;

output;

end;

end;

run;

proc sql;

create table SUMS as

select     FLAG,

sum(I) as SUM_OF_I

from       WORK.WANT

group by  FLAG;

quit;

Occasional Contributor
Posts: 11

## Re: Divisibility in SAS

I'd say (untested):

proc means data=divide sum;

where div3 = 1 and div5 = 1;

var i;

run;

Cheers, Eric

🔒 This topic is solved and locked.