How to create your own type definitions and reuse them


You can create a simple or complex type definition that can be consistently and easily reused with other variables without having to copy and paste the contents of the original one.

This is done using the TYPEDEF clause in the data description.

 

Here's an example of a record that holds a time structure to be reused with many time-type variables. You can create the type definition in the working-storage like this:

    01 td-time      typedef.
       05 ws-hrs    pic 99.
       05 filler    pic x value ":".
       05 ws-mins   pic 99.
       05 filler    pic x value ":".
       05 ws-secs   pic 99.

You would define the actual variables based on that definition, like this:

    01 init-time    usage td-time.
    01 end-time     usage td-time.
    01 first-time   usage td-time.

This way a variable will contain the 3 children fields: ws-hrs, ws-mins and ws-secs every time.

You can then use them in the procedure division, as the following snippet demonstrates:

    move 13 to ws-hrs  of init-time
    move 20 to ws-mins of init-time
    move 40 to ws-secs of init-time
 
    move init-time     to end-time
    add 5 to ws-hrs    of end-time
    add 3 to ws-mins   of end-time

    move end-time      to first-time
    add 4 to ws-mins   of first-time
    add 6 to ws-secs   of first-time

    display "Init  time: " init-time  x"0d0a"
            "End   time: " end-time   x"0d0a"
            "First time: " first-time x"0d0a"

That would display the following on the console:

    Init  time: 13:20:40
    End   time: 18:23:40
    First time: 18:27:46


Article ID: 304
Created: August 8, 2019
Last Updated: December 17, 2025
Author: Support KB Author

Online URL: https://support.veryant.com/phpkb/article.php?id=304