Date arithmetic in DFSORT means adding or subtracting days, months, or years from a date field, or computing derived dates such as the last day of the month or quarter. Some DFSORT versions support ADDDAYS, SUBDAYS, ADDMONS, SUBMONS, ADDYEARS, SUBYEARS, and related operators in INREC and OUTREC. There may also be support for "current date plus or minus n days" (e.g. DATENS) and for DATEDIFF (days between two dates). Availability and exact syntax depend on your DFSORT PTF level. This page explains the main ideas: add/subtract days (or months/years), when to use INREC vs OUTREC, and what to do when these features are not available.
When your DFSORT level supports it, you can add or subtract a number of days from a date field in the record. You specify: the source date (position, length, and format such as Y4T or Y4W), the arithmetic operator (ADDDAYS or SUBDAYS), the offset (e.g. +30 or +7), and the target format (e.g. TOGREG=Y4T). The result is placed at the output position you define in the BUILD or OVERLAY.
Example: input has an 8-byte YYYYMMDD date at position 50. You want a new field "date + 30 days" at the start of the record for sorting (e.g. to sort by due date):
12INREC BUILD=(1:50,8,Y4T,ADDDAYS,+30,TOGREG=Y4T,9:1,49,59:51,30) SORT FIELDS=(1,8,CH,A)
Here 1:50,8,Y4T,ADDDAYS,+30,TOGREG=Y4T reads the date at 50, adds 30 days, and writes the 8-byte YYYYMMDD result at output position 1. The rest of the record is copied. To subtract 7 days, use SUBDAYS,+7 (or the syntax your product uses for subtraction). Exact operand order and punctuation (e.g. +30 vs 30) can vary; check your DFSORT manual.
ADDDAYS and SUBDAYS are not available in all DFSORT releases. Some documentation states that support is present when the SYSOUT shows ICE201I H (or similar), corresponding to a specific z/OS DFSORT PTF (e.g. V1R10 UK90025 or later). If you specify ADDDAYS or SUBDAYS and get a SYNTAX ERROR, your installation may not have the required PTF. In that case you have two options: (1) request the PTF if your site allows it, or (2) perform the date arithmetic in a separate step (e.g. a COBOL program that uses INTEGER-OF-DATE and DATE-OF-INTEGER, or a REXX exec) and then run DFSORT on the result.
Some DFSORT versions also support ADDMONS, SUBMONS, ADDYEARS, and SUBYEARS. These work conceptually like ADDDAYS/SUBDAYS but add or subtract whole months or years. Month arithmetic can be tricky (e.g. Jan 31 + 1 month): behavior (e.g. end-of-month normalization) is product-dependent. Check your manual for exact syntax and semantics.
| Operator | Meaning |
|---|---|
| ADDDAYS,+n | Add n days to the source date. |
| SUBDAYS,+n | Subtract n days from the source date. |
| ADDMONS / SUBMONS | Add or subtract months. |
| ADDYEARS / SUBYEARS | Add or subtract years. |
| LASTDAYM | Last day of the month containing the source date. |
| LASTDAYQ | Last day of the quarter. |
| LASTDAYY | Last day of the year. |
| DATEDIFF | Number of days between two date fields. |
Sometimes you need the run date (today) plus or minus a fixed number of days in each record—for example "today + 30" as a default due date. Some DFSORT versions support a keyword such as DATENS=(+n, format) or DATENS=(-n, format) in OUTREC (or INREC) to write the current date plus n days (or minus n for past) at a given position. Example: OUTREC FIELDS=(1,20,21:DATENS=(+30,Y4T)) might place "today + 30 days" in YYYYMMDD form starting at position 21. The exact keyword (DATENS or another) and format codes are product-dependent; see your documentation.
The same date arithmetic syntax, when supported, typically works in both INREC and OUTREC. Use INREC when the computed date must participate in SORT FIELDS= or INCLUDE/OMIT—because INREC runs before the sort, the sort and filter see the new field. Use OUTREC when you only need the computed date in the final output (e.g. a report column) and do not need to sort or filter on it.
Operators like LASTDAYM, LASTDAYQ, and LASTDAYY take a source date and produce the last day of that month, quarter, or year. For example, from 20240215 you get 20240229 (in a leap year) for LASTDAYM, and 20240331 for LASTDAYQ if the quarter is Jan–Mar. These are useful for period-end reporting. Syntax and availability depend on your DFSORT version.
If your DFSORT does not support ADDDAYS/SUBDAYS or the operators you need:
Imagine you have a calendar and someone says "what date is 30 days after this one?" You count 30 days forward and point to the new date. Date arithmetic in DFSORT is the computer doing that: it looks at a date in the record, adds (or subtracts) a number of days (or months or years), and writes the new date somewhere. So each record can get its own "due date" or "expiry date" without a separate program—if your DFSORT has that feature. If not, we use another program to do the counting and then sort the result.
1. What does ADDDAYS do in DFSORT INREC or OUTREC?
2. How do you put "today plus 30 days" (a future date from run date) in each record?
3. Why might ADDDAYS or SUBDAYS give a SYNTAX ERROR?
4. What is the purpose of LASTDAYM or LASTDAYQ in date arithmetic?
5. When should you do date arithmetic in INREC vs OUTREC?