STRREP: Replacing Character Strings

How to:

Reference:

The STRREP replaces all instances of a specified string within a source string. It also supports replacement by null strings.

Syntax: How to Replace Character Strings

STRREP (inlength, instring, searchlength, searchstring, replength, repstring, outlength, output)

where:

inlength

Numeric

Is the number of characters in the source string.

instring

Alphanumeric

Is the source string.

searchlength

Numeric

Is the number of characters in the (shorter length) string to be replaced.

searchstring

Alphanumeric

Is the character string to be replaced.

replength

Numeric

Is the number of characters in the replacement string. Must be zero (0) or greater.

repstring

Alphanumeric

Is the replacement string (alphanumeric). Ignored if replength is zero (0).

outlength

Numeric

Is the number of characters in the resulting output string. Must be 1 or greater.

output

Alphanumeric

Is the resulting output string after all replacements and padding.

Reference: Usage Note for STRREP Function

The maximum string length is 4095.

Example: Replacing Commas and Dollar Signs

In the following example, STRREP finds and replaces commas and dollar signs that appear in the CS_ALPHA field, first replacing commas with null strings to produce CS_NOCOMMAS (removing the commas) and then replacing the dollar signs ($) with (USD) in the right-most CURR_SAL column:

TABLE FILE EMPLOYEE
SUM CURR_SAL NOPRINT
COMPUTE CS_ALPHA/A15=FTOA(CURR_SAL,'(D12.2M)',CS_ALPHA);
        CS_NOCOMMAS/A14=STRREP(15,CS_ALPHA,1,',',0,'X',14,CS_NOCOMMAS);
        CS_USD/A17=STRREP(14,CS_NOCOMMAS,1,'$',4,'USD ',17,CS_USD);
        NOPRINT
        CS_USD/R AS CURR_SAL
BY LAST_NAME
END

The output is:

LAST_NAME   CS_ALPHA             CS_NOCOMMAS                 CURR_SAL
---------   --------             -----------        -----------------
BANNING          $29,700.00           $29700.00          USD 29700.00
BLACKWOOD        $21,780.00           $21780.00          USD 21780.00
CROSS            $27,062.00           $27062.00          USD 27062.00
GREENSPAN         $9,000.00            $9000.00          USD  9000.00
IRVING           $26,862.00           $26862.00          USD 26862.00
JONES            $18,480.00           $18480.00          USD 18480.00
MCCOY            $18,480.00           $18480.00          USD 18480.00
MCKNIGHT         $16,100.00           $16100.00          USD 16100.00
ROMANS           $21,120.00           $21120.00          USD 21120.00
SMITH            $22,700.00           $22700.00          USD 22700.00
STEVENS          $11,000.00           $11000.00          USD 11000.00