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.
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.
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:
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.
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:
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.
| Type | Meaning | Example |
|---|---|---|
| C'literal' | Character constant; characters in EBCDIC | C' ' = one space |
| X'hex' | Hexadecimal constant; each pair = one byte | X'40' = EBCDIC space |
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.
1INREC 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:
1INREC 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:
1INREC 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.
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.
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.
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:
1INREC 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.
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.
1. What does C'ABC' in INREC BUILD do?
2. What is X'40' in EBCDIC?
3. INREC BUILD=(1,10,C'|',11,20) produces what output layout?
4. Can you insert a constant at the beginning of the INREC output?
5. Why insert spaces or zeros between fields in INREC?