• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / SAS / mod Function in SAS – Find Remainder of 2 Numbers After Division

mod Function in SAS – Find Remainder of 2 Numbers After Division

January 20, 2022 Leave a Comment

In SAS, we can use the mod() function to calculate the remainder of 2 numbers after division.

You can use the SAS mod() function in a SAS data step, in PROC SQL, or in the macro language.

data data_new;
    set data;
    remainder = mod(var1,divisor);
run;

proc sql noprint;
   select mod(num,divisor) into :x from data;
quit;

%macro calc_mod(a,b);
   %let mod = %sysfunc(MOD(&a,&b));
   %put &mod;
%mend;

%calc_mod(10,3); /* Puts 1 to log */

In SAS, the mod() function can be very useful in data steps or in a SAS macro. Using the SAS mod() function in a SAS data step is easy.

Let’s say we have the following SAS dataset.

data data;
	input num;
	datalines;
84
19
5
32
-13
-100
;
run;

Let’s calculate the remainder of these numbers when divided by 2, 3, 5 and 9 after using the mod() function in our SAS code.

data data_new;
	set data;
	r2 = mod(num,2);
	r3 = mod(num,3);
	r5 = mod(num,5);
	r9 = mod(num,9);
run;

/* Output: */
   num  r2   r3   r5   r9
1   84	 0    0    4    3
2   19   1    1    4    1
3    5   1    2    0    5
4   32   0    2    2    5
5  -13  -1   -1   -3   -4
6 -100   0   -1    0   -1

The mod() function in SAS also allows for negative moduli and non-integer moduli.

Let’s calculate the remainders of the numbers in our dataset when dividing by some negative numbers as well as some non-integers.

data data_new;
	set data;
	rm3 = mod(num,-3);
	rm10 = mod(num,-10);
	r0p3 = mod(num,0.3);
	r0p83 = mod(num,0.83);
run;

/* Output: */
   num rm3 rm10  r0p3  r0p83
1   84	 0    4     0   0.17
2   19   1    9   0.1   0.74
3    5   2    5   0.2   0.02
4   32   2    2   0.2   0.46
5  -13  -1   -3  -0.1  -0.55
6 -100  -1    0  -0.1   -0.4

Using the SAS mod() Function in PROC SQL

You can also use the SAS mod() function with PROC SQL.

We can replicate the dataset from above using PROC SQL and the SAS mod() function. The below code will create a new dataset with the remainders calculated after dividing by 2, 3, 5 and 9.

proc sql;
   create table data_new as select *, mod(num,2) as r2, mod(num,3) as r3, mod(num,5) as r5, mod(num,9) as r9 from data;
quit;

/* Output: */
   num  r2   r3   r5   r9
1   84	 0    0    4    3
2   19   1    1    4    1
3    5   1    2    0    5
4   32   0    2    2    5
5  -13  -1   -1   -3   -4
6 -100   0   -1    0   -1

As you can see, this is the same result as we received above in the data step.

Using the SAS mod() Function with the SAS Macro Language

With the SAS Macro Language, we can create complex programs which can be dynamic and effective for getting a lot done.

Unfortunately, there is no mod() function in the macro language, but we can use the SAS %sysfunc() function to use mod() in a macro.

For example, we can use the mod() function like so in a simple macro:

%macro calc_mod(a,b);
   %let mod = %sysfunc(MOD(&a,&b));
   %put &mod;
%mend;

%calc_mod(10,3); /* puts 1 to the log */

Many times when we have a macro, we are in a loop and updating things depending on where we are in the loop. We can use mod() to only perform an operation in certain areas of our loop.

Let’s say we are looping over the numbers 1 to 10 and only want to do something when we are in the 3rd, 6th and 9th iteration.

We can use the mod() function in our macro to easily accomplish this.

%macro looping_macro;
    %do i = 1 %to 10;   
        %if %sysfunc(mod(&i,3)) = 0 %then %do;
            /* do stuff here */
        %end;
    %end;
%mend;

With the code above, you will only “do stuff here” 3 times – when i is equal to 3, 6 and 9.

Hopefully this article has been useful for you to learn how to use the SAS mod() function to calculate the remainder of two numbers after division.

Other Articles You'll Also Like:

  • 1.  SAS contains() – Check if Variable Contains a String in Where Statement
  • 2.  SAS include – Execute Code from Other Files in SAS with %include
  • 3.  Concatenating Strings in a SAS Data Step
  • 4.  PROC Format – Create User-Defined Formats for Variables in SAS
  • 5.  Round Number to Nearest Integer in SAS
  • 6.  SAS where in – Subset Data by Multiple Values in Data Step
  • 7.  SAS min() Function – Find Minimum Value Across Columns in Data Step
  • 8.  Do Loop in SAS Macro Language
  • 9.  SAS _n_ – How to Use the Automatic Variable _n_ in a Data Step
  • 10.  SAS ceil – Round Up to Ceiling of Number in a SAS Data Step

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy