SAMEPOS(subfield)

The SAMEPOS keyword is used in a definition to specify that the starting position of the subfield is the same as the subfield named in the parameter.

The parameter must be a subfield that is previously specified in the data structure, at the same level.

A data structure with several subfields defined with SAMEPOS

In the following example, several subfields are defined using the SAMEPOS keyword.
  1. Subfield A is in positions 1 to 10.
  2. Subfield B is in position 11.
  3. Subfield C is defined with keyword SAMEPOS(B), so it starts in position 11, the same as B. It ends in position 30.
  4. Data structure subfield SUB_DS starts in position 31, the next position in the data structure.
  5. Subfield N is in positions 1 to 5 of data structure subfield SUB_DS.
  6. Subfield X1 is in position 6 of data structure subfield SUB_DS.
  7. Subfield X2 is in position 7 of data structure subfield SUB_DS.
  8. Subfield X3 is in position 8 of data structure subfield SUB_DS.
  9. Array subfield ARR_X is defined with keyword SAMEPOS(X1), so it is in positions 6 to 8 of data structure subfield SUB_DS. The array overlays subfields X1, X2, and X3.
  10. Subfield ERROR_1 is defined with keyword SAMEPOS(a). This is an error, because subfield A is not in data structure subfield SUB_DS.
  11. Subfield ERROR_2 is defined with keyword SAMEPOS(last). This is an error, because subfield LAST is not specified before subfield ERROR_2.

DCL-DS ds1 QUALIFIED;
   a CHAR(10);                          //  1 
   b CHAR(1);                           //  2 
   c CHAR(20) SAMEPOS(b);               //  3 
   DCL-DS sub_ds;                       //  4 
      n  CHAR(5);                       //     5 
      x1 CHAR(1);                       //     6 
      x2 CHAR(1);                       //     7 
      x3 CHAR(1);                       //     8 
      arr_x CHAR(1) DIM(3) SAMEPOS(x1); //     9 
      error_1 CHAR(1) SAMEPOS(a);       //     10 
      error_2 CHAR(1) SAMEPOS(last);    //     11 
      last CHAR(1);
   END-DS;
END-DS;

Defining an array over several repeated fields in an externally-described data structure

In the following example, file SALESFILE has four fields SALESQ1, SALESQ2, SALESQ3 AND SALESQ4, all defined the same way, with no intervening fields.

     A          R REC
     A            AGENT         50A
     A            SALESQ1        9P 2
     A            SALESQ2        9P 2
     A            SALESQ3        9P 2
     A            SALESQ4        9P 2
     A            SALESTOTAL    10P 2
The SALES array subfield is positioned over the SALESQ1, SALESQ2, SALESQ3, and SALESQ4 subfields.

DCL-DS ds EXTNAME('SALESFILE');
   sales LIKE(salesq1) DIM(4) SAMEPOS(salesq1);
END-DS;
DCL-S i INT(10);
FOR i to %ELEM(sales);
   DSPLY sales(i);
ENDFOR;

Defining an array subfield over several repeated data structure subfields

The following example defines several identical data structure subfields, followed by an array of the same data structure, using the SAMEPOS keyword to position the array at the same starting position as the first subfield.

DCL-DS info QUALIFIED;
   other_subfield CHAR(11);
   DCL-DS info1;
      age INT(10) INZ(5);
      name CHAR(10) INZ('Mary');
   END-DS;
   DCL-DS info2;
      age INT(10) INZ(7);
      name CHAR(10) INZ('Tom');
   END-DS;
   DCL-DS info3;
      age INT(10) INZ(6);
      name CHAR(10) INZ('Sally');
   END-DS;
   DCL-DS info_arr DIM(3) SAMEPOS(info1);
      age INT(10);
      name CHAR(10);
   END-DS;
END-DS;
DCL-S i INT(10);

FOR i = 1 TO %ELEM(info_arr);
   DSPLY ('Name: ' + info.info_arr(i).name + ' '
        + 'Age: ' + %char(info.info_arr(i).age));
ENDFOR;