CHKFMT: Checking the Format of a String

How to:

The CHKFMT function checks a character string for incorrect characters or character types. It compares each character string to a second string, called a mask, by comparing each character in the first string to the corresponding character in the mask. If all characters in the character string match the characters or character types in the mask, CHKFMT returns the value 0. Otherwise, CHKFMT returns a value equal to the position of the first character in the character string not matching the mask.

If the mask is shorter than the character string, the function checks only the portion of the character string corresponding to the mask. For example, if you are using a four-character mask to test a nine-character string, only the first four characters in the string are checked; the rest are returned as a no match with CHKFMT giving the first non-matching position as the result.


Top of page

x
Syntax: How to Check the Format of a Character String
CHKFMT(numchar, source_string, 'mask', output)

where:

numchar

Integer

Is the number of characters being compared to the mask.

string

Alphanumeric

Is the character string to be checked enclosed in single quotation marks, or a field or variable that contains the character string.

'mask'

Alphanumeric

Is the mask, which contains the comparison characters enclosed in single quotation marks.

Some characters in the mask are generic and represent character types. If a character in the string is compared to one of these characters and is the same type, it matches. Generic characters are:

A is any letter between A and Z (uppercase or lowercase).

9 is any digit between 0–9.

X is any letter between A–Z or any digit between 0-9.

$ is any character.

Any other character in the mask represents only that character. For example, if the third character in the mask is B, the third character in the string must be B to match.

output

Integer

Is the name of the field that contains the result, or the format of the output value enclosed in single quotation marks.



Example: Checking the Format of a Field

CHKFMT examines EMP_ID for nine numeric characters starting with 11 and stores the result in CHK_ID:

TABLE FILE EMPLOYEE
PRINT EMP_ID AND LAST_NAME AND
COMPUTE CHK_ID/I3 = CHKFMT(9, EMP_ID, '119999999', CHK_ID);
WHERE DEPARTMENT EQ 'PRODUCTION';
END

The output is:

EMP_ID     LAST_NAME     CHK_ID
------     ---------     ------
071382660  STEVENS            1
119265415  SMITH              0
119329144  BANNING            0
123764317  IRVING             2
126724188  ROMANS             2
451123478  MCKNIGHT           1


Example: Checking the Format of a Field With MODIFY on z/OS

The following MODIFY procedure adds records of new employees to the EMPLOYEE data source. Each transaction begins as an employee ID that is alphanumeric with the first five characters as digits. The procedure rejects records with other characters in the employee ID.

MODIFY FILE EMPLOYEE
PROMPT EMP_ID LAST_NAME FIRST_NAME DEPARTMENT
MATCH EMP_ID
   ON MATCH REJECT
   ON NOMATCH COMPUTE
      BAD_CHAR/I3 = CHKFMT(5, EMP_ID, '99999', BAD_CHAR);
   ON NOMATCH VALIDATE
      ID_TEST = IF BAD_CHAR EQ 0 THEN 1 ELSE 0;
      ON INVALID TYPE
         "BAD EMPLOYEE ID: <EMP_ID"
         "INVALID CHARACTER IN POSITION <BAD_CHAR"
   ON NOMATCH INCLUDE
   LOG INVALID MSG OFF
DATA

A sample execution is:

>
 EMPLOYEEFOCUS   A ON 12/05/96 AT 15.42.03
 DATA FOR TRANSACTION    1
 EMP_ID      =
111w2
 LAST_NAME   =
johnson
 FIRST_NAME  =
greg
 DEPARTMENT  =
production
 BAD EMPLOYEE ID: 111W2
 INVALID CHARACTER IN POSITION   4
 DATA FOR TRANSACTION    2
 EMP_ID      =
end
 TRANSACTIONS:         TOTAL =     1  ACCEPTED=     0  REJECTED=     1
 SEGMENTS:             INPUT =     0  UPDATED =     0  DELETED =     0
>

The procedure processes as follows:

  1. The procedure searches the data source for the ID 111w2. If it does not find this ID, it continues processing the transaction.
  2. CHKFMT checks the ID against the mask 99999, which represents five digits.
  3. The fourth character in the ID, the letter w, is not a digit. The function returns the value 4 to the BAD_CHAR field.
  4. The VALIDATE command tests the BAD_CHAR field. Since BAD_CHAR is not equal to 0, the procedure rejects the transaction and displays a message indicating the position of the invalid character in the ID.

Information Builders