Ora

What is the DIVIDE Function in SAS?

Published in SAS Functions 4 mins read

The DIVIDE function in SAS is a specialized arithmetic function designed to divide two numbers and return a result that is specifically compatible with ODS (Output Delivery System) conventions. It provides a robust way to perform division, especially when dealing with missing data, by handling special missing values for ODS output.

Understanding the DIVIDE Function

While SAS offers the standard division operator (/), the DIVIDE function provides enhanced behavior, particularly beneficial when your results are intended for presentation through ODS. It ensures that output aligns with how ODS expects missing and special missing values to be treated.

Syntax

The syntax for the DIVIDE function is straightforward:

DIVIDE(numerator, denominator);
  • numerator: The number to be divided.
  • denominator: The number by which the numerator is divided.

How DIVIDE Handles Values

The DIVIDE function is unique in its approach to various scenarios, especially concerning missing values and division by zero.

Key Features and Benefits:

  • ODS Compatibility: The primary advantage of DIVIDE is its design to produce results that are compatible with ODS conventions. This means the function handles output in a way that is optimized for ODS reports and tables, ensuring consistency in how missing data is presented.
  • Special Missing Value Handling: Unlike the standard / operator, DIVIDE explicitly handles special missing values (e.g., .A, .B, .Z) for ODS output. This ensures that these values are interpreted and processed in a manner suitable for ODS, which can be crucial for maintaining data integrity and report clarity.
  • Division by Zero:
    • If the denominator is zero, the function returns a standard missing value (.).
    • If the numerator is also zero, it still returns a standard missing value (.).
  • Missing Values:
    • If the numerator is missing and the denominator is not zero, the function returns a missing value.
    • If the denominator is missing, the function returns a missing value.

DIVIDE vs. Standard Division Operator (/)

It's important to understand when to choose DIVIDE over the simpler / operator.

Feature DIVIDE Function Standard Division Operator (/)
Primary Use Case ODS-compatible output, robust handling General arithmetic
Division by Zero Returns standard missing (.) Returns .
Missing Values Returns standard missing (.) Returns .
Special Missing Values Explicitly handles for ODS output Treats as standard missing
Error Handling Designed for consistent ODS output Basic arithmetic error handling

For most basic calculations where ODS compatibility and special missing value interpretation aren't critical, the / operator is sufficient. However, when generating reports or working with data that involves various types of missing values and requires precise ODS formatting, DIVIDE is the preferred choice.

Practical Examples

Let's look at how the DIVIDE function operates in different scenarios.

data financial_ratios;
    input Sales Cost NetIncome @;
    datalines;
    1000 500 200
    500 250 .
    . 100 50
    800 0 150
    0 0 0
    900 .A 300 /* .A is a special missing value */
    ;
run;

data calculated_ratios;
    set financial_ratios;

    /* Using DIVIDE function */
    Profit_Margin_DIVIDE = DIVIDE(NetIncome, Sales);
    Cost_Ratio_DIVIDE = DIVIDE(Cost, Sales);
    EPS_DIVIDE = DIVIDE(NetIncome, 0); /* Example of division by zero */
    Special_Missing_Ratio_DIVIDE = DIVIDE(Sales, Cost); /* Demonstrating .A handling for ODS */

    /* Using standard division operator (for comparison) */
    Profit_Margin_Standard = NetIncome / Sales;
    Cost_Ratio_Standard = Cost / Sales;
    EPS_Standard = NetIncome / 0;
    Special_Missing_Ratio_Standard = Sales / Cost;
run;

proc print data=calculated_ratios;
    title "Comparison of DIVIDE Function vs. Standard Division Operator";
run;

In this example:

  • Profit_Margin_DIVIDE and Cost_Ratio_DIVIDE will correctly calculate ratios, returning standard missing values where NetIncome, Sales, or Cost are missing.
  • EPS_DIVIDE and EPS_Standard will both result in a missing value (.) because the denominator is zero.
  • Special_Missing_Ratio_DIVIDE will process Cost as .A in the calculation, with DIVIDE ensuring its compatibility with ODS conventions, while Special_Missing_Ratio_Standard will treat .A simply as a missing value. The key difference here is the intent and metadata that DIVIDE carries for ODS.

For more in-depth information, you can refer to the official SAS documentation for the DIVIDE function.