OVERLAY: Replacing Characters in a String

How to:

Given a starting position, length, source string, and insertion string, OVERLAY replaces the number of characters defined by length in the source string with the insertion string, starting from the starting position.

Syntax: How to Replace Characters in a String

OVERLAY(src, ins, start, len)

where:

src

Alphanumeric

Is the source string whose characters will be replaced.

ins

Alphanumeric

Is the insertion string with the replacement characters.

start

Numeric

Is the starting position for the replacement in the source string.

len

Numeric

Is the number of characters to replace in the source string with the entire insertion string.

Example: Replacing Characters in a String

The following request replaces the first three characters in the last name with the first four characters of the first name.

TABLE FILE EMPLOYEE
PRINT
COMPUTE FIRST4/A4 = LEFT(FIRST_NAME,4);
NEWNAME/A20 = OVERLAY(LAST_NAME, FIRST4, 1, 3);
BY LAST_NAME
BY FIRST_NAME
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.