%SUBSTRING built-in function

The substring built-in function (%SUBSTRING or%SST) produces a character string that is a subset of an existing character string and can only be used within a CL procedure.

In a Change Variable (CHGVAR) command, the %SST function can be specified in place of the variable (VAR parameter) to be changed or the value (VALUE parameter) to which the variable is to be changed. In an If (IF) command, the %SST function can be specified in the expression.

The format of the substring built-in function is shown in this example:
%SUBSTRING(character-variable-name starting-position length)

It can also be formatted as shown in this example:

%SST(character-variable-name starting-position length)

You can code *LDA in place of the character variable name to indicate that the substring function is performed on the contents of the local data area.

The substring function produces a substring from the contents of the specified CL character variable or the local data area. The substring begins at the specified starting position (which can be a variable name) and continues for the length specified (which can also be a variable name). Neither the starting position nor the length can be 0 or negative. If the sum of the starting position and the length of the substring are greater than the length of the entire variable or the local data area, an error occurs. The length of the local data area is 1024.

The following are examples of the substring built-in function:

The following procedure uses the substring built-in function to find the first sentence in a 50-character field &INPUT and to place any remaining text in a field &REMAINDER It assumes that a sentence must have at least 2 characters, and no embedded periods.

        PGM (&INPUT &REMAINDER)   /*  SEARCH  */
        DCL &INPUT     *CHAR LEN(50)
        DCL &REMAINDER *CHAR LEN(50)
        DCL &X *INT  /* INDEX            */
        DCL &L *INT  /* REMAINING LENGTH */

        DOFORL:
        DOFOR &X 3 50
          IF (%SST(&INPUT &X 1) *EQ '.') THEN(DO)
             CHGVAR &L (50-&X)
             CHGVAR &X (&X+1)
             CHGVAR &REMAINDER %SST(&INPUT &X &L)
             LEAVE
             ENDDO
        ENDDO
ENDPGM          

The procedure starts by checking the third position for a period. Note that the substring function checks &INPUT from position 3 to a length of 1, which is position 3 only (length cannot be zero). If position 3 is a period, the remaining length of &INPUT is calculated. The value of &X is advanced to the beginning of the remainder, and the remaining portion of &INPUT is moved to &REMAINDER.

If position 3 is not a period, the procedure checks to see if it is at position 49. If so, it assumes that position 50 is a period and returns. If it is not at position 49, the procedure advances &X to position 4 and repeats the process.

Related tasks
Specify a list or qualified name using a variable
Change the value of a variable
Related reference
Built-in functions for CL
Related information
CL command finder
If (IF) command
Change Variable (CHGVAR) command