BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jsrg2015
Calcite | Level 5

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?

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

View solution in original post

5 REPLIES 5
EricHoogenboom
Fluorite | Level 6

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

jsrg2015
Calcite | Level 5

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?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

EricHoogenboom
Fluorite | Level 6

I'd say (untested):

proc means data=divide sum;

where div3 = 1 and div5 = 1;

var i;

run;

Cheers, Eric

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 6833 views
  • 9 likes
  • 3 in conversation