LIKE(name {: length-adjustment})

The LIKE keyword is used to define an item like an existing one. For information about using LIKE with an object, see LIKE(object-name).

When the LIKE keyword is specified, the item being defined takes on the length and the data format of the item specified as the parameter. Standalone fields, prototypes, parameters, and data-structure subfields may be defined using this keyword. The parameter of LIKE can be a standalone field, a data structure, a data structure subfield, a parameter in a procedure interface definition, or a prototype name. The data type entry (position 40) must be blank.

This keyword is similar to the *LIKE DEFINE operation code (see *LIKE DEFINE). However, it differs from *LIKE DEFINE in that the defined data takes on the data format and CCSID as well as the length.
Note: Attributes such as ALTSEQ(*NONE), NOOPT, ASCEND, CONST and null capability are not inherited from the parameter of LIKE by the item defined. Only the data type, length, decimal positions, and CCSID are inherited.

If the parameter of LIKE is a prototype, then the item being defined will have the same data type as the return value of the prototype. If there is no return value, then an error message is issued.

The length of the item being defined can be adjusted. You specify the length adjustment in the second parameter of the LIKE keyword in free-form definitions, or the Length entry in fixed-form definitions. The length adjust must be specified with either a positive (+) or negative (-) sign.

Here are some considerations for using the LIKE keyword with different data types:
  • For character fields, the length adjustment is the number of additional (or fewer) characters.
  • For numeric fields, the length adjustment is the number of additional (or fewer) digits. For integer or unsigned fields, adjustment values must be such that the resulting number of digits for the field are 3, 5, 10, or 20. For float fields, length adjustment is not allowed.
  • For graphic or UCS-2 fields, the length adjustment is the number of additional (or fewer) graphic or UCS-2 characters (1 graphic or UCS-2 character = 2 bytes).
  • For date, time, timestamp, basing pointer, or procedure pointer fields, length adjustment is not allowed.

When LIKE is used to define an array, the DIM keyword is still required to define the array dimensions. However, DIM(%elem(array)) can be used to define an array exactly like another array.

Use LIKEDS to define a data structure like another data structure, with the same subfields.

Examples of defining data using the LIKE keyword

The following examples are shown first in free-form and then in fixed-form.

  1. Field Long_name is defined like field Name with a length increase of 5 characters.
  2. Subfield array NameList is defined like field Name. Each array element is initialized with the value *ALL'X'.
  3. Prototype GetBonus is defined like field Salary with a length decrease of 2 digits.
Figure 1. Defining fields LIKE other fields in Free Form

 DCL-S Name CHAR(20);
 DCL-S Long_name LIKE(Name : +5);  1 

 DCL-DS Struct;
    NameList LIKE(Name) DIM(20) INZ(*ALL'X');  2 
 END-DS;

 DCL-PR GetBonus LIKE(Salary : -2);  3 
    Employee_Id INT(10) VALUE;
 END-PR;
Figure 2. Defining fields LIKE other fields in Fixed Form
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++
D.....................................Keywords+++++++++++++++++++++++++++++
D  Name           S             20
D  Long_name      S             +5    LIKE(Name)  1 

D  Struct         DS
D   NameList                          LIKE(Name) DIM(20) INZ(*ALL'X')  2 

D  GetBonus       PR         -2    LIKE(Salary)  3 
D   Employee_Id                 10I 0 VALUE