Wednesday, June 26, 2024

Master ABAP `nmin` and `nmax` Functions with Examples | ABAP Programming Guide

ABAP `nmin` and `nmax` Predefined Functions

ABAP nmin and nmax Predefined Functions

ABAP offers powerful predefined functions that simplify various programming tasks. Two such functions are nmin and nmax. These functions help in finding the minimum and maximum values, respectively, from a set of numeric expressions.

nmin Function

The nmin function returns the minimum value from a list of numeric expressions. The syntax is:

nmin( val1, val2, ... valN )

Here is an example of using nmin:

DATA(min_val) = nmin( 5, 10, -3, 8, 2 ). 

" min_val will be -3

DATA: val1 TYPE i VALUE 20,

      val2 TYPE i VALUE 15,

      val3 TYPE i VALUE 25,

      min_of_vals TYPE i.

min_of_vals = nmin( val1, val2, val3 ).

" min_of_vals will be 15

You can also use nmin with internal tables:

DATA: lt_values TYPE TABLE OF i WITH EMPTY KEY,

      lv_min_val TYPE i.

APPEND 3 TO lt_values.

APPEND 9 TO lt_values.

APPEND -2 TO lt_values.

lv_min_val = nmin( LINES OF lt_values ).

" lv_min_val will be -2

nmax Function

The nmax function returns the maximum value from a list of numeric expressions. The syntax is:

nmax( val1, val2, ... valN )

Here is an example of using nmax:

DATA(max_val) = nmax( 5, 10, -3, 8, 2 ). 

" max_val will be 10

DATA: val1 TYPE i VALUE 20,

      val2 TYPE i VALUE 15,

      val3 TYPE i VALUE 25,

      max_of_vals TYPE i.

max_of_vals = nmax( val1, val2, val3 ).

" max_of_vals will be 25

You can also use nmax with internal tables:

DATA: lt_values TYPE TABLE OF i WITH EMPTY KEY,

      lv_max_val TYPE i.

APPEND 3 TO lt_values.

APPEND 9 TO lt_values.

APPEND -2 TO lt_values.

lv_max_val = nmax( LINES OF lt_values ).

" lv_max_val will be 9

Both functions are extremely useful for quickly determining the smallest and largest values in a dataset, respectively.

Conclusion

The nmin and nmax functions in ABAP provide a straightforward way to find the minimum and maximum values from a set of numeric expressions or internal tables. They enhance code readability and efficiency, making your ABAP programs more robust and easier to maintain.

ABAP Inline Declarations: A Byte-Sized Guide with Examples

ABAP Inline Declarations ABAP Inline Declarations ABAP inline declarations are a feature intr...