COUNT function can be used to print messages containing the count of records in a data set. It can also be used to subtract a value from the count or add a value to the count, to create an output data set containing text and the count, or to set RC=12, RC=8, RC=4, or RC=0 based on meeting criteria for the number of records in a data set.
If the criteria are met (for example, HIGHER(20) is specified and the record count is 21 or more), ICETOOL sets the following return code for the COUNT operator:
- RC=12 if RC12 is specified, or by default if RC8 and RC4 are not specified
- RC=8 if RC8 is specified
- RC=4 if RC4 is specified
If the criteria are not met (for example, HIGHER(20) is specified and the record count is 20 or less), ICETOOL sets RC=0 for the COUNT operator.Â
COUNT FROM(DD2) HIGHER(20) RC4 USING(CTL2)
Sets RC=4 if more than 20 records are included from DD2, or sets RC=0 if 20 or fewer records are included from DD2.
COUNT FROM(DD1) EMPTY
Sets RC=12 if DD1 is empty, or sets RC=0 if DD1 is not empty.
COUNT Function Syntax
>>-COUNT--FROM(indd)--+-------------+--+-------------+----------> Â Â Â Â Â Â Â Â Â Â Â Â '-USING(xxxx)-'Â '-VSAMTYPE(x)-'Â Â Â >--+-----------------+--+------+--+-------------+--+--------+---> Â Â Â +-LOCALE(name)----+Â +-RC4--+Â +-EMPTY-------+Â +-SUB(q)-+Â Â Â Â Â Â +-LOCALE(CURRENT)-+Â +-RC8--+Â +-NOTEMPTY----+Â '-ADD(r)-'Â Â Â Â Â Â '-LOCALE(NONE)----'Â '-RC12-'Â +-HIGHER(x)---+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â +-LOWER(y)----+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â +-EQUAL(v)----+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â '-NOTEQUAL(w)-'Â Â Â Â Â Â Â Â Â >--+----------------+--+----------------+-----------------------> Â Â Â '-WRITE(countdd)-'Â '-TEXT('string')-'Â Â Â >--+---------------------+--+----------+----------------------->< Â Â Â +-DIGITS(d)-----------+Â '-WIDTH(n)-'Â Â Â Â Â Â '-EDCOUNT(formatting)-'Â Â Â Â Â Â Â Â Â Â
There are multiple ways to find the number of records in a file. One way is to add a sequence number field to the record. Or just use a COPY and look at the job output to see how many records SORT found or use the COUNT function of SORT.
Example 1:Count the number of records in the input file by using COUNT.
COUNT FROM(IN2) WRITE(CT2) TEXT('Count is ') -   EDCOUNT(A1,U10) WIDTH(100) Prints a message containing the count of records in the IN2 data set. Writes an 100-byte record with the specified string and an edited count to the CT2 data set. If IN2 contains 1234567 records, the 80-byte output record in CT2 would look like this: Output: Count is   1,234,567
Example 2:Count the number of records in the input file by using COUNT with the arithmetic operation.
COUNT FROM(IN3) WRITE(CT3) DIGITS(8) SUB(2) Subtracts 2 from the count of records in the IN3 data set. Prints a message containing the modified count. Writes a 8-byte record with the modified count to the CT3 data set. If IN3 contains 9999 records, the 8-byte output record in CT3 would look like this: Output: 00009999
Example 3: Count the number of records in the input file by using COUNT.
//STEP010 EXEC PGM=ICETOOL         //TOOLMSG DD SYSOUT=*          //DFSMSG  DD SYSOUT=*       //DD01   DD DSN=... input file  //       DISP=SHR     //TOOLIN  DD *   COUNT FROM(DD01)    /* Output: 00009999
Example 4:Count the number of records in the input file by using TRAILER and COUNT.
//STEP1 EXEC PGM=SORT //SYSOUT DD SYSOUT=* //SORTIN DD DSN=... input file //SORTOUT DD DSN=... output file //SYSIN DD *  SORT FIELDS=COPY  OUTFIL REMOVECC,NODETAIL,  TRAILER1=('Count of records: ',COUNT=(M11,LENGTH=8)) /* Output: Count of records: 00009999
Example 5: If you have two fields, one State field in positions 1-15 and City field in positions 16-30, and you want to count the number of records and store as a third field then use sequence number starting from 1 and increment by 1, you could use START=1 and INCR=1 as shown in the following statements:
//STEP1 EXEC PGM=SORT //SYSOUT DD SYSOUT=* //SORTIN DD DSN=... input file //SORTOUT DD DSN=... output file //SYSIN DD *  SORT FIELDS=COPY  OUTREC OVERLAY=(32:SEQNUM,5,ZD,START=1,INCR=1) /* Output XXXXXX       00001 YYYYYY       00002 …… …… ZZZZZZ       00026