MainframeMaster

Field Insertion

Field insertion in DFSORT INREC means placing literal data—data that does not come from the input record—into the reformatted record. You do this with constants: C'...' for character data and X'...' for hexadecimal data. Constants are listed in INREC FIELDS= or BUILD= along with (position,length) extraction items; they are inserted in the output at that position in the sequence. So you can add delimiters (e.g. comma or pipe) between fields, pad with spaces or zeros to fixed lengths, add a fixed prefix (e.g. record type code), or fill gaps so that every record has a uniform layout. This page explains character and hex constants, where and why to insert them, how they affect output length, and how to combine insertion with field extraction. Field insertion is essential for building fixed-format or delimited layouts before the sort.

INREC Processing
Progress0 of 0 lessons

What Is Field Insertion?

When you insert a field in INREC, you are adding bytes to the output record that are not copied from the input. Those bytes are fixed values you specify: a space, a comma, a type code like 'D' for detail, or a string of zeros for padding. In INREC you mix these with (position,length) extractions. The result is a record that contains both extracted data (from the input) and inserted data (constants). Insertion lets you add delimiters, align columns to fixed widths, or ensure every record has the same structure (e.g. a leading byte or a trailing filler) before the sort.

Character Constants: C'...'

C'literal' means: insert the characters between the quotes into the output, encoded in EBCDIC (the mainframe character set). The length of the constant is the number of characters. Examples:

  • C' ' — One space. Often used as a delimiter or padding.
  • C', ' — Comma followed by space (2 bytes).
  • C'|' — One pipe character (1 byte).
  • C'HDR' — The three characters H, D, R (3 bytes). Useful as a record type prefix.
  • C'00000' — Five zero characters (5 bytes). Useful for zero-padding.

Any printable character (and many non-printable ones) can be in a C'...' literal. The quotes delimit the literal; to include a single quote inside the literal, you typically double it or use the appropriate escape per your DFSORT documentation.

Hexadecimal Constants: X'...'

X'hex' means: each pair of hexadecimal digits forms one byte. So the length of the constant is half the number of hex digits. Examples:

  • X'40' — One byte: 0x40. In EBCDIC this is the space character. So X'40' is equivalent to C' '.
  • X'C1C2C3' — Three bytes: EBCDIC for A, B, C. So X'C1C2C3' is equivalent to C'ABC'.
  • X'F0F0F0' — Three bytes: EBCDIC for 0, 0, 0 (numeric zeros).
  • X'00' — One null byte. Useful for binary or fixed-length fields that must be zero.

Hex constants are useful when you need a non-printable character (e.g. X'0D' for carriage return in some contexts) or when you want to specify the exact byte value. You must use an even number of hex digits.

Constant types
TypeMeaningExample
C'literal'Character constant; characters in EBCDICC' ' = one space
X'hex'Hexadecimal constant; each pair = one byteX'40' = EBCDIC space

Where to Insert Constants

Constants can appear anywhere in the INREC FIELDS= or BUILD= list: at the beginning, between extracted segments, or at the end. The order of items is the order in the output. Examples:

Prefix: Insert a record type at the start.

text
1
INREC BUILD=(C'D',1,80)

Output: 1 byte 'D', then 80 bytes from input 1–80. Total 81 bytes. So every record starts with 'D' (e.g. for "detail") and you could sort or filter on position 1.

Delimiter between fields:

text
1
INREC BUILD=(1,10,C',',11,30,C'|',41,20)

Output: 10 bytes (input 1–10), comma, 30 bytes (input 11–40), pipe, 20 bytes (input 41–60). Total 10+1+30+1+20 = 62 bytes. The constants act as separators between extracted segments.

Trailing padding:

text
1
INREC BUILD=(1,50,C' ')

Output: 50 bytes from input 1–50, then 5 spaces. Total 55 bytes. Useful to pad the record to a fixed length (e.g. 80) or to clear a reserved area.

Output Length with Insertion

The output record length is the sum of: (1) the length of every (position,length) segment, and (2) the length of every constant. For C'...', length = number of characters. For X'...', length = number of hex digits divided by 2. So BUILD=(1,10,C'ABC',11,5) produces 10+3+5 = 18 bytes.

Why Insert Fields?

Common reasons to insert constants in INREC: (1) Delimiters — Comma, pipe, or tab between fields for downstream parsing or reporting. (2) Fixed-length padding — Spaces or zeros so that a field always occupies the same number of bytes (e.g. 10-byte name field). (3) Record type or prefix — A leading byte or code (e.g. 'H' for header, 'D' for detail) so the sort or a later program can identify the record type. (4) Alignment — So columns line up in a fixed-format report or so SORT FIELDS positions are predictable. (5) Filler — Reserved areas filled with spaces or zeros for future use.

Combining Extraction and Insertion

In practice you almost always combine (position,length) extraction with constants. Extract the input segments you need and insert constants where you need delimiters or padding. Example: build a fixed 80-byte output with ID (1–8), 2 spaces, name (9–38), 2 spaces, amount (39–46), and the rest spaces to 80:

text
1
INREC BUILD=(1,8,C' ',9,30,C' ',39,8,C' ')

That gives 8+2+30+2+8+10 = 60 bytes. To reach 80 you would add more C' ' or use a longer space constant. The idea is the same: extracted segments plus inserted constants in one BUILD= list.

Explain It Like I'm Five

When you copy parts from the big worksheet onto a small one, sometimes you want to add something that wasn't on the big sheet—like a comma between two numbers or a label at the top that says "Detail." Field insertion is like that: you tell the computer "put this exact thing here"—a space, a comma, or the letter D. So the small sheet has both the pieces you copied (extraction) and the pieces you wrote in yourself (insertion). That way every small sheet has the same layout, with commas or spaces in the right places.

Exercises

  1. Write INREC BUILD= to produce: 5 bytes from input 1–5, then a comma, then 10 bytes from input 20–29. What is the total output length?
  2. How do you insert exactly 10 spaces in INREC? Give both C'...' and X'...' forms (for 10 spaces in EBCDIC).
  3. You want every record to start with the two characters 'TX' (record type) and then the full 80-byte input. Write the INREC BUILD= and give the output length.
  4. What is the difference between C'0' and X'F0' in EBCDIC? Are they the same when inserted?

Quiz

Test Your Knowledge

1. What does C'ABC' in INREC BUILD do?

  • Copies bytes 1–3 from input
  • Inserts the three characters A, B, C into the output at that position
  • Sorts by ABC
  • Skips 3 bytes

2. What is X'40' in EBCDIC?

  • Character 4
  • Space character
  • Null
  • Newline

3. INREC BUILD=(1,10,C'|',11,20) produces what output layout?

  • 10 bytes, pipe, 20 bytes — total 31
  • 10 bytes from input 1–10, then pipe, then 20 bytes from input 11–30 — total 31 bytes
  • Pipe only
  • 10 bytes then 20 bytes with no pipe

4. Can you insert a constant at the beginning of the INREC output?

  • No
  • Yes—list the constant as the first item, e.g. BUILD=(C'HDR',1,80)
  • Only with OVERLAY
  • Only in OUTREC

5. Why insert spaces or zeros between fields in INREC?

  • INREC requires it
  • To pad fields to fixed lengths, align columns, or create gaps for later overlay
  • Spaces improve sort speed
  • Only for reports