MainframeMaster

Find/Replace Operations

Find/replace in DFSORT means searching for a specific string or value in the record and replacing it with another. You do this with FINDREP, specifying IN= (what to find) and OUT= (what to replace it with). FINDREP is used with INREC or OUTREC. You can list multiple find/replace pairs so that several different strings or values are replaced in one pass. When you use FINDREP in INREC, the sort and INCLUDE/OMIT see the record after replacement; when you use it in OUTREC, only the final output is changed. This page covers FINDREP syntax, single and multiple replacements, and when to use INREC vs OUTREC.

Data Transformation
Progress0 of 0 lessons

What Is FINDREP?

FINDREP lets you change specific content in each record without writing a program. You say "find this string (or value) and replace it with that one." The product scans the record (or the reformatted record when FINDREP is combined with BUILD/FIELDS) and substitutes every occurrence of the find value with the replace value. That is useful for normalizing codes (e.g. replace "TML" with "TPT"), fixing known bad values, or standardizing abbreviations. Unlike TRAN=LTOU, which converts all lowercase letters to uppercase, FINDREP works on exact strings or values—so you can replace "OLD" with "NEW" without affecting other characters.

Basic Syntax

The general form is:

text
1
2
INREC FINDREP=(IN=find_value,OUT=replace_value) OUTREC FINDREP=(IN=find_value,OUT=replace_value)

For character strings you typically use C'...' (or the equivalent in your product) for IN and OUT. The find and replace values can be different lengths in some products (e.g. replace "XX" with "ABCD"); in others they must be the same length. Check your DFSORT manual. Example: replace the literal string "OLD" with "NEW" in every record:

text
1
2
SORT FIELDS=COPY OUTREC FINDREP=(IN=C'OLD',OUT=C'NEW')

Multiple Find/Replace Pairs

You can specify more than one replacement in the same FINDREP. List additional IN/OUT pairs after the first:

text
1
OUTREC FINDREP=(IN=C'OLD1',OUT=C'NEW1',IN=C'OLD2',OUT=C'NEW2',IN=C'X',OUT=C'Y')

The product applies each pair. If the same substring could match more than one IN (e.g. "OLD1" and "OLD"), the order of application may matter: the first matching replacement might change the data so that a later IN no longer matches. Design your pairs so that the intended replacements are clear (e.g. longer strings first if one string is a prefix of another). Replacing every occurrence means that if "OLD" appears three times in the record, all three become "NEW" (unless your product supports a "replace first only" option).

Replacing Numeric Values

Some DFSORT implementations allow you to find and replace numeric values—for example, replace the value 123 with 456. The exact syntax is product-dependent: you might specify IN=123, OUT=456, or you might need to give position, length, and format (e.g. PD, ZD) so the product knows where and how the number is stored. Replacing a numeric value is different from replacing the character string "123": the character form is three bytes EBCDIC "1" "2" "3", while a packed decimal 123 is typically two bytes. Check your manual for numeric FINDREP support and syntax.

INREC vs OUTREC for FINDREP

INREC runs before the sort and before INCLUDE/OMIT. So FINDREP in INREC means that the record seen by the sort and by any conditions is already modified. Use INREC when you need to sort by the replaced value or filter on it—for example, replace a code in INREC and then SORT FIELDS= or INCLUDE COND= on that code. OUTREC runs after the sort. The sort and filter see the original record; only the output dataset has the replacement. Use OUTREC when you only need the change in the final file (e.g. writing a corrected extract) and the sort/filter should use the original data.

Combining FINDREP with BUILD or FIELDS

In some products you can use FINDREP together with BUILD or FIELDS in the same statement. For example, you might first build a reformatted record with OUTREC BUILD=(...) and then apply FINDREP to that built record. The exact syntax (e.g. BUILD=(...),FINDREP=(...) or FINDREP before BUILD) is product-dependent. This is useful when the replacement should apply only to the reformatted output (e.g. after you have moved or edited fields) rather than to the raw input. See your DFSORT documentation for the allowed combinations.

FINDREP vs TRAN and ALTSEQ

FINDREP replaces specific strings or values. TRAN=LTOU (or UTOL, ETOA, etc.) converts whole character sets (e.g. all lowercase to uppercase) in a field. ALTSEQ applies a byte-by-byte translation table. Use FINDREP when you want to change one exact string to another (e.g. "TML" → "TPT"). Use TRAN when you want case or code-page conversion. Use ALTSEQ when you need a custom one-byte-to-one-byte mapping.

Example: Normalizing Status Codes

Suppose a 2-byte status at position 45 can be "A ", "IA", or " I" and you want to standardize to "A ", "I ", and "I ". You could use multiple find/replace pairs (if the exact strings are unique in the record) or handle with IFTHEN/OVERLAY in BUILD. FINDREP is simplest when the strings to replace do not appear elsewhere:

text
1
OUTREC FINDREP=(IN=C'IA',OUT=C'I ',IN=C' I',OUT=C'I ')

Order matters: if you had IN=C\' I\',OUT=C\'I \' first, then "IA" might be affected differently. Test with sample data to confirm the desired result.

Explain It Like I'm Five

Imagine you have a sentence and you want to change every "cat" to "dog". You don't change anything else—just find "cat" and write "dog" instead. FINDREP does that for the whole record: you say "find OLD, replace with NEW," and the computer does it. You can do several at once: find OLD1 replace NEW1, find OLD2 replace NEW2. We can do this before the sort (INREC) so the sort sees the new words, or only when writing the file (OUTREC) so the sort still saw the old words.

Exercises

  1. Write OUTREC FINDREP= to replace every occurrence of "N/A" with "NA " in the record.
  2. You need to sort by a code that sometimes appears as "X" and sometimes as "X ". Should you use FINDREP in INREC or OUTREC? Why?
  3. What is the difference between replacing C'123' with C'456' and replacing the numeric value 123 with 456?

Quiz

Test Your Knowledge

1. What is FINDREP in DFSORT?

  • A sort key
  • A find-and-replace feature: search for a specific string or value in the record and replace it with another; specified with FINDREP=(IN=...,OUT=...) in INREC or OUTREC
  • Only for OUTFIL
  • A DD name

2. How do you replace multiple different strings in one step?

  • Only one replacement per step
  • Specify multiple IN/OUT pairs: FINDREP=(IN=C'OLD1',OUT=C'NEW1',IN=C'OLD2',OUT=C'NEW2')
  • Use two SORT steps
  • Only with IFTHEN

3. When should you use FINDREP in INREC vs OUTREC?

  • Always OUTREC
  • Use INREC when the replaced value must be used for sorting or INCLUDE/OMIT—the sort and filter see the modified record. Use OUTREC when you only need the replacement in the final output
  • Always INREC
  • FINDREP only works in OUTREC

4. Can FINDREP replace a numeric value (e.g. 123 with 456)?

  • No, only character
  • Yes—syntax is product-dependent; some support IN=123,OUT=456 or similar for fixed-length numeric replacement. Check your manual for exact form (e.g. ZD, PD, or character representation)
  • Only in OUTFIL
  • Only with BUILD

5. Does FINDREP replace every occurrence of the string or only the first?

  • Only the first
  • Typically every occurrence in the record (or in the scope specified)—each IN/OUT pair is applied and may replace all matches; exact behavior is product-dependent
  • Only in the sort key
  • Only one replacement per record