Using SORT DATE Functions you can dynamically insert the date, like ‘yyyy-mm-dd’ or any format instead of a constant date like ‘2021-01-01’. There are multiple Date Functions by which you can reformat input dates. By using INREC, OUTREC, and OUTFIL statements to insert the current date or past date, or future date in a variety of formats and time in a variety of formats into your records.
Current Date Format | Format of Constant | Example of Constant |
DATE1 | C’yyyymmdd’ | C’20050621′ |
DATE1(c) | C’yyyycmmcdd’ | C’2005/06/21′ |
DATE2 | C’yyyymm’ | C’200506′ |
DATE2(c) | C’yyyycmm’ | C’2005/06′ |
DATE3 | C’yyyyddd’ | C’2005172′ |
DATE3(c) | C’yyyycddd’ | C’2005/172′ |
DATE4 | C’yyyy-mm-dd-hh.mm.ss’ | C’2005-06-21-16.52.45′ |
DATE4 | C’yyyy-mm-dd-hh.mm.ss’ | C’2005-06-21-16.52.45′ |
DATE5 | C’yyyy-mm-dd-hh.mm.ss.nnnnnn’ | C’2005-06-21-16.52.45.582013′ |
Both the DATE1(c) and DATE=(4MDc) operands correspond to a C’yyyycmmcdd’ constant for today’s date where c is any separator character you like except blank. So either of the following pairs of control statements will sort your records on input positions 1-6 and reformat them with today’s date in the form C’yyyy-mm-dd’ in output positions 1-10, and input positions 1-6 in output positions 11-16.
SORT FIELDS=(1,6,CH,A),FORMAT=CH OUTREC BUILD=(1:DATE1(-),11:1,6)
OR
SORT FIELDS=(1,6,CH,A),FORMAT=CH OUTREC BUILD=(1:DATE=(4MD-),11:1,6)
You could insert the current time as well as the current date in your records to produce a timestamp.
For example: OUTREC BUILD=(DATE3,TIME1,1,6) would produce a character timestamp in output positions 1-12 of the form: yyyydddhhmmss
More easily, you could use DATE4 to produce a timestamp of the form: yyyy-mm-dd-hh.mm.ss or DATE5 to produce a timestamp with microseconds of the form: yyyy-mm-dd-hh.mm.ss.nnnnnn
Date constants can be produced in a variety of other characters, zoned decimal and packed decimal formats as well such as C’yyyy-mm’, Z’yyyymmdd’ and P’yyddd’. Time constants can also be produced in a variety of other characters, zoned decimal and packed decimal formats as well such as C’hh:mm’, Z’hhmmssxx’ and P’hhmmss’.
If, as in the second question above, you wanted to produce just one record containing the date, you could select from a variety of date formats.
For example, if you wanted to create a record with just C’dddyy’, you could do it with OUTREC as follows:
//DATERCD EXEC PGM=ICEMAN //SYSOUT DD SYSOUT=* //SORTIN DD * DUMMY RECORD //SORTOUT DD DSN=... //SYSIN DD * OPTION COPY OUTREC BUILD=(YDDDNS=(DY)) /*
Include records with today’s date using SORT DATE Functions
Let’s say we have a C’yyyymmdd’ date field starting at position 10 of each record. Each day we want only the records for that day to be copied into the output file.
INCLUDE and OMIT statements can be used to select records using a variety of formats for today’s date like C’yyyymmdd’, C’yyyy/mm/dd’, +yyyymmdd, C’yyyyddd’, C’yyyy/ddd’, +yyyyddd, C’yymmdd’ and so on.
The DATE1 operand corresponds to a C’yyyymmdd’ constant for today’s date. So the following control statement will include only those records with a C’yyyymmdd’ date in positions 10-17 equal to today’s date:
INCLUDE COND=(27,8,CH,EQ,DATE1)
Some other examples:
- For date values in the form C’yyyy/mm/dd’, you could use the DATE1(/) constant;
- For date values in the form C’yyyy-mm’, you could use the DATE2(-) constant;
- For date values in the form P’yyyyddd’, you could use the DATE3P constant;
- For date values in the form Z’yymmdd’ (2-digit year date), you could use the Y’DATE1′ constant.
Of course, you can use the other comparison operators (NE, GT, GE, LT, LE) as well as EQ. For example, you could use GT to select records with dates after today, or LT to select records with dates before today.
Include records using relative dates using SORT DATE Functions
Let’s say we have a file with a date in a particular position and we want to select only records where the date is greater than the current or a particular date + or – N number of days and it can be 0 to 9999. Let’s assume N is 30 days.
You can use INCLUDE and OMIT to select records using a variety of formats for past and future dates like C’yyyymmdd’, C’yyyy/mm/dd’, +yyyymmdd, C’yyyyddd’, C’yyyy/ddd’, +yyyyddd, C’yymmdd’ and so on.
The DATE1(-)-30 operand corresponds to a C’yyyy-mm-dd’ constant for today’s date minus 30 days. So the following control statement will include only those records with a C’yyyy-mm-dd’ date in positions 14-23 greater than today’s date – 30 days.
INCLUDE COND=(14,10,CH,GT,DATE1(-)-30)
Some other examples:
- For ‘yyyymm’ + 3 months, you could use DATE2+3;
- For P’yyyyddd’ – 150 days, you could use DATE3P-150;
- For Z’mmddyy’ + 7 days, you could use Y’DATE1’+7.
Example
Input file 10 suresh 20000 01 20120203 20 NARENDRA 40000 06 20110925 30 jacob A 25000 07 20111018 40 RAMESH 34000 03 20120410 50 Kishore 50000 02 20130408
INCLUDE in SORT - selecting the records between the two dates. //SYSIN DD * SORT FIELDS=COPY INCLUDE COND=(22,8,ZD,LE,DATE1P+10,AND,22,8,ZD,GE,DATE1P-10) /*
Explanation – In the above example, the SORT card will select the records, if the date in the input
record is between the current date +/- 10days. Selected records will be copied to the output file.
Output file for SORT JCL – Assume the current date is – 4-Apr-2012.
Following records will be selected from the input file.
40 RAMESH 34000 03 20120410 50 Kishore 50000 02 20120408