How to Use the MOD Function in SAS (With Example)


You can use the MOD function in SAS to calculate the remainder from a division operator

This function uses the following syntax:

MOD(dividend, divisor)

where:

  • dividend: The number to divide
  • divisor: The number to divide by

The following example shows how to use this function in practice.

Example: How to Use the MOD Function in SAS

Suppose we have the following dataset in SAS:

/*create dataset*/
data my_data;
    input dividend divisor;
    datalines;
36 6
10 3
15 5
15 6
10 7
22 4
24 4
30 8
;
run;

/*view dataset*/
proc print data=my_data;

The following code shows how to use the MOD function to create a new column that shows the remainder from dividing the values in the dividend column by the values in the divisor column of each row:

/*calculate remainder for each row*/
data new_data;
    set my_data;
    mod = mod(dividend, divisor);
run;

/*view new dataset*/
proc print data=new_data;

The new column called mod shows the remainder from dividing the values in the dividend column by the values in the divisor column of each row.

For example:

  • 6 goes into 36 exactly six times with a remainder of 0.
  • 3 goes into 10 three times with a remainder of 1.
  • 5 goes into 15 exactly three times with a remainder of 0.
  • 6 goes into 15 two times with a remainder of 3.

And so on.

Note that if the value in the divisor column was zero, the MOD function would simply return a period ( . ) in the mod column to indicate that division by zero is not possible.

Note: You can find the complete documentation for the SAS MOD function here.

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

How to Extract Numbers from String in SAS
How to Use the SUBSTR Function in SAS
How to Remove Special Characters from Strings in SAS

Featured Posts

Leave a Reply

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