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.

## Leave a Reply