- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I'd say (untested):
proc means data=divide sum;
where div3 = 1 and div5 = 1;
var i;
run;
Cheers, Eric