MainframeMaster

Date Arithmetic

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.

Date & Time Processing
Progress0 of 0 lessons

Adding and Subtracting Days

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):

text
1
2
INREC 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.

PTF and Version Requirements

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.

Months and Years

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.

Date arithmetic operators (availability is product-dependent)
OperatorMeaning
ADDDAYS,+nAdd n days to the source date.
SUBDAYS,+nSubtract n days from the source date.
ADDMONS / SUBMONSAdd or subtract months.
ADDYEARS / SUBYEARSAdd or subtract years.
LASTDAYMLast day of the month containing the source date.
LASTDAYQLast day of the quarter.
LASTDAYYLast day of the year.
DATEDIFFNumber of days between two date fields.

Current Date Plus or Minus N Days (DATENS)

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.

INREC vs OUTREC

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.

Last Day of Month / Quarter / Year

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.

When Date Arithmetic Is Not Available

If your DFSORT does not support ADDDAYS/SUBDAYS or the operators you need:

  • Pre-step: Use a COBOL program (or REXX, etc.) to read the file, compute the new date (e.g. INTEGER-OF-DATE, add days, DATE-OF-INTEGER), and write the result to a new field. Then run DFSORT on that file.
  • Fixed offsets: If you only need a few fixed values (e.g. "today" and "today+30"), you can generate them once (e.g. in JCL or a small program) and use constants or a small lookup in DFSORT if your logic allows.
  • Post-step: Sort or copy with DFSORT first, then run a program that does the date arithmetic on the sorted output.

Explain It Like I'm Five

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.

Exercises

  1. You need "order date + 14 days" as a new field and want to sort by it. Do you use INREC or OUTREC for the arithmetic?
  2. What should you check if ADDDAYS gives a SYNTAX ERROR?
  3. Why might you use LASTDAYM on a date field?

Quiz

Test Your Knowledge

1. What does ADDDAYS do in DFSORT INREC or OUTREC?

  • Adds the current date
  • Adds a number of days to an existing date field in the record; you specify the source date (position, format) and the offset (e.g. +30)
  • Only works in OUTREC
  • Adds days to the sort key only

2. How do you put "today plus 30 days" (a future date from run date) in each record?

  • Use ADDDAYS on an input date field
  • Use DATENS=(+30,Y4T) (or similar) in OUTREC/INREC—DATENS is the current date with an offset; syntax is product-dependent
  • Only COBOL can do this
  • Use DATE= in HEADER only

3. Why might ADDDAYS or SUBDAYS give a SYNTAX ERROR?

  • Only OUTREC supports it
  • These functions may require a specific DFSORT PTF level (e.g. z/OS DFSORT V1R10 UK90025 or later); ICE201I H in SYSOUT indicates support
  • Only for Julian dates
  • Invalid date format

4. What is the purpose of LASTDAYM or LASTDAYQ in date arithmetic?

  • Last day of the week
  • LASTDAYM = last day of the month for the given date; LASTDAYQ = last day of the quarter—useful for period-end dates
  • Only for leap years
  • Same as SUBDAYS

5. When should you do date arithmetic in INREC vs OUTREC?

  • Always OUTREC
  • INREC when the computed date must be used for SORT FIELDS= or INCLUDE/OMIT; OUTREC when you only need it in the final output
  • Only INREC
  • Neither supports date arithmetic