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

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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