Supported RPG Features

The LIKE keyword declares a variable with the same type, length, and precision as an existing variable:

DCL-S wPrice  PACKED(7:2);
DCL-S wTotal  LIKE(wPrice);         // wTotal is PACKED(7:2)

DCL-S wName   CHAR(30);
DCL-S wAlias  LIKE(wName);          // wAlias is CHAR(30)

An optional length adjustment can increase or decrease the target size:

DCL-S wBase   CHAR(10);
DCL-S wBigger LIKE(wBase : +5);     // CHAR(15)
DCL-S wSmaller LIKE(wBase : -3);    // CHAR(7)

The LIKE(ref : adjustment) second-parameter form is free-format only. In fixed-format D-specs the adjustment is instead a signed +n/-n value in the length columns (positions 33-39), alongside the LIKE(ref) keyword:

     D Base            S              5A
     D Bigger          S             +3    LIKE(Base)
     D Smaller         S             -2    LIKE(Base)

The adjustment changes the derived length by n characters (alpha) or digits (numeric). Using the colon form LIKE(ref : adjustment) in a fixed-format D-spec is rejected with a compile error - put the adjustment in the length columns instead. For integer (I) and unsigned (U) fields the adjusted digit count must remain a valid integer size (3, 5, 10, or 20); any other count (for example LIKE(int5Field) +1, which would be 6 digits) is rejected. A length adjustment applied to a type that has no adjustable length - float, date, time, timestamp, or pointer - is also rejected with a compile error.

LIKE supports all data types (PACKED, ZONED, CHAR, VARCHAR, INT, UNS, DATE, etc.) and resolves transitive chains: if field A is LIKE(B) and B is LIKE(C), A inherits the type of C.

LIKE may also be used on a procedure parameter to inherit the type from a module-level variable or a qualified data structure subfield. This works with both CONST and VALUE passing modes:

DCL-S gPrice PACKED(7:2);
DCL-S gName  CHAR(20);

DCL-PROC showInfo;
  DCL-PI *N;
    pPrice LIKE(gPrice) CONST;
    pName  LIKE(gName)  CONST;
  END-PI;
  DSPLY (%CHAR(pPrice) + ' ' + %TRIMR(pName));
END-PROC;

LIKE may also type a data-structure subfield, inheriting the referenced field's type, length, and decimal positions. The target may be a sibling subfield of the same non-qualified structure, or any field already in scope:

DCL-DS order;
  qty    PACKED(5:0);
  amount PACKED(9:2);
  total  LIKE(amount);              // total is PACKED(9:2)
END-DS;

The same length adjustment applies: LIKE(amount : +2) in free-format, or a signed +n/-n in the length columns of a fixed-format D-spec subfield alongside the LIKE(field) keyword. A subfield of a QUALIFIED structure cannot reference a bare sibling name - qualify the target as dsName.subfield or reference a field declared outside the structure.