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?
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;
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
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;
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?
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;
I'd say (untested):
proc means data=divide sum;
where div3 = 1 and div5 = 1;
var i;
run;
Cheers, Eric
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!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.