Date conversion in DFSORT means changing a date field from one format to another—for example, from MMDDYYYY to YYYYMMDD, or from YYDDD (Julian) to YYYYMMDD (Gregorian). You can do this in INREC or OUTREC using format codes (Y2T, Y2W, Y4T, Y4W) and conversion operators (TOGREG, TOJUL). Doing the conversion in INREC is important when you need to sort by the converted date or filter on it with INCLUDE/OMIT—because INREC runs before the sort, the sort sees the new date. This page explains the format codes (year first vs year last, 2-digit vs 4-digit year), how to use TOGREG and TOJUL, building a sortable YYYYMMDD field in INREC, and century windowing for 2-digit years. The same syntax applies in OUTREC when you only need the converted date in the final output.
Dates on the mainframe appear in many forms: YYYYMMDD (8 bytes), YYYYDDD (Julian, 7 bytes), MMDDYYYY, DDMMYYYY, or YYMMDD with 2-digit year. To sort or filter by date, you usually want a single, consistent format—and often one where character order equals date order, such as YYYYMMDD. If the input has MMDDYYYY at positions 50–57, the character order of that string does not match chronological order (e.g. 12311999 vs 01012000). So you convert that field to YYYYMMDD and put it at a known position (e.g. at the start of the INREC output). Then SORT FIELDS=(1,8,CH,A) sorts by date correctly, and INCLUDE COND=(1,8,CH,GE,C'20230101') filters correctly. Because INREC runs before the sort, the converted date is in the record that the sort and INCLUDE/OMIT see. If you only converted in OUTREC, the sort would still see the original format.
DFSORT uses four main format codes to describe the source date field. Each code indicates whether the year is 2-digit or 4-digit and whether the year comes first or last.
| Code | Meaning | Example |
|---|---|---|
| Y2T | 2-digit year first: yymmdd or yyddd | 251015 = 15 Oct 2025 (if window 1940–2039) |
| Y2W | 2-digit year last: mmddyy or dddyy | 101525 = 15 Oct 2025 |
| Y4T | 4-digit year first: ccyymmdd or ccyyddd | 20251015 |
| Y4W | 4-digit year last: mmddccyy or dddccyy | 10152025 |
Y2 = 2-digit year (yy); Y4 = 4-digit year (ccyy). T = year first (e.g. yymmdd, ccyymmdd, yyddd, ccyyddd). W = year last (e.g. mmddyy, mmddccyy, dddyy, dddccyy). You must specify the format that matches your input data so the conversion interprets the bytes correctly.
After you specify the source field and its format, you use a conversion operator to say what format you want for the output.
So you might have: (81,8,Y4W,TOGREG=Y4T) — take 8 bytes at position 81, interpret as Y4W (mmddccyy), convert to Gregorian Y4T (ccyymmdd), and place the result in the output at the position specified (often with an output position prefix like 1: or 21:). Exact syntax can vary; see your DFSORT manual for BUILD vs FIELDS and placement.
Input has: bytes 1–80 data, bytes 81–88 date in MMDDYYYY (e.g. 12252024). You want to sort by date. Convert in INREC and put the YYYYMMDD field at the start of the reformatted record:
12INREC BUILD=(1:81,8,Y4W,TOGREG=Y4T,9:1,80) SORT FIELDS=(1,8,CH,A)
Here, 1:81,8,Y4W,TOGREG=Y4T means: take the 8-byte field at input position 81, interpret it as Y4W (mmddccyy), convert to Y4T (ccyymmdd), and place the result starting at output position 1. Then 9:1,80 copies input 1–80 to output positions 9–88. So the reformatted record has the 8-byte YYYYMMDD at 1–8 and the original 80 bytes at 9–88. SORT FIELDS=(1,8,CH,A) then sorts by that date. (Note: output-position syntax like 1: and 9: may vary by DFSORT version; refer to your product manual.)
If the source is Julian (e.g. YYDDD or YYYYDDD), use the matching source format (Y2T for yyddd, Y4T for ccyyddd) and convert to Gregorian with TOGREG=Y4T (or Y2T for 2-digit year output). Example: input has 7-byte YYYYDDD at position 50. Convert to 8-byte YYYYMMDD:
1INREC BUILD=(1:50,7,Y4T,TOGREG=Y4T,9:1,49,58:51,30)
Conceptually: source 50,7 as Y4T (ccyyddd), TOGREG=Y4T places 8-byte ccyymmdd at output 1–8; then copy input 1–49 to output 9–57, and input 51–80 to output 58–87. Again, exact placement syntax depends on your DFSORT version; the idea is to build the converted date where the sort can use it.
When the source date uses a 2-digit year (Y2T or Y2W), DFSORT applies a century window to decide whether yy means 19xx or 20xx. The default window (e.g. 1940–2039) means 40 → 1940, 99 → 1999, 00 → 2000, 39 → 2039. So 25 is interpreted as 2025. If your data has years outside that window (e.g. 1935 or 2045), the converted date can get the wrong century. Whenever possible, use 4-digit year source (Y4T, Y4W) so there is no ambiguity. If you must use 2-digit years, check your DFSORT documentation for options to set or change the century window.
The same date conversion operands (source format and TOGREG/TOJUL) are supported in OUTREC. Use OUTREC when you do not need to sort or filter by the converted date—you only want it in the final output. For example, you might sort by an existing key and then in OUTREC convert a date field to a display format (e.g. with separators) for a report. INREC is for when the converted date must participate in the sort or in INCLUDE/OMIT.
If the input has separate fields (e.g. month at 20–21, day at 22–23, year at 24–27), you can sometimes build YYYYMMDD manually with INREC by copying and reordering: e.g. year, month, day in that order. That is just field extraction and reordering (and possibly padding with zeros). When the input is already in a single field but in the wrong format (e.g. MMDDYYYY), use the date conversion (Y4W, TOGREG=Y4T) as above. When the input is in pieces, building with (position,length) may be simpler than conversion.
Imagine the date is written in different ways on different cards: one card says "12-25-2024" (month-day-year), another says "20241225" (year-month-day). To put the cards in order by date, we need them all in the same form—like year first, so when we sort the numbers, the oldest date comes first. Date conversion is when the computer changes the date from one form to another. We do it before sorting (in INREC) so that when the sort looks at the card, it already sees the date in the right form. So the computer rewrites the date into the format we want, then we sort by it.
1. Why convert a date in INREC instead of OUTREC when you need to sort by it?
2. What does Y4T mean in DFSORT date conversion?
3. What is TOGREG=Y4T in a BUILD specification?
4. What is the century window (Y2T) problem?
5. Can you use the same date conversion syntax in OUTREC?