Start of change

%LEN built-in function

The %LEN built-in function returns the number of digits or characters of the CL numeric or character variable.

The %LEN built-in function can be used anywhere that CL supports an arithmetic expression. %LEN can be used alone or as part of a more complex arithmetic expression. For example, %LEN can be used to compare the number of digits of a decimal CL variable to a numeric CL variable in the COND parameter of an IF or WHEN command. %LEN can also be used to set the value of a CL command parameter, if the associated command object defines the parameter with EXPR(*YES) and TYPE of *DEC, *INT2, *INT4, *UINT2, or *UINT4.

The format of the getting length built-in function is:
%LEN(variable-argument)

The variable-argument must be a CL variable with TYPE of *CHAR, *DEC, *INT or *UINT.

For numeric variables, the value returned represents the precision of the variables and not necessarily the actual number of significant digits. For 2-byte *INT or *UINT CL variables, the value returned is always 5. For 4-byte *INT or *UINT CL variables, the value returned is always 10. For 8-byte *INT CL variables, the value returned is always 19. For 8-byte *UINT CL variables, the value returned is always 20.

For character variables, the value returned is the number of the characters.

The following are examples of using the %LEN built-in function:

  • Get the length of numeric variables
    DCL VAR(&NUM1) TYPE(*DEC)
    DCL VAR(&NUM2) TYPE(*DEC) LEN(7 2)
    DCL VAR(&NUM3) TYPE(*INT) LEN(4)
    DCL VAR(&NUM4) TYPE(*UINT) LEN(2)
    DCL VAR(&RTN) TYPE(*INT) LEN(2)
    
    /* &RTN will have the value 15. */
    CHGVAR VAR(&RTN) VALUE(%LEN(&NUM1))
    /* &RTN will have the value 7. */
    CHGVAR VAR(&RTN) VALUE(%LEN(&NUM2))
    /* &RTN will have the value 10. */
    CHGVAR VAR(&RTN) VALUE(%LEN(&NUM3))
    /* &RTN will have the value 5. */
    CHGVAR VAR(&RTN) VALUE(%LEN(&NUM4))
  • Get the length of character variables
    DCL VAR(&CHAR1) TYPE(*CHAR)
    DCL VAR(&CHAR2) TYPE(*CHAR) LEN(20)
    DCL VAR(&RTN) TYPE(*INT) LEN(2)
    
    /* &RTN will have the value 32. */
    CHGVAR VAR(&RTN) VALUE(%LEN(&CHAR1))
    /* &RTN will have the value 20. */
    CHGVAR VAR(&RTN) VALUE(%LEN(&CHAR2))
    /* &RTN will have the value 52. */
    CHGVAR VAR(&RTN) VALUE(%LEN(&CHAR1) + %LEN(&CHAR2))
End of change