Cobol Programming Guide V4R2
Cobol Programming Guide V4R2
Programming Guide
V ersion 4 Release 2
SC23-8529-01
Programming Guide
V ersion 4 Release 2
SC23-8529-01
Note! Before using this information and the product it supports, be sure to read the general information under Notices on page 835.
Second Edition (August 2009) This edition applies to Version 4 Release 2 of IBM Enterprise COBOL for z/OS (program number 5655-S71) and to all subsequent releases and modifications until otherwise indicated in new editions. Make sure that you are using the correct edition for the level of the product. You can order publications online at www.ibm.com/shop/publications/order/, or order by phone or fax. IBM Software Manufacturing Solutions takes publication orders between 8:30 a.m. and 7:00 p.m. Eastern Standard Time (EST). The phone number is (800)879-2755. The fax number is (800)445-9269. You can also order publications through your IBM representative or the IBM branch office serving your locality. Copyright International Business Machines Corporation 1991, 2009. US Government Users Restricted Rights Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
Contents
Tables . . . . . . . . . . . . . . . xiii Preface . . . . . . . . . . . . . . xv
About this information . . . . . . . . . . xv How this document will help you . . . . . . xv Abbreviated terms . . . . . . . . . . . xv Comparison of commonly used terms . . . . xvi How to read syntax diagrams . . . . . . . xvi How examples are shown . . . . . . . . xviii Accessing softcopy documentation and support information. . . . . . . . . . . . . . xviii Summary of changes . . . . . . . . . . . xix Version 4 Release 2 (August 2009) . . . . . xix Version 4 Release 1 (December 2007) . . . . . xix How to send your comments . . . . . . . . xxi Accessibility . . . . . . . . . . . . . . xxi Interface information . . . . . . . . . . xxi Keyboard navigation . . . . . . . . . . xxi Accessibility of this information . . . . . . xxii IBM and accessibility. . . . . . . . . . xxii Examples: initializing data items . . . . . . Initializing a structure (INITIALIZE) . . . . . Assigning values to elementary data items (MOVE) . . . . . . . . . . . . . . Assigning values to group data items (MOVE) . Assigning arithmetic results (MOVE or COMPUTE) . . . . . . . . . . . . . Assigning input from a screen or file (ACCEPT) Displaying values on a screen or in a file (DISPLAY) Displaying data on the system logical output device . . . . . . . . . . . . . . . Using WITH NO ADVANCING . . . . . . Using intrinsic functions (built-in functions) . . . Using tables (arrays) and pointers . . . . . . . Storage and its addressability . . . . . . . . Settings for RMODE . . . . . . . . . . . Storage restrictions for passing data . . . . . Location of data areas . . . . . . . . . . Storage for LOCAL-STORAGE data . . . . . Storage for external data . . . . . . . . . Storage for QSAM input-output buffers . . . . 30 32 34 35 36 37 38 39 39 40 41 42 42 43 43 43 44 44
constants . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .
25 25 26 27 28 28 29
iii
Arithmetic comparisons (relation conditions) Examples: fixed-point and floating-point evaluations . . . . . . . . . . . Using currency signs . . . . . . . . . Example: multiple currency signs . . . .
. . . .
. 65 . 66 . 67 . 68
Transforming to reverse order (REVERSE) . Converting to numbers (NUMVAL, NUMVAL-C) . . . . . . . . . . Converting from one code page to another Evaluating data items (intrinsic functions) . . Evaluating single characters for collating sequence . . . . . . . . . . . . Finding the largest or smallest data item . Finding the length of data items . . . . Finding the date of compilation . . . .
. . . . . . . .
. 77 . 79 . 80 81 . 82 . 83 . 84 . 84 . 85 . 86 . 87
. 113
iv
Defining line-sequential files and records in COBOL . . . . . . . . . . . . . . . Allowable control characters . . . . . . . Describing the structure of a line-sequential file Defining and allocating line-sequential files . . . Coding input-output statements for line-sequential files . . . . . . . . . . . . . . . . Opening line-sequential files . . . . . . . Reading records from line-sequential files . . . Adding records to line-sequential files . . . . Closing line-sequential files. . . . . . . . Handling errors in line-sequential files . . . . .
207 208 208 209 209 210 210 211 211 212
213
214 214 215 215 216 217 218 218 219 219 220 220 221 222 223 223 224 224 225 225 226 226 227 228 229 230 231 231 231 232
179
180 181 182 182 184 185 185 187 189 189 192 193 193 194 194 194 195 196 196 197 197 198 200 202 203
VSAM files . . . . . . . . . . . . . . Defining VSAM file organization and records . . Specifying sequential organization for VSAM files . . . . . . . . . . . . . . . Specifying indexed organization for VSAM files Specifying relative organization for VSAM files Specifying access modes for VSAM files . . . Defining record lengths for VSAM files. . . . Coding input and output statements for VSAM files . . . . . . . . . . . . . . . . File position indicator . . . . . . . . . Opening a file (ESDS, KSDS, or RRDS) . . . . Reading records from a VSAM file . . . . . Updating records in a VSAM file . . . . . . Adding records to a VSAM file . . . . . . Replacing records in a VSAM file. . . . . . Deleting records from a VSAM file . . . . . Closing VSAM files . . . . . . . . . . Handling errors in VSAM files . . . . . . . Protecting VSAM files with a password . . . . Example: password protection for a VSAM indexed file . . . . . . . . . . . . . Working with VSAM data sets under z/OS and z/OS UNIX . . . . . . . . . . . . . . Defining VSAM files . . . . . . . . . . Creating alternate indexes . . . . . . . . Allocating VSAM files . . . . . . . . . Sharing VSAM files through RLS . . . . . . Improving VSAM performance . . . . . . .
Sort and merge process . . . . . . . . . . Describing the sort or merge file . . . . . . . Describing the input to sorting or merging . . . Example: describing sort and input files for SORT . . . . . . . . . . . . . . . Coding the input procedure . . . . . . . . Describing the output from sorting or merging . . Coding the output procedure . . . . . . . . Example: coding the output procedure when using DFSORT . . . . . . . . . . . . Restrictions on input and output procedures . . . Defining sort and merge data sets . . . . . . Sorting variable-length records . . . . . . . Requesting the sort or merge . . . . . . . . Setting sort or merge criteria . . . . . . . Example: sorting with input and output procedures . . . . . . . . . . . . . Choosing alternate collating sequences . . . . Sorting on windowed date fields . . . . . . Preserving the original sequence of records with equal keys . . . . . . . . . . . . . Determining whether the sort or merge was successful . . . . . . . . . . . . . . Stopping a sort or merge operation prematurely Improving sort performance with FASTSRT . . . FASTSRT requirements for JCL . . . . . . FASTSRT requirements for sort input and output files . . . . . . . . . . . . . Checking for sort errors with NOFASTSRT . . . Controlling sort behavior . . . . . . . . . Changing DFSORT defaults with control statements . . . . . . . . . . . . . Allocating storage for sort or merge operations Allocating space for sort files . . . . . . . Using checkpoint/restart with DFSORT . . . . Sorting under CICS . . . . . . . . . . . CICS SORT application restrictions . . . . .
Contents
Using file status keys . . . . . . . . . Example: file status key . . . . . . . . Using VSAM status codes (VSAM files only) Example: checking VSAM status codes . . . Coding INVALID KEY phrases . . . . . Example: FILE STATUS and INVALID KEY . Handling errors when calling programs . . . Writing routines for handling errors . . . . .
Setting environment variables under z/OS UNIX Specifying compiler options under z/OS UNIX . Compiling and linking with the cob2 command Creating a DLL under z/OS UNIX . . . . Example: using cob2 to compile and link under z/OS UNIX . . . . . . . . . . . . cob2 syntax and options . . . . . . . . cob2 input and output files . . . . . . . Compiling using scripts . . . . . . . . .
| | |
LINECOUNT . . . . . . LIST . . . . . . . . . MAP . . . . . . . . . MDECK . . . . . . . . NAME . . . . . . . . NSYMBOL . . . . . . . NUMBER . . . . . . . NUMPROC . . . . . . . OBJECT . . . . . . . . OFFSET . . . . . . . . OPTFILE . . . . . . . . OPTIMIZE . . . . . . . OUTDD . . . . . . . . PGMNAME . . . . . . . PGMNAME(COMPAT) . . PGMNAME(LONGUPPER). PGMNAME(LONGMIXED) Usage notes . . . . . . QUOTE/APOST . . . . . RENT . . . . . . . . . RMODE . . . . . . . . SEQUENCE . . . . . . . SIZE . . . . . . . . . SOURCE . . . . . . . . SPACE . . . . . . . . SQL . . . . . . . . . SQLCCSID . . . . . . . SSRANGE . . . . . . . TERMINAL . . . . . . . TEST . . . . . . . . . THREAD . . . . . . . . TRUNC . . . . . . . . TRUNC example 1 . . . TRUNC example 2 . . . VBREF . . . . . . . . WORD . . . . . . . . XMLPARSE . . . . . . . XREF . . . . . . . . . YEARWINDOW . . . . . ZWB . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
327 328 328 329 331 331 332 333 334 335 335 336 337 338 339 339 340 340 340 341 342 343 344 344 345 345 347 347 348 349 352 353 355 355 356 356 357 358 360 360
Getting listings . . . . . . . . . . Example: short listing . . . . . . Example: SOURCE and NUMBER output Example: MAP output . . . . . . Reading LIST output . . . . . . . Example: XREF output: data-name cross-references. . . . . . . . . Example: OFFSET compiler output . . Example: VBREF compiler output . .
. . . . . . . .
. . . . . . . .
. . . . .
. 417
Contents
vii
. 429
Handling null-terminated strings . . . . . Using pointers to process a chained list . . Passing return-code information . . . . . . Understanding the RETURN-CODE special register . . . . . . . . . . . . . Using PROCEDURE DIVISION RETURNING . .. . . . . . . . . . . . . . . . Specifying CALL . . . RETURNING . . . . Sharing data by using the EXTERNAL clause. . Sharing files between programs (external files) . Example: using external files . . . . . .
503
. . . . . . . . . . . 504 506 506 508 510 514 515 518 520 521 525 526 528
viii
| |
Handling encoding conflicts . . . . Terminating XML parsing . . . . . . XML PARSE examples . . . . . . . Example: parsing a simple document . Example: program for processing XML . Example: parsing an XML document that namespaces . . . . . . . . . . Example: parsing an XML document one segment at a time . . . . . . . . Example: parsing XML documents with validation . . . . . . . . . .
. . . . . . . . . . uses . . . . . .
. . . . .
543
543 547 548 549 549 550 553 553 554 557
Generating XML output . . . . . . . . . . Controlling the encoding of generated XML output Handling XML GENERATE exceptions . . . . . Example: generating XML . . . . . . . . . Program XGFX . . . . . . . . . . . . Program Pretty . . . . . . . . . . . . Output from program XGFX . . . . . . . Enhancing XML output . . . . . . . . . . Example: enhancing XML output . . . . . . Example: converting hyphens in element or attribute names to underscores . . . . . .
Defining a subclass . . . . . . . . . . . CLASS-ID paragraph for defining a subclass REPOSITORY paragraph for defining a subclass WORKING-STORAGE SECTION for defining subclass instance data . . . . . . . . . Defining a subclass instance method . . . . Example: defining a subclass (with methods) Defining a factory section . . . . . . . . . WORKING-STORAGE SECTION for defining factory data . . . . . . . . . . . . . Defining a factory method . . . . . . . . Example: defining a factory (with methods) . . Wrapping procedure-oriented COBOL programs Structuring OO applications . . . . . . . . Examples: COBOL applications that run using the java command . . . . . . . . . . .
589 590 591 592 592 592 594 594 595 597 603 603 604
. 568 . 569 . 569 . 570 . 571 . 571 . . . . . . . . . . . . 572 573 574 575 576 578 579 580 581 582 586 588 589
ix
Using a century window . . . . . . . . Using internal bridging . . . . . . . . . Moving to full field expansion. . . . . . . Using year-first, year-only, and year-last date fields Compatible dates . . . . . . . . . . . Example: comparing year-first date fields . . . Using other date formats . . . . . . . . Example: isolating the year . . . . . . . . Manipulating literals as dates . . . . . . . . Assumed century window . . . . . . . . Treatment of nondates . . . . . . . . . Setting triggers and limits . . . . . . . . . Example: using limits . . . . . . . . . Using sign conditions . . . . . . . . . Sorting and merging by date . . . . . . . . Example: sorting by date and time . . . . . Performing arithmetic on date fields. . . . . . Allowing for overflow from windowed date fields . . . . . . . . . . . . . . . Specifying the order of evaluation . . . . . Controlling date processing explicitly . . . . . Using DATEVAL . . . . . . . . . . . Using UNDATE . . . . . . . . . . . Example: DATEVAL . . . . . . . . . . Example: UNDATE . . . . . . . . . . Analyzing and avoiding date-related diagnostic messages . . . . . . . . . . . . . . . Avoiding problems in processing dates . . . . . Avoiding problems with packed-decimal fields Moving from expanded to windowed date fields
638 639 641 643 643 644 644 645 645 646 647 648 649 650 650 651 651 652 653 653 654 654 655 655 656 657 657 658
Sample list of Language Environment callable services . . . . . . . . . . . . . Calling Language Environment services . . Example: Language Environment callable services . . . . . . . . . . . . .
. 699 . 699
709
XML PARSE exceptions with XMLPARSE(COMPAT) in effect . . . . . . . 711 XML PARSE exceptions that allow continuation 711 XML PARSE exceptions that do not allow continuation . . . . . . . . . . . . . 715 XML GENERATE exceptions . . . . . . . . 718
Compilation unit start|end record: X0002 Options record: X0010 . . . . . . . External symbol record: X0020 . . . . Parse tree record: X0024 . . . . . . Token record: X0030. . . . . . . . Source error record: X0032 . . . . . Source record: X0038 . . . . . . . COPY REPLACING record: X0039 . . . Symbol record: X0042 . . . . . . . Symbol cross-reference record: X0044 . . Nested program record: X0046 . . . . Library record: X0060 . . . . . . . Statistics record: X0090 . . . . . . . EVENTS record: X0120 . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
753 754 763 764 779 792 793 794 794 807 808 809 809 810
815
. . . . . . . . . . 815 816 817 818 819 820 822 823 825 828
| | | | |
. 734 . 735
. 829
Appendix F. JNI.cpy
Index . . . . . . . . . . . . . . . 875
Contents
xi
xii
Tables
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. FILE-CONTROL entries . . . . . . . . 8 FILE SECTION entries . . . . . . . . 14 Assignment to data items in a program 29 Effect of RMODE and RENT compiler options on the RMODE attribute . . . . . 42 Ranges in value of COMP-5 data items 51 Internal representation of numeric items 53 NUMCLS(PRIM) and valid signs . . . . . 57 NUMCLS(ALT) and valid signs . . . . . 57 Order of evaluation of arithmetic operators 59 Numeric intrinsic functions . . . . . . . 60 Compatibility of math intrinsic functions and callable services . . . . . . . . . . . 61 INTDATE(LILIAN) and compatibility of date intrinsic functions and callable services. . . 62 INTDATE(ANSI) and compatibility of date intrinsic functions and callable services. . . 62 Hexadecimal values of the euro sign . . . . 67 COBOL statements and national data 122 Intrinsic functions and national character data. . . . . . . . . . . . . . . 124 National group items that are processed with group semantics . . . . . . . . 132 Encoding and size of alphanumeric, DBCS, and national data . . . . . . . . . . 133 Summary of file organizations, access modes, and record formats of COBOL files . 147 QSAM file allocation. . . . . . . . . 167 Maximum record length of QSAM files 171 Handling of QSAM user labels . . . . . 176 Identifiers for standard tape labels . . . . 176 Comparison of VSAM, COBOL, and non-VSAM terminology. . . . . . . . 179 Comparison of VSAM data-set types 181 VSAM file organization, access mode, and record format . . . . . . . . . . . 182 Definition of VSAM fixed-length records 186 Definition of VSAM variable-length records 186 I/O statements for VSAM sequential files 188 I/O statements for VSAM relative and indexed files . . . . . . . . . . . 188 Statements to load records into a VSAM file 191 Statements to update records in a VSAM file . . . . . . . . . . . . . . . 193 Methods for improving VSAM performance 203 Methods for checking for sort errors with NOFASTSRT . . . . . . . . . . . 228 Methods for controlling sort behavior 228 Compiler data sets . . . . . . . . . 265 Block size of fixed-length compiler data sets 267 Block size of variable-length compiler data sets . . . . . . . . . . . . . . . 267 Types of compiler output under z/OS 273 Severity codes for compiler diagnostic messages . . . . . . . . . . . . . 281 Input files to the cob2 command . . . . . 289 42. Output files from the cob2 command 43. Commands for compiling and linking a class definition . . . . . . . . . . . 44. java command options for customizing the JVM . . . . . . . . . . . . . . 45. Compiler options . . . . . . . . . . 46. Mutually exclusive compiler options 47. EBCDIC multibyte coded character set identifiers . . . . . . . . . . . . 48. Values of the LANGUAGE compiler option 49. Severity levels of compiler messages 50. Using compiler options to get listings 51. Terms used in MAP output . . . . . . 52. Symbols used in LIST and MAP output 53. Signature information bytes for compiler options . . . . . . . . . . . . . 54. Signature information bytes for the DATA DIVISION . . . . . . . . . . . . 55. Signature information bytes for the ENVIRONMENT DIVISION . . . . . . 56. Signature information bytes for PROCEDURE DIVISION verbs . . . . . 57. Signature information bytes for more PROCEDURE DIVISION items . . . . . 58. Calls between COBOL and assembler under CICS . . . . . . . . . . . . . . 59. Compiler options required for the integrated CICS translator . . . . . . . . . . . 60. Compiler options required for the separate CICS translator . . . . . . . . . . . 61. TRUNC compiler options recommended for the separate CICS translator . . . . . . 62. Compiler options required with the DB2 coprocessor . . . . . . . . . . . . 63. Samples with POSIX function calls . . . . 64. Effects of termination statements. . . . . 65. Methods for passing data in the CALL statement . . . . . . . . . . . . . 66. Compiler options for DLL applications 67. Binder options for DLL applications 68. Special registers used by the XML parser 69. Result of processing-procedure changes to XML-CODE with XMLPARSE(XMLSS) in effect . . 70. Result of processing-procedure changes to XML-CODE with XMLPARSE(COMPAT) in effect . . 71. Coded character sets for XML documents 72. Hexadecimal values of white-space characters. . . . . . . . . . . . . 73. Aliases for XML encoding declarations 74. Hexadecimal values of special characters for various EBCDIC CCSIDs . . . . . . . 75. XML events and special registers . . . . 76. XML events and special registers . . . . 77. Encoding of generated XML if the ENCODING phrase is omitted . . . . . 78. Structure of class definitions . . . . . . 289 292 294 301 304 312 326 374 377 384 385 390 391 392 392 394 410 412 415 415 424 441 448 466 482 483 508 511 511 521 522 524 524 531 536 547 564
| | | | | |
xiii
| |
79. Structure of instance method definitions 80. Structure of COBOL clients . . . . . . 81. Conformance of arguments in a COBOL client . . . . . . . . . . . . . . 82. Conformance of the returned data item in a COBOL client . . . . . . . . . . . 83. Structure of factory definitions . . . . . 84. Structure of factory method definitions 85. JNI services for local and global references 86. Interoperable data types in COBOL and Java 87. Interoperable arrays and strings in COBOL and Java . . . . . . . . . . . . . 88. Noninteroperable array types in COBOL and Java . . . . . . . . . . . . . 89. JNI array services . . . . . . . . . . 90. Services that convert between jstring references and national data . . . . . . 91. Services that convert between jstring references and alphanumeric data . . . . 92. Advantages and disadvantages of Year 2000 solutions . . . . . . . . . . . . . 93. Performance-related compiler options 94. Performance-tuning worksheet . . . . . 95. Language Environment callable services 96. IGZCA2D return codes . . . . . . . . 97. IGZCD2A return codes . . . . . . . . 98. Reason codes for XML PARSE exceptions that are unique to Enterprise COBOL . . . 99. XML PARSE exceptions that allow continuation (for XMLPARSE(COMPAT)) . . . . 100. XML PARSE exceptions that do not allow continuation (for XMLPARSE(COMPAT)) . . . . 101. XML GENERATE exceptions . . . . . . 102. Layout of the user-exit work area . . . . 103. INEXIT processing . . . . . . . . . 104. INEXIT parameters . . . . . . . . . 105. LIBEXIT processing . . . . . . . . . 106. LIBEXIT processing with nonnested COPY statements . . . . . . . . . . . . 107. LIBEXIT processing with nested COPY statements . . . . . . . . . . . .
570 578 583 585 594 595 611 612 613 614 614 617 617 638 672 675 682 705 707 709 711
| | |
108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135.
LIBEXIT parameters . . . . . . . . . PRTEXIT processing . . . . . . . . . PRTEXIT parameters . . . . . . . . . ADEXIT processing . . . . . . . . . ADEXIT parameters . . . . . . . . . MSGEXIT processing . . . . . . . . MSGEXIT parameters . . . . . . . . FIPS (FLAGSTD) message categories . . . . Actions possible in exit modules for CICS and SQL statements . . . . . . . . . SYSADATA record types . . . . . . . SYSADATA common header section SYSADATA job identification record ADATA identification record . . . . . . SYSADATA compilation unit start|end record . . . . . . . . . . . . . . SYSADATA options record . . . . . . . SYSADATA external symbol record SYSADATA parse tree record . . . . . . SYSADATA token record . . . . . . . SYSADATA source error record . . . . . SYSADATA source record . . . . . . . SYSADATA COPY REPLACING record SYSADATA symbol record . . . . . . . SYSADATA symbol cross-reference record SYSADATA nested program record . . . . SYSADATA library record . . . . . . . SYSADATA statistics record . . . . . . SYSADATA EVENTS TIMESTAMP record layout . . . . . . . . . . . . . . SYSADATA EVENTS PROCESSOR record layout . . . . . . . . . . . . . . SYSADATA EVENTS FILE END record layout . . . . . . . . . . . . . . SYSADATA EVENTS PROGRAM record layout . . . . . . . . . . . . . . SYSADATA EVENTS FILE ID record layout SYSADATA EVENTS ERROR record layout
725 726 726 727 728 729 729 732 734 748 751 752 753 754 754 764 764 779 793 793 794 794 807 808 809 809 810 810 811 811 811 812
xiv
Preface
About this information
Welcome to IBM Enterprise COBOL for z/OS, IBMs latest host COBOL compiler! This version of IBM COBOL adds new COBOL function to help integrate COBOL business processes and Web-oriented business processes by: v Simplifying the componentization of COBOL programs and enabling interoperability with Java components v Promoting the exchange and use of data in standardized formats, including XML and Unicode
Abbreviated terms
Certain terms are used in a shortened form in this information. Abbreviations for the product names used most frequently are listed alphabetically in the following table.
Term used CICS
Long form CICS Transaction Server IBM Enterprise COBOL for z/OS IBM z/OS Language Environment MVS/ESA
xv
In addition to these abbreviated terms, the term Standard COBOL 85 is used to refer to the combination of the following standards: v ISO 1989:1985, Programming languages - COBOL v ISO/IEC 1989/AMD1:1992, Programming languages - COBOL: Intrinsic function module v ISO/IEC 1989/AMD2:1994, Programming languages - Correction and clarification amendment for COBOL v ANSI INCITS 23-1985, Programming Languages - COBOL v ANSI INCITS 23a-1989, Programming Languages - Intrinsic Function Module for COBOL v ANSI INCITS 23b-1993, Programming Language - Correction Amendment for COBOL The ISO standards are identical to the American National standards. Other terms, if not commonly understood, are shown in italics the first time that they appear, and are listed in the glossary.
Pass parameters indirectly, by value BY CONTENT Routine Scalar Program Elementary item
xvi
The ---> symbol indicates that the syntax diagram is continued on the next line. The >--- symbol indicates that the syntax diagram is continued from the previous line. The --->< symbol indicates the end of a syntax diagram. Diagrams of syntactical units other than complete statements start with the >--symbol and end with the ---> symbol. v Required items appear on the horizontal line (the main path):
required_item
required_item optional_item
v If you can choose from two or more items, they appear vertically, in a stack. If you must choose one of the items, one item of the stack appears on the main path:
required_item
required_choice1 required_choice2
If choosing one of the items is optional, the entire stack appears below the main path:
If one of the items is the default, it appears above the main path and the remaining choices are shown below:
v An arrow returning to the left, above the main line, indicates an item that can be repeated:
Preface
xvii
required_item
repeatable_item
If the repeat arrow contains a comma, you must separate repeated items with a comma:
, required_item repeatable_item
v Keywords appear in uppercase (for example, FROM). They must be spelled exactly as shown. Variables appear in lowercase italics (for example, column-name). They represent user-supplied names or values. v If punctuation marks, parentheses, arithmetic operators, or other such symbols are shown, you must enter them as part of the syntax.
xviii
Summary of changes
This section lists the major changes that have been made to Enterprise COBOL in Version 4. The changes that are described in this information have an associated cross-reference for your convenience. The latest technical changes are marked by a vertical bar (|) in the left margin in the PDF and BookManager versions. | | | | | | | | | | | | | | | | | | | | | | | | | | | |
xix
You can parse documents that are encoded in Unicode UTF-8 (Parsing XML documents encoded in UTF-8 on page 525). The RETURNING NATIONAL phrase enables you to receive XML document fragments in Unicode regardless of the original encoding of an XML document. You can parse documents that reside in a data set or parse very large documents a buffer at a time (Parsing XML documents one segment at a time on page 518). v The XML GENERATE statement has been enhanced (Chapter 29, Producing XML output, on page 543): You can specify a namespace using the NAMESPACE phrase, and a namespace prefix to be applied to each element using the NAMESPACE-PREFIX phrase. You can specify the code page of the generated document using the ENCODING phrase (Controlling the encoding of generated XML output on page 547). XML documents can now be generated in UTF-8 as well as in UTF-16 or various EBCDIC code pages. The WITH ATTRIBUTES phrase causes eligible elementary items to be expressed as attributes rather than as child elements in the generated XML. The WITH XML-DECLARATION phrase causes an XML declaration to be generated. v A new compiler option, OPTFILE, enables the specifying of COBOL compiler options from within a data set (OPTFILE on page 335). v Compiler listings now cross-reference copybooks and the data sets from which copybooks are obtained (Example: XREF output: COPY/BASIS cross-references on page 400). v Support for new features of DB2 for z/OS V9 is enabled when you use the integrated DB2 coprocessor (SQL compiler option) (DB2 coprocessor on page 419): New SQL data types are supported: XML types, BINARY, VARBINARY, BIGINT, and file reference variables. New SQL syntax for XML manipulation, enhancements to large object manipulation, MERGE, and SELECT FROM MERGE is supported. DB2 processing options STDSQL(YES|NO), NOFOR, and SQL(ALL|DB2) are supported as suboptions to the SQL compiler option (Compiling with the SQL option on page 423). v Several usability enhancements to COBOL-DB2 applications are available when you use the integrated DB2 coprocessor: The compiler listing is enhanced to show the DB2 options in effect (if you use DB2 for z/OS V9) and to show the expansion of the SQLCA and SQLDA control blocks. You can specify an alternate ddname for DBRMLIB when you invoke the compiler from an assembler language program (Starting the compiler from an assembler program on page 263). An explicitly coded LOCAL-STORAGE SECTION or WORKING-STORAGE SECTION is no longer required. v Debugging has been enhanced to support Debug Tool V8. A new suboption of the TEST compiler option, EJPD, enables the Debug Tool commands JUMPTO and GOTO for production debugging. The TEST compiler option has been simplified and has restructured suboptions (TEST on page 349).
xx
Accessibility
Accessibility features help users who have a disability, such as restricted mobility or limited vision, to use software products successfully. The accessibility features in z/OS provide accessibility for Enterprise COBOL. The major accessibility features in z/OS are: v Interfaces that are commonly used by screen readers and screen-magnifier software v Keyboard-only navigation v Ability to customize display attributes such as color, contrast, and font size
Interface information
Assistive technology products work with the user interfaces that are found in z/OS. For specific guidance information, see the documentation for the assistive technology product that you use to access z/OS interfaces.
Keyboard navigation
Users can access z/OS user interfaces by using TSO/E or ISPF. For information about accessing TSO/E or ISPF interfaces, see the following publications: v z/OS TSO/E Primer v z/OS TSO/E Users Guide v z/OS ISPF Users Guide Volume I
Preface
xxi
These guides describe how to use TSO/E and ISPF, including the use of keyboard shortcuts or function keys (PF keys). Each guide includes the default settings for the PF keys and explains how to modify their functions.
xxii
Using intrinsic functions (built-in functions) Using tables (arrays) and pointers . . . . Storage and its addressability . . . . . Settings for RMODE . . . . . . . . Storage restrictions for passing data . . Location of data areas . . . . . . . Storage for LOCAL-STORAGE data . . Storage for external data . . . . . . Storage for QSAM input-output buffers .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
40 41 42 42 43 43 43 44 44 45 45 47 48 49 49 50 50 51 52 52 52 54 54 54 55 55 55 56 57 58 58 59 60 62 62 63 63 63 64 64 64 65 65 65 66 67 68
25 25 25 26 27 28 28 29 30 32 34 35 36 37 38 39 39
Chapter 3. Working with numbers and arithmetic Defining numeric data. . . . . . . . . . . Displaying numeric data . . . . . . . . . . Controlling how numeric data is stored . . . . . Formats for numeric data. . . . . . . . . . External decimal (DISPLAY and NATIONAL) items . . . . . . . . . . . . . . . External floating-point (DISPLAY and NATIONAL) items . . . . . . . . . . . Binary (COMP) items . . . . . . . . . . Native binary (COMP-5) items . . . . . . . Packed-decimal (COMP-3) items . . . . . . Internal floating-point (COMP-1 and COMP-2) items . . . . . . . . . . . . . . . Examples: numeric data and internal representation . . . . . . . . . . . . Data format conversions . . . . . . . . . . Conversions and precision . . . . . . . . Conversions that lose precision . . . . . . Conversions that preserve precision . . . . Conversions that result in rounding . . . . Sign representation of zoned and packed-decimal data . . . . . . . . . . . . . . . . . Checking for incompatible data (numeric class test) Performing arithmetic . . . . . . . . . . . Using COMPUTE and other arithmetic statements . . . . . . . . . . . . . . Using arithmetic expressions . . . . . . . Using numeric intrinsic functions . . . . . . Using math-oriented callable services . . . . . Using date callable services . . . . . . . . Examples: numeric intrinsic functions . . . . General number handling . . . . . . . Date and time . . . . . . . . . . . Finance . . . . . . . . . . . . . . Mathematics . . . . . . . . . . . . Statistics . . . . . . . . . . . . . Fixed-point contrasted with floating-point arithmetic Floating-point evaluations . . . . . . . . Fixed-point evaluations . . . . . . . . . Arithmetic comparisons (relation conditions) . . Examples: fixed-point and floating-point evaluations . . . . . . . . . . . . . Using currency signs . . . . . . . . . . . Example: multiple currency signs . . . . . .
Chapter 4. Handling tables. . . . . . . . Defining a table (OCCURS) . . . . . . . . Nesting tables . . . . . . . . . . . . Example: subscripting . . . . . . . . . Example: indexing . . . . . . . . . . Referring to an item in a table . . . . . . . Subscripting . . . . . . . . . . . . Indexing . . . . . . . . . . . . . Putting values into a table . . . . . . . . Loading a table dynamically. . . . . . . Initializing a table (INITIALIZE) . . . . . Assigning values when you define a table (VALUE) . . . . . . . . . . . . . Initializing each table item individually . . Initializing a table at the group level . . . Initializing all occurrences of a given table element. . . . . . . . . . . . . Example: PERFORM and subscripting . . . Example: PERFORM and indexing. . . . . Creating variable-length tables (DEPENDING ON) Loading a variable-length table . . . . . . Assigning values to a variable-length table . . Searching a table . . . . . . . . . . . Doing a serial search (SEARCH) . . . . . Example: serial search . . . . . . . . Doing a binary search (SEARCH ALL) . . . Example: binary search . . . . . . . Processing table items using intrinsic functions . Example: processing tables using intrinsic functions . . . . . . . . . . . . .
. . . . . . . . . . .
69 69 71 72 72 72 73 74 75 75 76
. 77 . 77 . 78 . 78 . 79 . 80 81 . 82 . 83 . 84 . 84 . 84 . 85 . 86 . 86 . 87
Referring to substrings of data items . . . Reference modifiers . . . . . . . . Example: arithmetic expressions as reference modifiers . . . . . . . . . . . . Example: intrinsic functions as reference modifiers . . . . . . . . . . . . Tallying and replacing data items (INSPECT) . Examples: INSPECT statement. . . . . Converting data items (intrinsic functions). . Converting to uppercase or lowercase (UPPER-CASE, LOWER-CASE) . . . . Transforming to reverse order (REVERSE) . Converting to numbers (NUMVAL, NUMVAL-C) . . . . . . . . . . Converting from one code page to another Evaluating data items (intrinsic functions) . . Evaluating single characters for collating sequence . . . . . . . . . . . . Finding the largest or smallest data item . Returning variable-length results with alphanumeric or national functions . . Finding the length of data items . . . . Finding the date of compilation . . . .
. . . . . . . . . . . . . . . . .
. 113 . 113 . 113 . 115 . 115 . 115 . 116 . 117 . 118 . 119
Chapter 5. Selecting and repeating program actions . . . . . . . . . . . . . . . 89 Selecting program actions . . . . . . . . . 89 Coding a choice of actions . . . . . . . . 89 Using nested IF statements . . . . . . . 90 Using the EVALUATE statement . . . . . 91 Coding conditional expressions . . . . . . . 94 Switches and flags . . . . . . . . . . 95 Defining switches and flags . . . . . . . 95 Example: switches . . . . . . . . . . 95 Example: flags . . . . . . . . . . . 96 Resetting switches and flags . . . . . . . 96 Example: set switch on . . . . . . . . 96 Example: set switch off . . . . . . . . 97 Repeating program actions . . . . . . . . . 97 Choosing inline or out-of-line PERFORM . . . 98 Example: inline PERFORM statement . . . . 98 Coding a loop . . . . . . . . . . . . 99 Looping through a table . . . . . . . . . 100 Executing multiple paragraphs or sections. . . 100 Chapter 6. Handling strings . . . . . . . . 101 Joining data items (STRING) . . . . . . . . 101 Example: STRING statement . . . . . . . 102 STRING results. . . . . . . . . . . 103 Splitting data items (UNSTRING) . . . . . . 103 Example: UNSTRING statement . . . . . . 104 UNSTRING results . . . . . . . . . 105 Manipulating null-terminated strings . . . . . 106 Example: null-terminated strings . . . . . . 107
Chapter 7. Processing data in an international environment . . . . . . . . . . . . . COBOL statements and national data . . . . . Intrinsic functions and national data. . . . . . Unicode and the encoding of language characters Using national data (Unicode) in COBOL . . . . Defining national data items . . . . . . . Using national literals . . . . . . . . . Using national-character figurative constants Defining national numeric data items . . . . National groups . . . . . . . . . . . Using national groups . . . . . . . . . Using national groups as elementary items Using national groups as group items . . . Storage of character data . . . . . . . . Converting to or from national (Unicode) representation . . . . . . . . . . . . . Converting alphanumeric, DBCS, and integer to national (MOVE) . . . . . . . . . . . Converting alphanumeric or DBCS to national (NATIONAL-OF) . . . . . . . . . . . Converting national to alphanumeric (DISPLAY-OF) . . . . . . . . . . . . Overriding the default code page. . . . . . Conversion exceptions . . . . . . . . . Example: converting to and from national data Processing UTF-8 data . . . . . . . . . . Processing Chinese GB 18030 data . . . . . . Comparing national (UTF-16) data . . . . . . Comparing two class national operands . . . Comparing class national and class numeric operands . . . . . . . . . . . . . . Comparing national numeric and other numeric operands . . . . . . . . . . . . . . Comparing national and other character-string operands . . . . . . . . . . . . . .
121 122 124 125 126 127 127 128 129 129 130 131 132 133 134 134 135 136 136 136 137 137 138 139 139 140 140 140
Comparing national data and alphanumeric-group operands . . . . Coding for use of DBCS support . . . . Declaring DBCS data . . . . . . . Using DBCS literals . . . . . . . Testing for valid DBCS characters . . Processing alphanumeric data items that DBCS data . . . . . . . . . . Chapter 8. Processing files . . . . . File organization and input-output devices Choosing file organization and access mode Format for coding input and output . . Allocating files . . . . . . . . . . Checking for input or output errors . . .
. . . . . . . . . . . . . . . contain . . .
. . . 145 . . . 145 . . . 147 . . . 148 . . . 149 . . . 150 151 151 152 152 153 154 156 158 159 161 162 163 163 164 164 165 165 166 166 169 169 170 170 171 172 172 173 174 174 175 176 177 177 178 179 180 181 182 182 183
Chapter 9. Processing QSAM files . . . . . Defining QSAM files and records in COBOL . . . Establishing record formats. . . . . . . . Logical records . . . . . . . . . . . Requesting fixed-length format . . . . . Requesting variable-length format . . . . Requesting spanned format. . . . . . . Requesting undefined format . . . . . . Setting block sizes . . . . . . . . . . . Coding input and output statements for QSAM files . . . . . . . . . . . . . . . . Opening QSAM files . . . . . . . . . . Dynamically creating QSAM files. . . . . . Adding records to QSAM files. . . . . . . Updating QSAM files . . . . . . . . . Writing QSAM files to a printer or spooled data set . . . . . . . . . . . . . . . . Closing QSAM files . . . . . . . . . . Handling errors in QSAM files . . . . . . . Working with QSAM files . . . . . . . . . Defining and allocating QSAM files . . . . . Parameters for creating QSAM files . . . . Retrieving QSAM files . . . . . . . . . Parameters for retrieving QSAM files . . . Ensuring that file attributes match your program . . . . . . . . . . . . . . Processing existing files . . . . . . . . Processing new files . . . . . . . . . Using striped extended-format QSAM data sets Allocation of buffers for QSAM files. . . . Accessing HFS files using QSAM . . . . . . . Labels for QSAM files . . . . . . . . . . Using trailer and header labels . . . . . . Format of standard labels . . . . . . . . Standard user labels . . . . . . . . . Processing QSAM ASCII files on tape . . . . . Processing ASCII file labels . . . . . . . . . Chapter 10. Processing VSAM files . . . . . VSAM files . . . . . . . . . . . . . . Defining VSAM file organization and records . . Specifying sequential organization for VSAM files . . . . . . . . . . . . . . . Specifying indexed organization for VSAM files Using alternate keys . . . . . . . . .
Using an alternate index. . . . . . . . Specifying relative organization for VSAM files Fixed-length and variable-length RRDS . . . Using variable-length RRDS . . . . . . Specifying access modes for VSAM files . . . Example: using dynamic access with VSAM files . . . . . . . . . . . . . . Defining record lengths for VSAM files. . . . Defining fixed-length records . . . . . . Defining variable-length records . . . . . Coding input and output statements for VSAM files . . . . . . . . . . . . . . . . File position indicator . . . . . . . . . Opening a file (ESDS, KSDS, or RRDS) . . . . Opening an empty file . . . . . . . . Statements to load records into a VSAM file Opening a loaded file (a file with records) Reading records from a VSAM file . . . . . Updating records in a VSAM file . . . . . . Adding records to a VSAM file . . . . . . Replacing records in a VSAM file. . . . . . Deleting records from a VSAM file . . . . . Closing VSAM files . . . . . . . . . . Handling errors in VSAM files . . . . . . . Protecting VSAM files with a password . . . . Example: password protection for a VSAM indexed file . . . . . . . . . . . . . Working with VSAM data sets under z/OS and z/OS UNIX . . . . . . . . . . . . . . Defining VSAM files . . . . . . . . . . Creating alternate indexes . . . . . . . . Example: entries for alternate indexes . . . Allocating VSAM files . . . . . . . . . Sharing VSAM files through RLS . . . . . . Preventing update problems with VSAM files in RLS mode . . . . . . . . . . . Restrictions when using RLS . . . . . . Handling errors in VSAM files in RLS mode Improving VSAM performance . . . . . . .
183 184 184 184 185 185 185 186 186 187 189 189 190 191 191 192 193 193 194 194 194 195 196 196 197 197 198 199 200 202 202 203 203 203
Chapter 11. Processing line-sequential files . . 207 Defining line-sequential files and records in COBOL . . . . . . . . . . . . . . . 207 Allowable control characters . . . . . . . 208 Describing the structure of a line-sequential file 208 Defining and allocating line-sequential files . . . 209 Coding input-output statements for line-sequential files . . . . . . . . . . . . . . . . 209 Opening line-sequential files . . . . . . . 210 Reading records from line-sequential files . . . 210 Adding records to line-sequential files . . . . 211 Closing line-sequential files. . . . . . . . 211 Handling errors in line-sequential files . . . . . 212 Chapter 12. Sorting and merging files . . Sort and merge process . . . . . . . . Describing the sort or merge file . . . . . Describing the input to sorting or merging . Example: describing sort and input files for SORT . . . . . . . . . . . . . Coding the input procedure . . . . . . . . 213 . . 214 . . 214 . . 215 . . . 215 . 216
Describing the output from sorting or merging . . Coding the output procedure . . . . . . . . Example: coding the output procedure when using DFSORT . . . . . . . . . . . . Restrictions on input and output procedures . . . Defining sort and merge data sets . . . . . . Sorting variable-length records . . . . . . . Requesting the sort or merge . . . . . . . . Setting sort or merge criteria . . . . . . . Example: sorting with input and output procedures . . . . . . . . . . . . . Choosing alternate collating sequences . . . . Sorting on windowed date fields . . . . . . Preserving the original sequence of records with equal keys . . . . . . . . . . . . . Determining whether the sort or merge was successful . . . . . . . . . . . . . . Stopping a sort or merge operation prematurely Improving sort performance with FASTSRT . . . FASTSRT requirements for JCL . . . . . . FASTSRT requirements for sort input and output files . . . . . . . . . . . . . QSAM requirements . . . . . . . . . VSAM requirements . . . . . . . . . Checking for sort errors with NOFASTSRT . . . Controlling sort behavior . . . . . . . . . Changing DFSORT defaults with control statements . . . . . . . . . . . . . Default characteristics of the IGZSRTCD data set . . . . . . . . . . . . . . . Allocating storage for sort or merge operations Allocating space for sort files . . . . . . . Using checkpoint/restart with DFSORT . . . . Sorting under CICS . . . . . . . . . . . CICS SORT application restrictions . . . . .
217 218 218 219 219 220 220 221 222 223 223 224 224 225 225 226 226 227 227 227 228 229 230 230 231 231 231 232
Chapter 13. Handling errors . . . . . . . . 233 Requesting dumps . . . . . . . . . . . 233 Handling errors in joining and splitting strings . . 234 Handling errors in arithmetic operations . . . . 234 Example: checking for division by zero . . . . 235 Handling errors in input and output operations 235 Using the end-of-file condition (AT END) . . . 238 Coding ERROR declaratives . . . . . . . 238 Using file status keys . . . . . . . . . . 239 Example: file status key . . . . . . . . . 240 Using VSAM status codes (VSAM files only) 241 Example: checking VSAM status codes . . . . 241 Coding INVALID KEY phrases . . . . . . 243 Example: FILE STATUS and INVALID KEY . . 243 Handling errors when calling programs . . . . 244 Writing routines for handling errors . . . . . . 244
Identifying a program Describing the computing environment on page 7 Describing the data on page 13 Processing the data on page 19 Defining a class on page 564 Defining a class instance method on page 569 Structuring OO applications on page 603
Identifying a program
Use the IDENTIFICATION DIVISION to name a program and optionally provide other identifying information. You can use the optional AUTHOR, INSTALLATION, DATE-WRITTEN, and DATE-COMPILED paragraphs for descriptive information about a program. The data you enter in the DATE-COMPILED paragraph is replaced with the latest compilation date.
IDENTIFICATION Program-ID. Author. Installation. Date-Written. Date-Compiled. DIVISION. Helloprog. A. Programmer. Computing Laboratories. 07/30/2009. 08/30/2009.
Use the PROGRAM-ID paragraph to name your program. The program-name that you assign is used in these ways: v Other programs use that name to call your program. v The name appears in the header on each page, except the first, of the program listing that is generated when you compile the program. v If you use the NAME compiler option, the name is placed on the NAME linkage-editor or binder control statement to identify the object module that the compilation creates. Tip: Do not use program-names that start with prefixes used by IBM products. If you use program-names that start with any of the following prefixes, your CALL statements might resolve to IBM library or compiler routines rather than to your intended program: AFB AFH CBC
Copyright IBM Corp. 1991, 2009
| | | | | | |
CEE CEH CEL CEQ CEU DFH DSN EDC FOR IBM IFY IGY
IGZ ILB Tip: If a program-name is case sensitive, avoid mismatches with the name that the compiler is looking for. Verify that the appropriate setting of the PGMNAME compiler option is in effect.
RELATED TASKS
Changing the header of a source listing on page 7 Identifying a program as recursive Marking a program as callable by containing programs Setting a program to an initial state on page 7
RELATED REFERENCES
Compiler limits (Enterprise COBOL Language Reference) Conventions for program-names (Enterprise COBOL Language Reference)
Sharing data in recursive or multithreaded programs on page 19 Making recursive calls on page 461
RELATED CONCEPTS
Ending and reentering main programs or subprograms on page 448 Making static calls on page 450 Making dynamic calls on page 451
The header indicates the compilation platform. You can customize the header on succeeding pages of the listing by using the compiler-directing TITLE statement.
RELATED REFERENCES
The terms file in COBOL terminology and data set or HFS file in operating-system terminology have essentially the same meaning and are used interchangeably in this information. For Customer Information Control System (CICS) and online Information Management System (IMS) message processing programs (MPP), code only the ENVIRONMENT DIVISION header and, optionally, the CONFIGURATION SECTION. CICS does not allow COBOL definition of files. IMS allows COBOL definition of files only for batch programs. v Provide information to control efficient transmission of the data records between your program and the external medium. Example: FILE-CONTROL entries
RELATED TASKS
Specifying the collating sequence on page 9 Defining symbolic characters on page 10 Defining a user-defined class on page 10 Defining files to the operating system on page 10
RELATED REFERENCES
VSAM file SELECT COMMUTER-FILE ASSIGN TO COMMUTER2 ORGANIZATION IS INDEXED3 ACCESS IS RANDOM4 RECORD KEY IS COMMUTER-KEY5 FILE STATUS IS5 COMMUTER-FILE-STATUS COMMUTER-VSAM-STATUS.
1
Line-sequential file SELECT PRINTFILE1 ASSIGN TO UPDPRINT2 ORGANIZATION IS LINE SEQUENTIAL3 ACCESS IS SEQUENTIAL.4
1. The SELECT clause chooses a file in the COBOL program to be associated with an external data set. 2. The ASSIGN clause associates the programs name for the file with the external name for the actual data file. You can define the external name with a DD statement or an environment variable. 3. The ORGANIZATION clause describes the files organization. For QSAM files, the ORGANIZATION clause is optional. 4. The ACCESS MODE clause defines the manner in which the records are made available for processing: sequential, random, or dynamic. For QSAM and line-sequential files, the ACCESS MODE clause is optional. These files always have sequential organization. 5. For VSAM files, you might have additional statements in the FILE-CONTROL paragraph depending on the type of VSAM file you use.
RELATED TASKS
Chapter 9, Processing QSAM files, on page 151 Chapter 10, Processing VSAM files, on page 179 Chapter 11, Processing line-sequential files, on page 207 Describing the computing environment on page 7
Choosing alternate collating sequences on page 223 Comparing national (UTF-16) data on page 139
"A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
RELATED TASKS
Also Also Also Also Also Also Also Also Also Also Also Also Also Also Also Also Also Also Also Also Also Also Also Also Also Also
"a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z".
You can reference the class-name only in a class condition. (This user-defined class is not the same as an object-oriented class.)
10
v ALLOCATE command under TSO. v Environment variable for z/OS or UNIX. The contents can define either an MVS data set or a file in the HFS (hierarchical file system). The following examples show the relationship of a FILE-CONTROL entry to the system data definition and to the FD entry in the FILE SECTION: v JCL DD statement:
(1) //OUTFILE /* DD DSNAME=MY.OUT171,UNIT=SYSDA,SPACE=(TRK,(50,5))
v COBOL code:
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CARPOOL ASSIGN TO OUTFILE (1) ORGANIZATION IS SEQUENTIAL. . . . DATA DIVISION. FILE SECTION. FD CARPOOL (2) LABEL RECORD STANDARD BLOCK CONTAINS 0 CHARACTERS RECORD CONTAINS 80 CHARACTERS
(1)
The assignment-name in the ASSIGN clause points to the ddname OUTFILE in the DD statement or the environment variable OUTFILE in the export command: v //OUTFILE DD DSNAME=OUT171 . . ., or v export OUTFILE= . . . When you specify a file file-name in a FILE-CONTROL entry, you must describe the file in an FD entry:
SELECT CARPOOL . . . FD CARPOOL
(2)
RELATED TASKS
FILE SECTION entries on page 14 File section (Enterprise COBOL Language Reference)
11
Environment variable values that are in effect at the time of the OPEN statement are used for associating COBOL file-names to the system file-names (including any path specifications). The name that you use in the assignment-name of the ASSIGN clause must be the same as the ddname in the DD statement or the environment variable in the export command. The file-name that you use in the SELECT clause (such as SELECT MASTER) must be the same as in the FD file-name entry. Two files should not use the same ddname or environment variable name in their SELECT clauses; otherwise, results could be unpredictable. For example, if DISPLAY output is directed to SYSOUT, do not use SYSOUT as the ddname or environment variable name in the SELECT clause for a file. Example: using different input files: This example shows that you use the same COBOL program to access different files by coding a DD statement or an export command before the programs runs. Consider a COBOL program that contains the following SELECT clause:
SELECT MASTER ASSIGN TO DA-3330-S-MASTERA
Assume the three possible input files are MASTER1, MASTER2, and MASTER3. Before running the program, code one of the following DD statements in the job step that calls for program execution, or issue one of the following export commands from the same shell from which you run the program:
//MASTERA DD DSNAME=MY.MASTER1,. . . export MASTERA=DSN(MY.MASTER1),. . . //MASTERA DD DSNAME=MY.MASTER2,. . . export MASTERA=DSN(MY.MASTER2),. . . //MASTERA DD DSNAME=MY.MASTER3,. . . export MASTERA=DSN(MY.MASTER3),. . .
Any reference in the program to MASTER will therefore be a reference to the file currently assigned to the ddname or environment-variable name MASTERA. Notice that in this example, you cannot use the PATH(path) form of the export command to reference a line-sequential file in the HFS, because you cannot specify an organization field (S- or AS-) with a line-sequential file.
12
The AWO compiler option applies an implicit APPLY WRITE-ONLY clause to all eligible files. The NOAWO compiler option has no effect on files that have the APPLY WRITE-ONLY clause specified. The APPLY WRITE-ONLY clause takes precedence over the NOAWO compiler option. The APPLY-WRITE ONLY clause can cause input files to use a record area rather than process the data in the buffer. This use might affect the processing of both input files and output files.
RELATED REFERENCES
Using data in input and output operations Using data from another program on page 18
RELATED REFERENCES
13
record in more detail. Use the READ INTO statement to bring the records into WORKING-STORAGE. Processing occurs on the copy of data in WORKING-STORAGE. A WRITE FROM statement writes processed data into the record area defined in the FILE SECTION. The record-name established is the object of WRITE and REWRITE statements. For QSAM files only, you can set the record format in the RECORDING MODE clause. If you omit the RECORDING MODE clause, the compiler determines the record format based on the RECORD clause and on the level-01 record descriptions. For QSAM files, you can set a blocking factor for the file in the BLOCK CONTAINS clause. If you omit the BLOCK CONTAINS clause, the file defaults to unblocked. However, you can override this with z/OS data management facilities (including a DD file job-control statement). For line-sequential files, you can set a blocking factor for the file in the BLOCK CONTAINS clause. When you code BLOCK CONTAINS 1 RECORDS, or BLOCK CONTAINS n CHARACTERS, where n is the length of one logical record in bytes, WRITE statements result in the record being transferred immediately to the file rather than being buffered. This technique is useful when you want each record written immediately, such as to an error log. Programs in the same run unit can share, or have access to, common files. The method for doing this depends on whether the programs are part of a nested (contained) structure or are separately compiled (including programs compiled as part of a batch sequence). You can use the EXTERNAL clause for separately compiled programs. A file that is defined as EXTERNAL can be referenced by any program in the run unit that describes the file. You can use the GLOBAL clause for programs in a nested, or contained, structure. If a program contains another program (directly or indirectly), both programs can access a common file by referencing a GLOBAL file-name.
RELATED CONCEPTS
14
Table 2. FILE SECTION entries (continued) Clause BLOCK CONTAINS To define Size of physical records Notes If the CHARACTERS phrase is specified, size indicates the number of bytes in a record regardless of the USAGE of the data items in the record. QSAM: If provided, must match information on JCL or data-set label. If specified as BLOCK CONTAINS 0, or not provided, the system determines the optimal block size for you. Line sequential: Can be specified to control buffering for WRITE statements. VSAM: Syntax-checked, but has no effect on execution. RECORD CONTAINS n Size of logical records (fixed length) Integer size indicates the number of bytes in a record regardless of the USAGE of the data items in the record. If the clause is provided, it must match information on JCL or data-set label. If n is equal to 0, LRECL must be coded on JCL or data-set label. Integer size or sizes, if specified, indicate the number of bytes in a record regardless of the USAGE of the data items in the record. If the clause is provided, it must match information on JCL or data-set label; compiler checks that record descriptions match. The integer sizes indicate the number of bytes in a record regardless of the USAGE of the data items in the record. If the clause is provided, it must match information on JCL or data-set label; compiler checks that record descriptions match. VSAM: Handled as comments QSAM: Handled as comments QSAM: Handled as comments
RECORD IS VARYING
RECORD CONTAINS n TO m
Labels defined by the user QSAM: Allowed for (optional) tape or disk An item in the label records associated with file Names of records associated with file Depth of logical page Comments only
15
Table 2. FILE SECTION entries (continued) Clause CODE-SET To define ASCII or EBCDIC files Notes QSAM only. When an ASCII file is identified with the CODE-SET clause, the corresponding DD statement might need to have DCB=(OPTCD=Q. . .) or DCB=(RECFM=D. . .) coded if the file was not created using VS COBOL II, COBOL for OS/390 & VM, or IBM Enterprise COBOL for z/OS. RECORDING MODE Physical record description QSAM only
RELATED CONCEPTS
16
RELATED TASKS
Ending and reentering main programs or subprograms on page 448 Chapter 27, Preparing COBOL programs for multithreading, on page 493 WORKING-STORAGE SECTION for defining class instance data on page 568
RELATED REFERENCES
Working-storage section (Enterprise COBOL Language Reference) Local-storage section (Enterprise COBOL Language Reference)
The following tables show the changing values of the data items in LOCAL-STORAGE and WORKING-STORAGE in the successive recursive calls of the program, and in the ensuing gobacks. During the gobacks, fact progressively accumulates the value of 5! (five factorial).
Recursive calls Main 1 2 3 Value for num in LOCAL-STORAGE 5 4 3 2 Value for numb in WORKING-STORAGE 5 4 3 2 Value for fact in WORKING-STORAGE 0 0 0 0
17
Recursive calls 4 5
Gobacks 5 4 3 2 1 Main
RELATED CONCEPTS
Sharing data in separately compiled programs Sharing data in nested programs Sharing data in recursive or multithreaded programs on page 19 Passing data on page 465
18
A nested program can also access data items in a sibling program (one at the same nesting level in the same containing program) that is declared with the COMMON attribute.
RELATED CONCEPTS
Making recursive calls on page 461 Processing files with multithreading on page 496
RELATED REFERENCES
You can code the division header to receive parameters by using the USING phrase, or to return a value by using the RETURNING phrase. To receive an argument that was passed by reference (the default) or by content, code the division header for a program in either of these ways:
PROCEDURE DIVISION USING dataname PROCEDURE DIVISION USING BY REFERENCE dataname
19
To receive a parameter that was passed by value, code the division header for a program as follows:
PROCEDURE DIVISION USING BY VALUE dataname
You can also combine USING and RETURNING in a PROCEDURE DIVISION header:
PROCEDURE DIVISION USING dataname RETURNING dataname2
The procedure division header (Enterprise COBOL Language Reference) The USING phrase (Enterprise COBOL Language Reference) CALL statement (Enterprise COBOL Language Reference)
20
Imperative statements
An imperative statement (such as ADD, MOVE, INVOKE, or CLOSE) indicates an unconditional action to be taken. You can end an imperative statement with an implicit or explicit scope terminator. A conditional statement that ends with an explicit scope terminator becomes an imperative statement called a delimited scope statement. Only imperative statements (or delimited scope statements) can be nested.
RELATED CONCEPTS
Conditional statements
A conditional statement is either a simple conditional statement (IF, EVALUATE, SEARCH) or a conditional statement made up of an imperative statement that includes a conditional phrase or option. You can end a conditional statement with an implicit or explicit scope terminator. If you end a conditional statement explicitly, it becomes a delimited scope statement (which is an imperative statement). You can use a delimited scope statement in these ways: v To delimit the range of operation for a COBOL conditional statement and to explicitly show the levels of nesting For example, use an END-IF phrase instead of a period to end the scope of an IF statement within a nested IF. v To code a conditional statement where the COBOL syntax calls for an imperative statement For example, code a conditional statement as the object of an inline PERFORM:
PERFORM UNTIL TRANSACTION-EOF PERFORM 200-EDIT-UPDATE-TRANSACTION IF NO-ERRORS PERFORM 300-UPDATE-COMMUTER-RECORD ELSE PERFORM 400-PRINT-TRANSACTION-ERRORS END-IF READ UPDATE-TRANSACTION-FILE INTO WS-TRANSACTION-RECORD AT END SET TRANSACTION-EOF TO TRUE END-READ END-PERFORM
An explicit scope terminator is required for the inline PERFORM statement, but it is not valid for the out-of-line PERFORM statement. For additional program control, you can use the NOT phrase with conditional statements. For example, you can provide instructions to be performed when a
Chapter 1. Structuring your program
21
particular exception does not occur, such as NOT ON SIZE ERROR. The NOT phrase cannot be used with the ON OVERFLOW phrase of the CALL statement, but it can be used with the ON EXCEPTION phrase. Do not nest conditional statements. Nested statements must be imperative statements (or delimited scope statements) and must follow the rules for imperative statements. The following statements are examples of conditional statements if they are coded without scope terminators: v Arithmetic statement with ON SIZE ERROR v v v v Data-manipulation statements with ON OVERFLOW CALL statements with ON OVERFLOW I/O statements with INVALID KEY, AT END, or AT END-OF-PAGE RETURN with AT END
RELATED CONCEPTS
Compiler-directing statements
A compiler-directing statement causes the compiler to take specific action about the program structure, COPY processing, listing control, or control flow. A compiler-directing statement is not part of the program logic.
RELATED REFERENCES
Chapter 18, Compiler-directing statements, on page 363 Compiler-directing statements (Enterprise COBOL Language Reference)
Scope terminators
A scope terminator ends a verb or statement. Scope terminators can be explicit or implicit. Explicit scope terminators end a verb without ending a sentence. They consist of END followed by a hyphen and the name of the verb being terminated, such as END-IF. An implicit scope terminator is a period (.) that ends the scope of all previous statements not yet ended. Each of the two periods in the following program fragment ends an IF statement, making the code equivalent to the code after it that instead uses explicit scope terminators:
IF ITEM = "A" DISPLAY "THE VALUE OF ITEM IS " ITEM ADD 1 TO TOTAL MOVE "C" TO ITEM DISPLAY "THE VALUE OF ITEM IS NOW " ITEM. IF ITEM = "B" ADD 2 TO TOTAL.
22
IF ITEM = "A" DISPLAY "THE VALUE OF ITEM IS " ITEM ADD 1 TO TOTAL MOVE "C" TO ITEM DISPLAY "THE VALUE OF ITEM IS NOW " ITEM END-IF IF ITEM = "B" ADD 2 TO TOTAL END-IF
If you use implicit terminators, the end of statements can be unclear. As a result, you might end statements unintentionally, changing your programs logic. Explicit scope terminators make a program easier to understand and prevent unintentional ending of statements. For example, in the program fragment below, changing the location of the first period in the first implicit scope example changes the meaning of the code:
IF ITEM = "A" DISPLAY "VALUE OF ITEM IS " ITEM ADD 1 TO TOTAL. MOVE "C" TO ITEM DISPLAY " VALUE OF ITEM IS NOW " ITEM IF ITEM = "B" ADD 2 TO TOTAL.
The MOVE statement and the DISPLAY statement after it are performed regardless of the value of ITEM, despite what the indentation indicates, because the first period terminates the IF statement. For improved program clarity and to avoid unintentional ending of statements, use explicit scope terminators, especially within paragraphs. Use implicit scope terminators only at the end of a paragraph or the end of a program. Be careful when coding an explicit scope terminator for an imperative statement that is nested within a conditional statement. Ensure that the scope terminator is paired with the statement for which it was intended. In the following example, the scope terminator will be paired with the second READ statement, though the programmer intended it to be paired with the first.
READ FILE1 AT END MOVE A TO B READ FILE2 END-READ
To ensure that the explicit scope terminator is paired with the intended statement, the preceding example can be recoded in this way:
READ FILE1 AT END MOVE A TO B READ FILE2 END-READ END-READ
RELATED CONCEPTS
Declaratives
Declaratives provide one or more special-purpose sections that are executed when an exception condition occurs.
Chapter 1. Structuring your program
23
Start each declarative section with a USE statement that identifies the function of the section. In the procedures, specify the actions to be taken when the condition occurs.
RELATED TASKS
24
Using variables, structures, literals, and constants Assigning values to data items on page 29 Displaying values on a screen or in a file (DISPLAY) on page 38 Using intrinsic functions (built-in functions) on page 40 Using tables (arrays) and pointers on page 41 Chapter 7, Processing data in an international environment, on page 121
variables data items and group items on page 26 literals on page 27 constants on page 28 figurative constants on page 28
RELATED REFERENCES
Using variables
A variable is a data item whose value can change during a program. The value is restricted, however, to the data type that you define when you specify a name and a length for the data item. For example, if a customer name is an alphanumeric data item in your program, you could define and use the customer name as shown below:
Data Division. 01 Customer-Name 01 Original-Customer-Name Pic X(20). Pic X(20).
25
You could instead declare the customer names above as national data items by specifying their PICTURE clauses as Pic N(20) and specifying the USAGE NATIONAL clause for the items. National data items are represented in Unicode UTF-16, in which most characters are represented in 2 bytes of storage.
RELATED CONCEPTS
NSYMBOL on page 331 Storage of character data on page 133 PICTURE clause (Enterprise COBOL Language Reference)
You could instead define Customer-Record as a national group item that is composed of two subordinate national group items by changing the declarations in
26
the DATA DIVISION as shown below. National group items behave in the same way as elementary category national data items in most operations. The GROUP-USAGE NATIONAL clause indicates that a group item and any group items subordinate to it are national groups. Subordinate elementary items in a national group must be explicitly or implicitly described as USAGE NATIONAL.
Data Division. File Section. FD Customer-File Record Contains 90 Characters. 01 Customer-Record Group-Usage National. 05 Customer-Name. 10 Last-Name Pic n(17). 10 Filler Pic n. 10 Initials Pic nn. 05 Part-Order. 10 Part-Name Pic n(15). 10 Part-Color Pic n(10). Working-Storage Section. 01 Orig-Customer-Name Group-Usage National. 05 Surname Pic n(17). 05 Initials Pic n(3). 01 Inventory-Part-Name Pic n(15) Usage National. . . . Procedure Division. Move Customer-Name to Orig-Customer-Name Move Part-Name to Inventory-Part-Name . . .
In the example above, the group items could instead specify the USAGE NATIONAL clause at the group level. A USAGE clause at the group level applies to each elementary data item in a group (and thus serves as a convenient shorthand notation). However, a group that specifies the USAGE NATIONAL clause is not a national group despite the representation of the elementary items within the group. Groups that specify the USAGE clause are alphanumeric groups and behave in many operations, such as moves and compares, like elementary data items of USAGE DISPLAY (except that no editing or conversion of data occurs).
RELATED CONCEPTS
Unicode and the encoding of language characters on page 125 National groups on page 129
RELATED TASKS
Using national data (Unicode) in COBOL on page 126 Using national groups on page 130
RELATED REFERENCES
FILE SECTION entries on page 14 Storage of character data on page 133 Classes and categories of group items (Enterprise COBOL Language Reference) PICTURE clause (Enterprise COBOL Language Reference) MOVE statement (Enterprise COBOL Language Reference) USAGE clause (Enterprise COBOL Language Reference)
Using literals
A literal is a character string whose value is given by the characters themselves. If you know the value you want a data item to have, you can use a literal representation of the data value in the PROCEDURE DIVISION.
27
You do not need to declare a data item for the value nor refer to it by using a data-name. For example, you can prepare an error message for an output file by moving an alphanumeric literal:
Move "Name is not valid" To Customer-Name
You can compare a data item to a specific integer value by using a numeric literal. In the example below, "Name is not valid" is an alphanumeric literal, and 03519 is a numeric literal:
01 Part-number Pic 9(5). . . . If Part-number = 03519 then display "Part number was found"
You can use the opening delimiter N" or N' to designate a national literal if the NSYMBOL(NATIONAL) compiler option is in effect, or to designate a DBCS literal if the NSYMBOL(DBCS) compiler option is in effect. You can use the opening delimiter NX" or NX' to designate national literals in hexadecimal notation (regardless of the setting of the NSYMBOL compiler option). Each group of four hexadecimal digits designates a single national character.
RELATED CONCEPTS
Using national literals on page 127 Using DBCS literals on page 142
RELATED REFERENCES
Using constants
A constant is a data item that has only one value. COBOL does not define a construct for constants. However, you can define a data item with an initial value by coding a VALUE clause in the data description (instead of coding an INITIALIZE statement).
Data Division. 01 Report-Header . . . 01 Interest pic x(50) value "Company Sales Report".
The example above initializes an alphanumeric and a numeric data item. You can likewise use a VALUE clause in defining a national or DBCS constant.
RELATED TASKS
Using national data (Unicode) in COBOL on page 126 Coding for use of DBCS support on page 141
28
For example:
Move Spaces To Report-Header
RELATED TASKS
Using national-character figurative constants on page 128 Coding for use of DBCS support on page 141
RELATED REFERENCES
Examine or replace characters or groups of characters in a data Use the INSPECT statement. item. Receive values from a file. Receive values from a system input device or a file. Establish a constant. Use the READ (or READ INTO) statement. Use the ACCEPT statement. Use the VALUE clause in the definition of the data item, and do not use the data item as a receiver. Such an item is in effect a constant even though the compiler does not enforce read-only constants. Use the SET statement.
One of these actions: v Place a value associated with a table element in an index. v Set the status of an external switch to ON or OFF. v Move data to a condition-name to make the condition true. v Set a POINTER, PROCEDURE-POINTER, or FUNCTION-POINTER data item to an address. v Associate an OBJECT REFERENCE data item with an object instance.
Initializing a structure (INITIALIZE) on page 32 Assigning values to elementary data items (MOVE) on page 34 Assigning values to group data items (MOVE) on page 35 Assigning input from a screen or file (ACCEPT) on page 37 Joining data items (STRING) on page 101
Chapter 2. Using data
29
Splitting data items (UNSTRING) on page 103 Assigning arithmetic results (MOVE or COMPUTE) on page 36 Tallying and replacing data items (INSPECT) on page 111 Chapter 7, Processing data in an international environment, on page 121
1. The symbol b represents a blank space. 2. Hexadecimal representation of the national (UTF-16) characters AB1. The example assumes that identifier-1 has Usage National. 3. Hexadecimal representation of the national (UTF-16) characters (three blank spaces). Note that if identifier-1 were not defined as Usage National, and if NSYMBOL(DBCS) were in effect, INITIALIZE would instead store DBCS spaces (4040) into identifier-1.
30
ALPHABETIC-1 ABCD
1. Hexadecimal representation of the national characters CD 2. Hexadecimal representation of the national characters AB
1. Hexadecimal representation of the national characters 456 2. Hexadecimal representation of the national characters 123 3. Hexadecimal representation of the national characters 045
31
1. Hexadecimal representation of the national characters 987 2. Hexadecimal representation of the national characters 012
1. Hexadecimal representation of the national characters 012 2. Hexadecimal representation of the national characters $127 3. Hexadecimal representation of the national characters $ 12
RELATED TASKS
Initializing a structure (INITIALIZE) Initializing a table (INITIALIZE) on page 76 Defining numeric data on page 45
RELATED REFERENCES
32
identical in each record that is produced. (The transaction record is defined as an alphanumeric group item, TRANSACTION-OUT.)
01 TRANSACTION-OUT. 05 TRANSACTION-CODE PIC X. 05 PART-NUMBER PIC 9(6). 05 TRANSACTION-QUANTITY PIC 9(5). 05 PRICE-FIELDS. 10 UNIT-PRICE PIC 9(5)V9(2). 10 DISCOUNT PIC V9(2). 10 SALES-PRICE PIC 9(5)V9(2). . . . INITIALIZE TRANSACTION-OUT Record 1 2 3 4 5 TRANSACTION-OUT before R001383000240000000000000000 R001390000480000000000000000 S001410000120000000000000000 C001383000000000425000000000 C002010000000000000100000000 TRANSACTION-OUT after b0000000000000000000000000001 b0000000000000000000000000001 b0000000000000000000000000001 b0000000000000000000000000001 b0000000000000000000000000001
You can likewise reset the values of all the subordinate data items in a national group item by applying the INITIALIZE statement to that group item. The following structure is similar to the preceding structure, but instead uses Unicode UTF-16 data:
01 TRANSACTION-OUT GROUP-USAGE 05 TRANSACTION-CODE 05 PART-NUMBER 05 TRANSACTION-QUANTITY 05 PRICE-FIELDS. 10 UNIT-PRICE 10 DISCOUNT 10 SALES-PRICE . . . INITIALIZE TRANSACTION-OUT NATIONAL. PIC N. PIC 9(6). PIC 9(5). PIC 9(5)V9(2). PIC V9(2). PIC 9(5)V9(2).
Regardless of the previous contents of the transaction record, after the INITIALIZE statement above is executed: v TRANSACTION-CODE contains NX"0020" (a national space). v Each of the remaining 27 national character positions of TRANSACTION-OUT contains NX"0030" (a national-decimal zero). When you use an INITIALIZE statement to initialize an alphanumeric or national group data item, the data item is processed as a group item, that is, with group semantics. The elementary data items within the group are recognized and processed, as shown in the examples above. If you do not code the REPLACING phrase of the INITIALIZE statement: v SPACE is the implied sending item for alphabetic, alphanumeric, alphanumeric-edited, DBCS, category national, and national-edited receiving items. v ZERO is the implied sending item for numeric and numeric-edited receiving items.
RELATED CONCEPTS
33
RELATED TASKS
If Customer-Name is longer than Orig-Customer-Name, truncation occurs on the right. If Customer-Name is shorter, the extra character positions on the right in Orig-Customer-Name are filled with spaces. For data items that contain numbers, moves can be more complicated than with character data items because there are several ways in which numbers can be represented. In general, the algebraic values of numbers are moved if possible, as opposed to the digit-by-digit moves that are performed with character data. For example, after the MOVE statement below, Item-x contains the value 3.0, represented as 0030:
01 Item-x Pic 999v9. . . . Move 3.06 to Item-x
You can move an alphabetic, alphanumeric, alphanumeric-edited, DBCS, integer, or numeric-edited data item to a category national or national-edited data item; the sending item is converted. You can move a national data item to a category national or national-edited data item. If the content of a category national data item has a numeric value, you can move that item to a numeric, numeric-edited, external floating-point, or internal floating-point data item. You can move a national-edited data item only to a category national data item or another national-edited data item. Padding or truncation might occur. For complete details about elementary moves, see the related reference below about the MOVE statement. The following example shows an alphanumeric data item in the Greek language that is moved to a national data item:
CBL CODEPAGE(00875) . . . 01 Data-in-Unicode Pic N(100) usage national. 01 Data-in-Greek Pic X(100). . . . Read Greek-file into Data-in-Greek Move Data-in-Greek to Data-in-Unicode
RELATED CONCEPTS
34
RELATED TASKS
Assigning values to group data items (MOVE) Converting to or from national (Unicode) representation on page 134
RELATED REFERENCES
CODEPAGE on page 310 Classes and categories of data (Enterprise COBOL Language Reference) MOVE statement (Enterprise COBOL Language Reference)
In the example above, State and Month within Group-N receive the values in national representation of State and Month, respectively, from Group-X. The other data items in Group-N are unchanged. (Filler items in a receiving group item are unchanged by a MOVE CORRESPONDING statement.) In a MOVE CORRESPONDING statement, sending and receiving group items are treated as group items, not as elementary data items; group semantics apply. That is, the elementary data items within each group are recognized, and the results are the same as if each pair of corresponding data items were referenced in a separate MOVE statement. Data conversions are performed according to the rules for the MOVE statement as specified in the related reference below. For details about which types of elementary data items correspond, see the related reference about the CORRESPONDING phrase.
35
RELATED CONCEPTS
Unicode and the encoding of language characters on page 125 National groups on page 129
RELATED TASKS
Assigning values to elementary data items (MOVE) on page 34 Using national groups on page 130 Converting to or from national (Unicode) representation on page 134
RELATED REFERENCES
Classes and categories of group items (Enterprise COBOL Language Reference) MOVE statement (Enterprise COBOL Language Reference) CORRESPONDING phrase (Enterprise COBOL Language Reference)
In the example above, the two statements in most cases have the same effect. The MOVE statement however carries out the assignment with truncation. You can use the DIAGTRUNC compiler option to request that the compiler issue a warning for MOVE statements that might truncate numeric receivers. When significant left-order digits would be lost in execution, the COMPUTE statement can detect the condition and allow you to handle it. If you use the ON SIZE ERROR phrase of the COMPUTE statement, the compiler generates code to detect a size-overflow condition. If the condition occurs, the code in the ON SIZE ERROR phrase is performed, and the content of z remains unchanged. If you do not specify the ON SIZE ERROR phrase, the assignment is carried out with truncation. There is no ON SIZE ERROR support for the MOVE statement. You can also use the COMPUTE statement to assign the result of an arithmetic expression or intrinsic function to a data item. For example:
Compute z = y + (x ** 3) Compute x = Function Max(x y z)
You can assign the results of date, time, mathematical, and other calculations to data items by using Language Environment callable services. Language Environment services are available through a standard COBOL CALL statement, and the values they return are passed in the parameters of the CALL statement. For example, you can call the Language Environment service CEESIABS to find the absolute value of a data item by coding the following statement:
Call 'CEESIABS' Using Arg, Feedback-code, Result.
As a result of this call, data item Result is assigned the absolute value of the value in data item Arg; data item Feedback-code contains the return code that indicates whether the service completed successfully. You have to define all the data items in the DATA DIVISION using the correct descriptions according to the requirements of the particular callable service. For the example above, the data items could be defined as follows:
77 Arg Pic s9(9) Binary. 77 Feedback-code Pic x(12) Display. 77 Result Pic s9(9) Binary.
36
RELATED REFERENCES
DIAGTRUNC on page 318 Intrinsic functions (Enterprise COBOL Language Reference) Language Environment Programming Reference (Callable services)
To read from a file instead of the screen, make the following change: v Change Console to device, where device is any valid system device (for example, SYSIN). For example:
SYSIN is Names-Input
device can be a ddname that references a hierarchical file system (HFS) path. If this ddname is not defined and your program is running in the z/OS UNIX environment, stdin is the input source. If this ddname is not defined and your program is not running in the z/OS UNIX environment, the ACCEPT statement fails. When you use the ACCEPT statement, you can assign a value to an alphanumeric or national group item, or to an elementary data item that has USAGE DISPLAY, USAGE DISPLAY-1, or USAGE NATIONAL. When you assign a value to a USAGE NATIONAL data item, input data from the console is converted from the EBCDIC code page specified in the CODEPAGE compiler option to national (Unicode UTF-16) representation. This is the only case where conversion of national data is done when you use the ACCEPT statement. Conversion is done in this case because the input is known to be coming from a screen. To have conversion done when the input data is from any other device, use the NATIONAL-OF intrinsic function.
RELATED CONCEPTS
CODEPAGE on page 310 ACCEPT statement (Enterprise COBOL Language Reference) SPECIAL-NAMES paragraph (Enterprise COBOL Language Reference)
37
In the example above, if the content of data item Customer-Name is JOHNSON, then the statement displays the following message on the system logical output device:
No entry for surname 'JOHNSON' found in the file.
To write data to a destination other than the system logical output device, use the UPON phrase with a destination other than SYSOUT. For example, the following statement writes to the file specified in the SYSPUNCH DD statement:
Display "Hello" upon syspunch.
You can specify a file in the HFS by using the SYSPUNCH DD statement. For example, the following definition causes DISPLAY output to be written to the file /u/userid/cobol/demo.lst:
//SYSPUNCH DD PATH='/u/userid/cobol/demo.lst', // PATHOPTS=(OWRONLY,OCREAT,OTRUNC),PATHMODE=SIRWXU, // FILEDATA=TEXT
The following statement writes to the job log or console and to the TSO screen if you are running under TSO:
Display "Hello" upon console.
When you display the value of a USAGE NATIONAL data item to the console, it is converted from Unicode (UTF-16) representation to EBCDIC based on the value of the CODEPAGE option. This is the only case where conversion of national data is done when you use the DISPLAY statement. Conversion is done in this case because the output is known to be directed to a screen. To have a national data item be converted when you direct output to a different device, use the DISPLAY-OF intrinsic function, such as in the following example:
01 Data-in-Unicode pic N(10) usage national. . . . Display function Display-of(Data-in-Unicode, 00037)
RELATED CONCEPTS
Displaying data on the system logical output device on page 39 Using WITH NO ADVANCING on page 39 Converting national to alphanumeric (DISPLAY-OF) on page 136 Coding COBOL programs to run under CICS on page 407
RELATED REFERENCES
38
The output is directed to the ddname that you specify in the OUTDD compiler option. You can specify a file in the hierarchical file system with this ddname. If the OUTDD ddname is not allocated and you are not running in the z/OS UNIX environment, a default DD of SYSOUT=* is allocated. If the OUTDD ddname is not allocated and you are running in the z/OS UNIX environment, the _IGZ_SYSOUT environment variable is used as follows: Undefined or set to stdout Output is routed to stdout (file descriptor 1). Set to stderr Output is routed to stderr (file descriptor 2). Otherwise (set to something other than stdout or stderr) The DISPLAY statement fails; a severity-3 Language Environment condition is raised. When DISPLAY output is routed to stdout or stderr, the output is not subdivided into records. The output is written as a single stream of characters without line breaks. If OUTDD and the Language Environment runtime option MSGFILE specify the same ddname, both DISPLAY output and Language Environment runtime diagnostics are routed to the Language Environment message file.
RELATED TASKS
39
If you code the statements above, the result sent to the output device is:
ABC CDEF +GHIJK +LMNOPQ RSTUVMX
The output that is printed depends on how the output device interprets printer control characters. If you do not specify the WITH NO ADVANCING phrase and the output is going to stdout or stderr, a newline character is appended to the end of the stream.
RELATED REFERENCES
A function-identifier represents both the invocation of the function and the data value returned by the function. Because it actually represents a data item, you can use a function-identifier in most places in the PROCEDURE DIVISION where a data item that has the attributes of the returned value can be used. The COBOL word function is a reserved word, but the function-names are not reserved. You can use them in other contexts, such as for the name of a data item. For example, you could use Sqrt to invoke an intrinsic function and to name a data item in your program:
Working-Storage Section. 01 x Pic 99 value 2. 01 y Pic 99 value 4. 01 z Pic 99 value 0. 01 Sqrt Pic 99 value 0.
40
A function-identifier represents a value that is of one of these types: alphanumeric, national, numeric, or integer. You can include a substring specification (reference modifier) in a function-identifier for alphanumeric or national functions. Numeric intrinsic functions are further classified according to the type of numbers they return. The functions MAX, MIN, DATEVAL, and UNDATE can return either type of value depending on the type of arguments you supply. The functions DATEVAL, UNDATE, and YEARWINDOW are provided with the millennium language extensions to assist with manipulating and converting windowed date fields. Functions can reference other functions as arguments provided that the results of the nested functions meet the requirements for the arguments of the outer function. For example, Function Sqrt(5) returns a numeric value. Thus, the three arguments to the MAX function below are all numeric, which is an allowable argument type for this function:
Compute x = Function Max((Function Sqrt(5)) 2.5 3.5)
RELATED TASKS
Processing table items using intrinsic functions on page 86 Converting data items (intrinsic functions) on page 112 Evaluating data items (intrinsic functions) on page 115
Defining a table (OCCURS) on page 69 Using procedure and function pointers on page 462
Chapter 2. Using data
41
42
Table 4. Effect of RMODE and RENT compiler options on the RMODE attribute (continued) RMODE compiler option RMODE(24) RMODE(ANY) RENT compiler option RENT or NORENT RENT or NORENT RMODE attribute RMODE 24 RMODE ANY
Link-edit considerations: When the object code that COBOL generates has an attribute of RMODE 24, you must link-edit it with RMODE 24. When the object code that COBOL generates has an attribute of RMODE ANY, you can link-edit it with RMODE ANY or RMODE 24.
43
AMODE switching on page 453 Language Environment Programming Guide (AMODE considerations for heap storage)
RELATED TASKS
Chapter 24, Using subprograms, on page 447 Chapter 25, Sharing data, on page 465
RELATED REFERENCES
Allocation of buffers for QSAM files on page 173 DATA on page 314 RENT on page 341 RMODE on page 342 Performance-related compiler options on page 672 Language Environment Programming Reference (HEAP, STACK, ALL31) MVS Program Management: Users Guide and Reference
44
Defining numeric data Displaying numeric data on page 47 Controlling how numeric data is stored on page 48 Checking for incompatible data (numeric class test) on page 56 Performing arithmetic on page 57 Using currency signs on page 67
You can similarly define numeric data items to hold national characters (UTF-16). For example, Count-n below is an external decimal data item that has USAGE NATIONAL (a national decimal item):
05 Count-n Pic 9(4) Value 25 Usage National.
You can code up to 18 digits in the PICTURE clause when you compile using the default compiler option ARITH(COMPAT) (referred to as compatibility mode). When you compile using ARITH(EXTEND) (referred to as extended mode), you can code up to 31 digits in the PICTURE clause. Other characters of special significance that you can code are: P
Copyright IBM Corp. 1991, 2009
45
S V
The field can therefore hold a positive or a negative value. The v indicates the position of an implied decimal point, but does not contribute to the size of the item because it does not require a storage position. An s usually does not contribute to the size of a numeric item, because by default s does not require a storage position. However, if you plan to port your program or data to a different machine, you might want to code the sign for a zoned decimal data item as a separate position in storage. In the following case, the sign takes 1 byte:
05 Price Pic s99V99 Sign Is Leading, Separate.
This coding ensures that the convention your machine uses for storing a nonseparate sign will not cause unexpected results on a machine that uses a different convention. Separate signs are also preferable for zoned decimal data items that will be printed or displayed. Separate signs are required for national decimal data items that are signed. The sign takes 2 bytes of storage, as in the following example:
05 Price Pic s99V99 Usage National Sign Is Leading, Separate.
You cannot use the PICTURE clause with internal floating-point data (COMP-1 or COMP-2). However, you can use the VALUE clause to provide an initial value for an internal floating-point literal:
05 Compute-result Usage Comp-2 Value 06.23E-24.
For information about external floating-point data, see the examples referenced below and the related concept about formats for numeric data. Examples: numeric data and internal representation on page 52
RELATED CONCEPTS
Formats for numeric data on page 49 Appendix A, Intermediate results and arithmetic precision, on page 687
RELATED TASKS
Displaying numeric data on page 47 Controlling how numeric data is stored on page 48 Performing arithmetic on page 57 Defining national numeric data items on page 129
RELATED REFERENCES
Sign representation of zoned and packed-decimal data on page 55 Storage of character data on page 133 ARITH on page 306 NUMPROC on page 333 SIGN clause (Enterprise COBOL Language Reference)
46
If the contents of Price are 0150099 (representing the value 1,500.99), $ 1,500.99 is displayed when you run the code. The z in the PICTURE clause of Edited-price indicates the suppression of leading zeros. You can define numeric-edited data items to hold national (UTF-16) characters instead of alphanumeric characters. To do so, declare the numeric-edited items as USAGE NATIONAL. The effect of the editing symbols is the same for numeric-edited items that have USAGE NATIONAL as it is for numeric-edited items that have USAGE DISPLAY, except that the editing is done with national characters. For example, if Edited-price is declared as USAGE NATIONAL in the code above, the item is edited and displayed using national characters. To display numeric or numeric-edited data items that have USAGE NATIONAL in EBCDIC, direct them to CONSOLE. For example, if Edited-price in the code above has USAGE NATIONAL, $ 1,500.99 is displayed when you run the program if the last statement above is:
Display Edited-price Upon Console
You can cause an elementary numeric or numeric-edited item to be filled with spaces when a value of zero is stored into it by coding the BLANK WHEN ZERO clause for the item. For example, each of the DISPLAY statements below causes blanks to be displayed instead of zeros:
05 Price Pic 9(5)v99. 05 Edited-price-D Pic $99,999.99 Blank When Zero. 05 Edited-price-N Pic $99,999.99 Usage National Blank When Zero. . . . Move 0 to Price Move Price to Edited-price-D Move Price to Edited-price-N Display Edited-price-D Display Edited-price-N upon console
You cannot use numeric-edited items as sending operands in arithmetic expressions or in ADD, SUBTRACT, MULTIPLY, DIVIDE, or COMPUTE statements. (Numeric editing takes place when a numeric-edited item is the receiving field for one of these statements, or when a MOVE statement has a numeric-edited receiving field and a numeric-edited or numeric sending field.) You use numeric-edited items primarily for displaying or printing numeric data.
47
You can move numeric-edited items to numeric or numeric-edited items. In the following example, the value of the numeric-edited item (whether it has USAGE DISPLAY or USAGE NATIONAL) is moved to the numeric item:
Move Edited-price to Price Display Price
If these two statements immediately followed the statements in the first example above, then Price would be displayed as 0150099, representing the value 1,500.99. Price would also be displayed as 0150099 if Edited-price had USAGE NATIONAL. You can also move numeric-edited items to alphanumeric, alphanumeric-edited, floating-point, and national data items. For a complete list of the valid receiving items for numeric-edited data, see the related reference about the MOVE statement. Examples: numeric data and internal representation on page 52
RELATED TASKS
Displaying values on a screen or in a file (DISPLAY) on page 38 Controlling how numeric data is stored Defining numeric data on page 45 Performing arithmetic on page 57 Defining national numeric data items on page 129 Converting to or from national (Unicode) representation on page 134
RELATED REFERENCES
MOVE statement (Enterprise COBOL Language Reference) BLANK WHEN ZERO clause (Enterprise COBOL Language Reference)
48
v Internal floating point (USAGE COMP-1 or USAGE COMP-2) COMP and COMP-4 are synonymous with BINARY, and COMP-3 is synonymous with PACKED-DECIMAL. The compiler converts displayable numbers to the internal representation of their numeric values before using them in arithmetic operations. Therefore it is often more efficient if you define data items as BINARY or PACKED-DECIMAL than as DISPLAY or NATIONAL. For example:
05 Initial-count Pic S9(4) Usage Binary Value 1000.
Regardless of which USAGE clause you use to control the internal representation of a value, you use the same PICTURE clause conventions and decimal value in the VALUE clause (except for internal floating-point data, for which you cannot use a PICTURE clause). Examples: numeric data and internal representation on page 52
RELATED CONCEPTS
Formats for numeric data Data format conversions on page 54 Appendix A, Intermediate results and arithmetic precision, on page 687
RELATED TASKS
Defining numeric data on page 45 Displaying numeric data on page 47 Performing arithmetic on page 57
RELATED REFERENCES
Conversions and precision on page 54 Sign representation of zoned and packed-decimal data on page 55
49
files, terminals, or printers. You can also use external decimal items as operands and receivers in arithmetic processing. However, if your program performs a lot of intensive arithmetic, and efficiency is a high priority, COBOLs computational numeric types might be a better choice for the data items used in the arithmetic.
The minus signs (-) do not mean that the mantissa and exponent must necessarily be negative numbers. Instead, they mean that when the number is displayed, the sign appears as a blank for positive numbers or a minus sign for negative numbers. If you instead code a plus sign (+), the sign appears as a plus sign for positive numbers or a minus sign for negative numbers. When USAGE NATIONAL is in effect for a floating-point data item, each PICTURE character position (except for v, if used) takes 2 bytes of storage. The items are stored as national characters (UTF-16). External floating-point items that have USAGE NATIONAL are referred to as national floating-point data items. The existing rules for display floating-point items apply to national floating-point items. In the following example, Compute-Result-N is a national floating-point item:
05 Compute-Result-N Pic -9v9(9)E-99 Usage National.
If Compute-Result-N is displayed, the signs appear as described above for Compute-Result, but in national characters. To instead display Compute-Result-N in EBCDIC characters, direct it to the console:
Display Compute-Result-N Upon Console
You cannot use the VALUE clause for external floating-point items. As with external decimal numbers, external floating-point numbers have to be converted (by the compiler) to an internal representation of their numeric value before they can be used in arithmetic operations. If you compile with the default option ARITH (COMPAT), external floating-point numbers are converted to long (64-bit) floating-point format. If you compile with ARITH (EXTEND), they are instead converted to extended-precision (128-bit) floating-point format.
50
A binary number with a PICTURE description of four or fewer decimal digits occupies 2 bytes; five to nine decimal digits, 4 bytes; and 10 to 18 decimal digits, 8 bytes. Binary items with nine or more digits require more handling by the compiler. Testing them for the SIZE ERROR condition and rounding is more cumbersome than with other types. You can use binary items, for example, for indexes, subscripts, switches, and arithmetic operands or results. Use the TRUNC(STD|OPT|BIN) compiler option to indicate how binary data (BINARY, COMP, or COMP-4) is to be truncated.
You can specify scaling (that is, decimal positions or implied integer positions) in the PICTURE clause of COMP-5 items. If you do so, you must appropriately scale the maximal capacities listed above. For example, a data item you describe as PICTURE S99V99 COMP-5 is represented in storage as a binary halfword, and supports a range of values from -327.68 through +327.67. Large literals in VALUE clauses: Literals specified in VALUE clauses for COMP-5 items can, with a few exceptions, contain values of magnitude up to the capacity of the native binary representation. See Enterprise COBOL Language Reference for the exceptions. Regardless of the setting of the TRUNC compiler option, COMP-5 data items behave like binary data does in programs compiled with TRUNC(BIN).
Chapter 3. Working with numbers and arithmetic
51
Unicode and the encoding of language characters on page 125 Appendix A, Intermediate results and arithmetic precision, on page 687
RELATED TASKS
Defining numeric data on page 45 Defining national numeric data items on page 129
RELATED REFERENCES
Storage of character data on page 133 TRUNC on page 353 Classes and categories of data (Enterprise COBOL Language Reference) SIGN clause (Enterprise COBOL Language Reference) VALUE clause (Enterprise COBOL Language Reference)
52
Table 6. Internal representation of numeric items Numeric type PICTURE and USAGE and optional SIGN clause Value + 1234 - 1234 1234 PIC 9999 DISPLAY PIC 9999 NATIONAL PIC S9999 DISPLAY SIGN LEADING PIC S9999 DISPLAY SIGN LEADING SEPARATE PIC S9999 DISPLAY SIGN TRAILING SEPARATE PIC S9999 NATIONAL SIGN LEADING SEPARATE PIC S9999 NATIONAL SIGN TRAILING SEPARATE Binary PIC S9999 BINARY PIC S9999 COMP PIC S9999 COMP-4 PIC S9999 COMP-5 1234 1234 + 1234 - 1234 + 1234 - 1234 + 1234 - 1234 + 1234 - 1234 + 1234 - 1234 + 1234 - 1234 + 123451 - 12345 PIC 9999 PIC 9999 PIC 9999 PIC 9999 BINARY COMP COMP-4 COMP-5 1234
1
Internal representation F1 F2 F3 C4 F1 F2 F3 D4 F1 F2 F3 C4 F1 F2 F3 F4 00 31 00 32 00 33 00 34 C1 F2 F3 F4 D1 F2 F3 F4 4E F1 F2 F3 F4 60 F1 F2 F3 F4 F1 F2 F3 F4 4E F1 F2 F3 F4 60 00 2B 00 31 00 32 00 33 00 34 00 2D 00 31 00 32 00 33 00 34 00 31 00 32 00 33 00 34 00 2B 00 31 00 32 00 33 00 34 00 2D 04 D2 FB 2E 30 39 CF C7 04 D2
EA 60 01 23 4C 01 23 4D 01 23 4F 43 4D 20 00 C3 4D 20 00 43 4D 20 00 00 00 00 00 C3 4D 20 00 00 00 00 00 4E F1 F2 4B F3 F4 C5 4E F0 F2 60 F1 F2 4B F3 F4 C5 4E F0 F2 00 2B 00 31 00 32 00 2E 00 33 00 34 00 45 00 2B 00 30 00 32 00 2D 00 31 00 32 00 2E 00 33 00 34 00 45 00 2B 00 30 00 32
Internal decimal PIC S9999 PACKED-DECIMAL PIC S9999 COMP-3 PIC 9999 PIC 9999 Internal floating point COMP-1 PACKED-DECIMAL COMP-3
COMP-2
+ 1234 - 1234
1. The example demonstrates that COMP-5 data items can contain values of magnitude up to the capacity of the native binary representation (2, 4, or 8 bytes), rather than being limited to the value implied by the number of 9s in the PICTURE clause.
53
Conversions and precision Sign representation of zoned and packed-decimal data on page 55
54
When a USAGE COMP-2 data item is moved to a fixed-point data item that has more than 18 digits, the fixed-point data item will receive only 18 significant digits, and the remaining digits will be zero.
55
NUMPROC(PFD) Given X'sd', where s is the sign representation and d represents the digit, when you use NUMPROC(PFD), the compiler assumes that the sign in your data is one of three preferred signs: Signed positive or 0: X'C' Signed negative: X'D' Unsigned or alphanumeric: X'F' Based on this assumption, the compiler uses whatever sign it is given to process data. The preferred sign is generated only where necessary (for example, when unsigned data is moved to signed data). Using the NUMPROC(PFD) option can save processing time, but you must use preferred signs with your data for correct processing. NUMPROC(NOPFD) When the NUMPROC(NOPFD) compiler option is in effect, the compiler accepts any valid sign configuration. The preferred sign is always generated in the receiver. NUMPROC(NOPFD) is less efficient than NUMPROC(PFD), but you should use it whenever data that does not use preferred signs might exist. If an unsigned, zoned-decimal sender is moved to an alphanumeric receiver, the sign is unchanged (even with NUMPROC(NOPFD) in effect). NUMPROC(MIG) When NUMPROC(MIG) is in effect, the compiler generates code that is similar to that produced by OS/VS COBOL. This option can be especially useful if you migrate OS/VS COBOL programs to IBM Enterprise COBOL for z/OS.
RELATED REFERENCES
56
The numeric class test checks the contents of a data item against a set of values that are valid for the PICTURE and USAGE of the data item. For example, a packed-decimal item is checked for hexadecimal values X0 through X9 in the digit positions and for a valid sign value in the sign position (whether separate or nonseparate). For zoned decimal and packed-decimal items, the numeric class test is affected by the NUMPROC compiler option and the NUMCLS option (which is set at installation time). To determine the NUMCLS setting used at your installation, consult your system programmer. If NUMCLS(PRIM) is in effect at your installation, use the following table to find the values that the compiler considers valid for the sign.
Table 7. NUMCLS(PRIM) and valid signs NUMPROC(NOPFD) Signed Unsigned Separate sign C, D, F F +, NUMPROC(PFD) C, D, +0 (positive zero) F +, -, +0 (positive zero) NUMPROC(MIG) C, D, F F +, -
If NUMCLS(ALT) is in effect at your installation, use the following table to find the values that the compiler considers valid for the sign.
Table 8. NUMCLS(ALT) and valid signs NUMPROC(NOPFD) Signed Unsigned Separate sign A to F F +, NUMPROC(PFD) C, D, +0 (positive zero) F +, -, +0 (positive zero) NUMPROC(MIG) A to F F +, -
RELATED REFERENCES
Performing arithmetic
You can use any of several COBOL language features (including COMPUTE, arithmetic expressions, numeric intrinsic functions, and math and date callable services) to perform arithmetic. Your choice depends on whether a feature meets your particular needs. For most common arithmetic evaluations, the COMPUTE statement is appropriate. If you need to use numeric literals, numeric data, or arithmetic operators, you might want to use arithmetic expressions. In places where numeric expressions are allowed, you can save time by using numeric intrinsic functions. Language Environment callable services for mathematical functions and for date and time operations also provide a means of assigning arithmetic results to data items.
RELATED TASKS
57
arithmetic expressions numeric intrinsic functions on page 59 math-oriented callable services on page 60 date callable services on page 62
Some arithmetic calculations might be more intuitive using arithmetic statements other than COMPUTE. For example:
COMPUTE Compute Increment = Increment + 1 Compute Balance = Balance - Overdraft Compute IncrementOne = IncrementOne + 1 Compute IncrementTwo = IncrementTwo + 1 Compute IncrementThree = IncrementThree + 1 Equivalent arithmetic statements Add 1 to Increment Subtract Overdraft from Balance Add 1 to IncrementOne, IncrementTwo, IncrementThree
You might also prefer to use the DIVIDE statement (with its REMAINDER phrase) for division in which you want to process a remainder. The REM intrinsic function also provides the ability to process a remainder. When you perform arithmetic calculations, you can use national decimal data items as operands just as you use zoned decimal data items. You can also use national floating-point data items as operands just as you use display floating-point operands.
RELATED CONCEPTS
Fixed-point contrasted with floating-point arithmetic on page 64 Appendix A, Intermediate results and arithmetic precision, on page 687
RELATED TASKS
58
Arithmetic expressions can consist of a single numeric literal, a single numeric data item, or a single intrinsic function reference. They can also consist of several of these items connected by arithmetic operators. Arithmetic operators are evaluated in the following order of precedence:
Table 9. Order of evaluation of arithmetic operators Operator Unary + or ** / or * Binary + or Meaning Algebraic sign Exponentiation Division or multiplication Addition or subtraction Order of evaluation First Second Third Last
Operators at the same level of precedence are evaluated from left to right; however, you can use parentheses to change the order of evaluation. Expressions in parentheses are evaluated before the individual operators are evaluated. Parentheses, whether necessary or not, make your program easier to read.
RELATED CONCEPTS
Fixed-point contrasted with floating-point arithmetic on page 64 Appendix A, Intermediate results and arithmetic precision, on page 687
59
Table 10. Numeric intrinsic functions Number handling LENGTH MAX MIN NUMVAL NUMVAL-C ORD-MAX ORD-MIN Date and time CURRENT-DATE DATE-OF-INTEGER DATE-TO-YYYYMMDD DATEVAL DAY-OF-INTEGER DAY-TO-YYYYDDD INTEGER-OF-DATE INTEGER-OF-DAY UNDATE WHEN-COMPILED YEAR-TO-YYYY YEARWINDOW Finance ANNUITY PRESENT-VALUE Mathematics ACOS ASIN ATAN COS FACTORIAL INTEGER INTEGER-PART LOG LOG10 MOD REM SIN SQRT SUM TAN Statistics MEAN MEDIAN MIDRANGE RANDOM RANGE STANDARD-DEVIATION VARIANCE
Examples: numeric intrinsic functions on page 62 You can reference one function as the argument of another. A nested function is evaluated independently of the outer function (except when the compiler determines whether a mixed function should be evaluated using fixed-point or floating-point instructions). You can also nest an arithmetic expression as an argument to a numeric function. For example, in the statement below, there are three function arguments (a, b, and the arithmetic expression (c / d)):
Compute x = Function Sum(a b (c / d))
You can reference all the elements of a table (or array) as function arguments by using the ALL subscript. You can also use the integer special registers as arguments wherever integer arguments are allowed. Many of the capabilities of numeric intrinsic functions are also provided by Language Environment callable services.
RELATED CONCEPTS
Fixed-point contrasted with floating-point arithmetic on page 64 Appendix A, Intermediate results and arithmetic precision, on page 687
RELATED REFERENCES
60
ARITH(EXTEND), COBOL floating-point intrinsic functions (with the exception of RANDOM) return extended-precision (128-bit) results. For example (considering the first row of the table below), if you compile using ARITH(COMPAT), CEESDACS returns the same result as ACOS. If you compile using ARITH(EXTEND), CEESQACS returns the same result as ACOS.
Table 11. Compatibility of math intrinsic functions and callable services COBOL intrinsic function ACOS ASIN ATAN COS LOG LOG10 RANDOM REM SIN SQRT TAN
1
Corresponding Corresponding Results same for intrinsic long-precision Language extended-precision Language function and callable Environment callable service Environment callable service service? CEESDACS CEESDASN CEESDATN CEESDCOS CEESDLOG CEESDLG1 CEERAN0 CEESDMOD CEESDSIN CEESDSQT CEESDTAN CEESQACS CEESQASN CEESQATN CEESQCOS CEESQLOG CEESQLG1 none CEESQMOD CEESQSIN CEESQSQT CEESQTAN Yes Yes Yes Yes Yes Yes No Yes Yes Yes Yes
1. RANDOM returns a long (64-bit) floating-point result even if you pass it a 31-digit argument and compile with ARITH(EXTEND).
Both the RANDOM intrinsic function and CEERAN0 service generate random numbers between zero and one. However, because each uses its own algorithm, RANDOM and CEERAN0 produce different random numbers from the same seed. Even for functions that produce the same results, how you use intrinsic functions and Language Environment callable services differs. The rules for the data types required for intrinsic function arguments are less restrictive. For numeric intrinsic functions, you can use arguments that are of any numeric data type. When you invoke a Language Environment callable service with a CALL statement, however, you must ensure that the parameters match the numeric data types (generally COMP-1 or COMP-2) required by that service. The error handling of intrinsic functions and Language Environment callable services sometimes differs. If you pass an explicit feedback token when calling the Language Environment math services, you must check the feedback code after each call and take explicit action to deal with errors. However, if you call with the feedback token explicitly OMITTED, you do not need to check the token; Language Environment automatically signals any errors.
RELATED CONCEPTS
Fixed-point contrasted with floating-point arithmetic on page 64 Appendix A, Intermediate results and arithmetic precision, on page 687
RELATED TASKS
61
RELATED REFERENCES
When the default setting of INTDATE(ANSI) is in effect, COBOL uses January 1, 1601 as day 1. The following table compares the results when INTDATE(ANSI) is in effect.
Table 13. INTDATE(ANSI) and compatibility of date intrinsic functions and callable services Language Environment callable COBOL intrinsic function service INTEGER-OF-DATE DATE-OF-INTEGER DAY-OF-INTEGER INTEGER-OF-DATE CEECBLDY CEEDATE with picture string YYYYMMDD CEEDATE with picture string YYYYDDD CEEDAYS Results Compatible Incompatible Incompatible Incompatible
RELATED TASKS
62
Additionally, to ensure that the contents in Product-Name are in uppercase letters, you can use the following statement:
Move Function Upper-case (Product-Name) to Product-Name
Finance
Business investment decisions frequently require computing the present value of expected future cash inflows to evaluate the profitability of a planned investment. The present value of an amount that you expect to receive at a given time in the future is that amount, which, if invested today at a given interest rate, would accumulate to that future amount. For example, assume that a proposed investment of $1,000 produces a payment stream of $100, $200, and $300 over the next three years, one payment per year respectively. The following COBOL statements calculate the present value of those cash inflows at a 10% interest rate:
01 01 01 01 01 . . Series-Amt1 Series-Amt2 Series-Amt3 Discount-Rate Todays-Value . Pic Pic Pic Pic Pic 9(9)V99 9(9)V99 9(9)V99 S9(2)V9(6) 9(9)V99. Value Value Value Value 100. 200. 300. .10.
63
You can use the ANNUITY function in business problems that require you to determine the amount of an installment payment (annuity) necessary to repay the principal and interest of a loan. The series of payments is characterized by an equal amount each period, periods of equal length, and an equal interest rate each period. The following example shows how you can calculate the monthly payment required to repay a $15,000 loan in three years at a 12% annual interest rate (36 monthly payments, interest per month = .12/12):
01 01 01 01 . . Loan Pic 9(9)V99. Payment Pic 9(9)V99. Interest Pic 9(9)V99. Number-Periods Pic 99. . Compute Loan = 15000 Compute Interest = .12 Compute Number-Periods = 36 Compute Payment = Loan * Function Annuity((Interest / 12) Number-Periods)
Mathematics
The following COBOL statement demonstrates that you can nest intrinsic functions, use arithmetic expressions as arguments, and perform previously complex calculations simply:
Compute Z = Function Log(Function Sqrt (2 * X + 1)) + Function Rem(X 2)
Here in the addend the intrinsic function REM (instead of a DIVIDE statement with a REMAINDER clause) returns the remainder of dividing X by 2.
Statistics
Intrinsic functions make calculating statistical information easier. Assume you are analyzing various city taxes and want to calculate the mean, median, and range (the difference between the maximum and minimum taxes):
01 01 01 01 01 01 01 . . Tax-S Pic Tax-T Pic Tax-W Pic Tax-B Pic Ave-Tax Pic Median-Tax Pic Tax-Range Pic . Compute Ave-Tax = Compute Median-Tax = Compute Tax-Range = 99v999 value 99v999 value 99v999 value 99v999 value 99v999. 99v999. 99v999. .045. .02. .035. .03.
Function Mean (Tax-S Tax-T Tax-W Tax-B) Function Median (Tax-S Tax-T Tax-W Tax-B) Function Range (Tax-S Tax-T Tax-W Tax-B)
RELATED TASKS
64
Many statements in a program could involve arithmetic. For example, each of the following types of COBOL statements requires some arithmetic evaluation: v General arithmetic
compute report-matrix-col = (emp-count ** .5) + 1 add report-matrix-min to report-matrix-max giving report-matrix-tot
v Arithmetic comparisons
if report-matrix-col < function sqrt(emp-count) + 1 if whole-hours not = function integer-part((average-hours) + 1)
Floating-point evaluations
In general, if your arithmetic coding has either of the characteristics listed below, it is evaluated in floating-point arithmetic: v An operand or result field is floating point. An operand is floating point if you code it as a floating-point literal or if you code it as a data item that is defined as USAGE COMP-1, USAGE COMP-2, or external floating point (USAGE DISPLAY or USAGE NATIONAL with a floating-point PICTURE). An operand that is a nested arithmetic expression or a reference to a numeric intrinsic function results in floating-point arithmetic when any of the following conditions is true: An argument in an arithmetic expression results in floating point. The function is a floating-point function. The function is a mixed function with one or more floating-point arguments. v An exponent contains decimal places. An exponent contains decimal places if you use a literal that contains decimal places, give the item a PICTURE that contains decimal places, or use an arithmetic expression or function whose result has decimal places. An arithmetic expression or numeric function yields a result that has decimal places if any operand or argument (excluding divisors and exponents) has decimal places.
Fixed-point evaluations
In general, if an arithmetic operation contains neither of the characteristics listed above for floating point, the compiler causes it to be evaluated in fixed-point arithmetic. In other words, arithmetic evaluations are handled as fixed point only if all the operands are fixed point, the result field is defined to be fixed point, and none of the exponents represent values with decimal places. Nested arithmetic expressions and function references must also represent fixed-point values.
65
floating point. This is also true of abbreviated comparisons even though one comparand does not explicitly appear in the comparison. For example:
if (a + d) = (b + e) and c
This statement has two comparisons: (a + d) = (b + e), and (a + d) = c. Although (a + d) does not explicitly appear in the second comparison, it is a comparand in that comparison. Therefore, the attributes of c can influence the evaluation of (a + d). The compiler handles comparisons (and the evaluation of any arithmetic expressions nested in comparisons) in floating-point arithmetic if either comparand is a floating-point value or resolves to a floating-point value. The compiler handles comparisons (and the evaluation of any arithmetic expressions nested in comparisons) in fixed-point arithmetic if both comparands are fixed-point values or resolve to fixed-point values. Implicit comparisons (no relational operator used) are not handled as a unit, however; the two comparands are treated separately as to their evaluation in floating-point or fixed-point arithmetic. In the following example, five arithmetic expressions are evaluated independently of one anothers attributes, and then are compared to each other.
evaluate (a + d) when (b + e) thru c when (f / g) thru (h * i) . . . end-evaluate
. . 01 01 01 01 01 01
66
add report-matrix-min to report-matrix-max giving report-matrix-tot compute report-matrix-max = function max(report-matrix-max report-matrix-tot) if whole-hours not = function integer-part((average-hours) + 1)
In this example, if Invoice-Amount contained 1500.00, the display output would be:
Invoice amount is $US1,500.00
By using more than one CURRENCY SIGN clause in your program, you can allow for multiple currency signs to be displayed. You can use a hexadecimal literal to indicate the currency sign value. Using a hexadecimal literal could be useful if the data-entry method for the source program does not allow the entry of the intended characters easily. The following example shows the hexadecimal value X'9F' used as the currency sign:
Currency Sign X'9F' with Picture Symbol 'U'. . . . 01 Deposit-Amount Pic UUUUU9.99.
If there is no corresponding character for the euro sign on your keyboard, you need to specify it as a hexadecimal value in the CURRENCY SIGN clause. The hexadecimal value for the euro sign is either X'9F' or X'5A' depending on the code page in use, as shown in the following table.
Table 14. Hexadecimal values of the euro sign Code page CCSID 1140 1141 1142 1143 Applicable countries Modified from Euro sign X9F X9F X5A X5A
USA, Canada, Netherlands, Portugal, Australia, 037 New Zealand Austria, Germany Denmark, Norway Finland, Sweden 273 277 278
67
Table 14. Hexadecimal values of the euro sign (continued) Code page CCSID 1144 1145 1146 1147 1148 1149 Applicable countries Italy Spain, Latin America - Spanish UK France Belgium, Canada, Switzerland Iceland Modified from 280 284 285 297 500 871 Euro sign X9F X9F X9F X9F X9F X9F
RELATED REFERENCES
CURRENCY on page 313 CURRENCY SIGN clause (Enterprise COBOL Language Reference)
The exchange rate used in this example is for illustrative purposes only.
68
In the example above, SAMPLE-TABLE-ONE is the group item that contains the table. TABLE-COLUMN names the table element of a one-dimensional table that occurs three times. Rather than defining repetitious items as separate, consecutive entries in the DATA DIVISION, you use the OCCURS clause in the DATA DIVISION entry to define a table. This practice has these advantages: v The code clearly shows the unity of the items (the table elements). v You can use subscripts and indexes to refer to the table elements. v You can easily repeat data items. Tables are important for increasing the speed of a program, especially one that looks up records.
RELATED TASKS
Nesting tables on page 71 Defining a table (OCCURS) Referring to an item in a table on page 72 Putting values into a table on page 75 Creating variable-length tables (DEPENDING ON) on page 81 Searching a table on page 84 Processing table items using intrinsic functions on page 86 Handling tables efficiently on page 665
In the example above, table-name is the name of an alphanumeric group item. The table element definition (which includes the OCCURS clause) is subordinate to the group item that contains the table. The OCCURS clause cannot appear in a level-01 description.
69
If a table is to contain only Unicode (UTF-16) data, and you want the group item that contains the table to behave like an elementary category national item in most operations, code the GROUP-USAGE NATIONAL clause for the group item:
01 table-nameN Group-Usage National. 05 element-nameN OCCURS m TIMES. 10 elementN1 Pic nn. 10 elementN2 Pic S99 Sign Is Leading, Separate. . . .
Any elementary item that is subordinate to a national group must be explicitly or implicitly described as USAGE NATIONAL, and any subordinate numeric data item that is signed must be implicitly or explicitly described with the SIGN IS SEPARATE clause. To create tables of two to seven dimensions, use nested OCCURS clauses. To create a variable-length table, code the DEPENDING ON phrase of the OCCURS clause. To specify that table elements will be arranged in ascending or descending order based on the values in one or more key fields of the table, code the ASCENDING or DESCENDING KEY phrases of the OCCURS clause, or both. Specify the names of the keys in decreasing order of significance. Keys can be of class alphabetic, alphanumeric, DBCS, national, or numeric. (If it has USAGE NATIONAL, a key can be of category national, or can be a national-edited, numeric-edited, national decimal, or national floating-point item.) You must code the ASCENDING or DESCENDING KEY phrase of the OCCURS clause to do a binary search (SEARCH ALL) of a table. Example: binary search on page 86
RELATED CONCEPTS
Nesting tables on page 71 Referring to an item in a table on page 72 Putting values into a table on page 75 Creating variable-length tables (DEPENDING ON) on page 81 Using national groups on page 130 Doing a binary search (SEARCH ALL) on page 85 Defining numeric data on page 45
RELATED REFERENCES
OCCURS clause (Enterprise COBOL Language Reference) SIGN clause (Enterprise COBOL Language Reference) ASCENDING KEY and DESCENDING KEY phrases (Enterprise COBOL Language Reference)
70
Nesting tables
To create a two-dimensional table, define a one-dimensional table in each occurrence of another one-dimensional table.
For example, in SAMPLE-TABLE-TWO above, TABLE-ROW is an element of a one-dimensional table that occurs two times. TABLE-COLUMN is an element of a two-dimensional table that occurs three times in each occurrence of TABLE-ROW. To create a three-dimensional table, define a one-dimensional table in each occurrence of another one-dimensional table, which is itself contained in each occurrence of another one-dimensional table. For example:
In SAMPLE-TABLE-THREE, TABLE-DEPTH is an element of a one-dimensional table that occurs two times. TABLE-ROW is an element of a two-dimensional table that occurs two times within each occurrence of TABLE-DEPTH. TABLE-COLUMN is an element of a three-dimensional table that occurs three times within each occurrence of TABLE-ROW. In a two-dimensional table, the two subscripts correspond to the row and column numbers. In a three-dimensional table, the three subscripts correspond to the depth, row, and column numbers. Example: subscripting on page 72 Example: indexing on page 72
RELATED TASKS
Defining a table (OCCURS) on page 69 Referring to an item in a table on page 72 Putting values into a table on page 75 Creating variable-length tables (DEPENDING ON) on page 81 Searching a table on page 84 Processing table items using intrinsic functions on page 86 Handling tables efficiently on page 665
RELATED REFERENCES
71
Example: subscripting
The following example shows valid references to SAMPLE-TABLE-THREE that use literal subscripts. The spaces are required in the second example.
TABLE-COLUMN (2, 2, 1) TABLE-COLUMN (2 2 1)
In either table reference, the first value (2) refers to the second occurrence within TABLE-DEPTH, the second value (2) refers to the second occurrence within TABLE-ROW, and the third value (1) refers to the first occurrence within TABLE-COLUMN. The following reference to SAMPLE-TABLE-TWO uses variable subscripts. The reference is valid if SUB1 and SUB2 are data-names that contain positive integer values within the range of the table.
TABLE-COLUMN (SUB1 SUB2)
RELATED TASKS
Subscripting on page 73
Example: indexing
The following example shows how displacements to elements that are referenced with indexes are calculated. Consider the following three-dimensional table, SAMPLE-TABLE-FOUR:
01 SAMPLE-TABLE-FOUR 05 TABLE-DEPTH OCCURS 3 TIMES INDEXED BY INX-A. 10 TABLE-ROW OCCURS 4 TIMES INDEXED BY INX-B. 15 TABLE-COLUMN OCCURS 8 TIMES INDEXED BY INX-C
PIC X(8).
This reference causes the following computation of the displacement to the TABLE-COLUMN element:
(contents of INX-A) + (256 * 1) + (contents of INX-B) + (64 * 2) + (contents of INX-C) - (8 * 1)
This calculation is based on the following element lengths: v Each occurrence of TABLE-DEPTH is 256 bytes in length (4 * 8 * 8). v Each occurrence of TABLE-ROW is 64 bytes in length (8 * 8). v Each occurrence of TABLE-COLUMN is 8 bytes in length.
RELATED TASKS
Indexing on page 74
72
v Use the data-name of the table element, along with its occurrence number (called a subscript) in parentheses. This technique is called subscripting. v Use the data-name of the table element, along with a value (called an index) that is added to the address of the table to locate an item (as a displacement from the beginning of the table). This technique is called indexing, or subscripting using index-names. v Use both subscripts and indexes together.
RELATED TASKS
Subscripting
The lowest possible subscript value is 1, which references the first occurrence of a table element. In a one-dimensional table, the subscript corresponds to the row number. You can use a literal or a data-name as a subscript. If a data item that has a literal subscript is of fixed length, the compiler resolves the location of the data item. When you use a data-name as a variable subscript, you must describe the data-name as an elementary numeric integer. The most efficient format is COMPUTATIONAL (COMP) with a PICTURE size that is smaller than five digits. You cannot use a subscript with a data-name that is used as a subscript. The code generated for the application resolves the location of a variable subscript at run time. You can increment or decrement a literal or variable subscript by a specified integer amount. For example:
TABLE-COLUMN (SUB1 - 1, SUB2 + 3)
You can change part of a table element rather than the whole element. To do so, refer to the character position and length of the substring to be changed. For example:
01 ANY-TABLE. 05 TABLE-ELEMENT PIC X(10) OCCURS 3 TIMES VALUE "ABCDEFGHIJ". . . . MOVE "??" TO TABLE-ELEMENT (1) (3 : 2).
The MOVE statement in the example above moves the string ?? into table element number 1, beginning at character position 3, for a length of 2 characters.
Indexing on page 74
Chapter 4. Handling tables
73
Putting values into a table on page 75 Searching a table on page 84 Handling tables efficiently on page 665
Indexing
You create an index by using the INDEXED BY phrase of the OCCURS clause to identify an index-name. For example, INX-A in the following code is an index-name:
05 TABLE-ITEM PIC X(8) OCCURS 10 INDEXED BY INX-A.
The compiler calculates the value contained in the index as the occurrence number (subscript) minus 1, multiplied by the length of the table element. Therefore, for the fifth occurrence of TABLE-ITEM, the binary value contained in INX-A is (5 - 1) * 8, or 32. You can use an index-name to reference another table only if both table descriptions have the same number of table elements, and the table elements are of the same length. You can use the USAGE IS INDEX clause to create an index data item, and can use an index data item with any table. For example, INX-B in the following code is an index data item:
77 INX-B USAGE IS INDEX. . . . SET INX-A TO 10 SET INX-B TO INX-A. PERFORM VARYING INX-A FROM 1 BY 1 UNTIL INX-A > INX-B DISPLAY TABLE-ITEM (INX-A) . . . END-PERFORM.
The index-name INX-A is used to traverse table TABLE-ITEM above. The index data item INX-B is used to hold the index of the last element of the table. The advantage of this type of coding is that calculation of offsets of table elements is minimized, and no conversion is necessary for the UNTIL condition. You can use the SET statement to assign to an index data item the value that you stored in an index-name, as in the statement SET INX-B TO INX-A above. For example, when you load records into a variable-length table, you can store the index value of the last record into a data item defined as USAGE IS INDEX. Then you can test for the end of the table by comparing the current index value with the index value of the last record. This technique is useful when you look through or process a table. You can increment or decrement an index-name by an elementary integer data item or a nonzero integer literal, for example:
SET INX-A DOWN BY 3
The integer represents a number of occurrences. It is converted to an index value before being added to or subtracted from the index.
74
Initialize the index-name by using a SET, PERFORM VARYING, or SEARCH ALL statement. You can then use the index-name in SEARCH or relational condition statements. To change the value, use a PERFORM, SEARCH, or SET statement. Because you are comparing a physical displacement, you can directly use index data items only in SEARCH and SET statements or in comparisons with indexes or other index data items. You cannot use index data items as subscripts or indexes. Example: indexing on page 72
RELATED TASKS
Subscripting on page 73 Putting values into a table Searching a table on page 84 Processing table items using intrinsic functions on page 86 Handling tables efficiently on page 665
RELATED REFERENCES
INDEXED BY phrase (Enterprise COBOL Language Reference) INDEX phrase (Enterprise COBOL Language Reference) SET statement (Enterprise COBOL Language Reference)
Loading a table dynamically Loading a variable-length table on page 82 Initializing a table (INITIALIZE) on page 76 Assigning values when you define a table (VALUE) on page 77 Assigning values to a variable-length table on page 83
75
To move the character X into each of the elementary alphanumeric data items in TABLE-ONE, you can code the following statement:
INITIALIZE TABLE-ONE REPLACING ALPHANUMERIC DATA BY "X".
When you use the INITIALIZE statement to initialize a table, the table is processed as a group item (that is, with group semantics); elementary data items within the group are recognized and processed. For example, suppose that TABLE-ONE is an alphanumeric group that is defined like this:
01 TABLE-ONE. 02 Trans-out Occurs 20. 05 Trans-code Pic X Value "R". 05 Part-number Pic XX Value "13". 05 Trans-quan Pic 99 Value 10. 05 Price-fields. 10 Unit-price Pic 99V Value 50. 10 Discount Pic 99V Value 25. 10 Sales-Price Pic 999 Value 375. . . . Initialize TABLE-ONE Replacing Numeric Data By 3 Alphanumeric Data By "X"
The table below shows the content that each of the twenty 12-byte elements Trans-out(n) has before execution and after execution of the INITIALIZE statement shown above:
Trans-out(n) before R13105025375 1. The symbol b represents a blank space. Trans-out(n) after XXb0303030031
You can similarly use an INITIALIZE statement to load a table that is defined as a national group. For example, if TABLE-ONE shown above specified the GROUP-USAGE NATIONAL clause, and Trans-code and Part-number had N instead of X in their PICTURE clauses, the following statement would have the same effect as the INITIALIZE statement above, except that the data in TABLE-ONE would instead be encoded in UTF-16:
Initialize TABLE-ONE Replacing Numeric Data By 3 National Data By N"X"
The REPLACING NUMERIC phrase initializes floating-point data items also. You can use the REPLACING phrase of the INITIALIZE statement similarly to initialize all of the elementary ALPHABETIC, DBCS, ALPHANUMERIC-EDITED, NATIONAL-EDITED, and NUMERIC-EDITED data items in a table. The INITIALIZE statement cannot assign values to a variable-length table (that is, a table that was defined using the OCCURS DEPENDING ON clause).
76
Initializing a structure (INITIALIZE) on page 32 Assigning values when you define a table (VALUE) Assigning values to a variable-length table on page 83 Looping through a table on page 100 Using data items and group items on page 26 Using national groups on page 130
RELATED REFERENCES
each table item individually a table at the group level on page 78 all occurrences of a given table element on page 78 a structure (INITIALIZE) on page 32
77
In the example above, the VALUE clause at the 01 level initializes each of the table items to the same value. Each table item could instead be described with its own VALUE clause to initialize that item to a distinct value. To initialize larger tables, use MOVE, PERFORM, or INITIALIZE statements.
RELATED TASKS
REDEFINES clause (Enterprise COBOL Language Reference) OCCURS clause (Enterprise COBOL Language Reference)
In the following example, the national group data item Table-OneN uses a VALUE clause that initializes each of the three elements of the subordinate data item Table-TwoN (each of which is implicitly USAGE NATIONAL). Note that you can initialize a national group data item with a VALUE clause that uses an alphanumeric literal, as shown below, or a national literal.
01 Table-OneN Group-Usage National Value "AB12CD34EF56". 05 Table-TwoN Occurs 3 Times Indexed By MyI. 10 ElementOneN Pic nn. 10 ElementTwoN Pic 99.
After Table-OneN is initialized, ElementOneN(1) contains NX"00410042" (the UTF-16 representation of AB), the national decimal item ElementTwoN(1) contains NX"00310032" (the UTF-16 representation of 12), and so forth.
RELATED REFERENCES
OCCURS clause (Enterprise COBOL Language Reference) GROUP-USAGE clause (Enterprise COBOL Language Reference)
For example, the code above causes all the X elements (1 through 5) to be initialized to AA, all the Y elements (1 through 5) to be initialized to 19, and all the Z elements (1 through 5) to be initialized to BB. T-OBJ is then set to 3.
78
RELATED TASKS
79
80
In the example above, X is called the ODO subject, and Y is called the ODO object. Two factors affect the successful manipulation of variable-length records: v Correct calculation of record lengths The length of the variable portions of a group item is the product of the object of the DEPENDING ON phrase and the length of the subject of the OCCURS clause. v Conformance of the data in the object of the OCCURS DEPENDING ON clause to its PICTURE clause If the content of the ODO object does not match its PICTURE clause, the program could terminate abnormally. You must ensure that the ODO object correctly specifies the current number of occurrences of table elements. The following example shows a group item (REC-1) that contains both the subject and object of the OCCURS DEPENDING ON clause. The way the length of the group item is determined depends on whether it is sending or receiving data.
WORKING-STORAGE SECTION. 01 MAIN-AREA. 03 REC-1. 05 FIELD-1 05 FIELD-2 OCCURS 1 TO 5 TIMES DEPENDING ON FIELD-1 01 REC-2. 03 REC-2-DATA
If you want to move REC-1 (the sending item in this case) to REC-2, the length of REC-1 is determined immediately before the move, using the current value in FIELD-1. If the content of FIELD-1 conforms to its PICTURE clause (that is, if FIELD-1 contains a zoned decimal item), the move can proceed based on the actual length of REC-1. Otherwise, the result is unpredictable. You must ensure that the ODO object has the correct value before you initiate the move. When you do a move to REC-1 (the receiving item in this case), the length of REC-1 is determined using the maximum number of occurrences. In this example, five occurrences of FIELD-2, plus FIELD-1, yields a length of 26 bytes. In this case, you do not need to set the ODO object (FIELD-1) before referencing REC-1 as a receiving item. However, the sending fields ODO object (not shown) must be set to a valid numeric value between 1 and 5 for the ODO object of the receiving field to be validly set by the move. However, if you do a move to REC-1 (again the receiving item) where REC-1 is followed by a variably located group (a type of complex ODO), the actual length of REC-1 is calculated immediately before the move, using the current value of the ODO object (FIELD-1). In the following example, REC-1 and REC-2 are in the same record, but REC-2 is not subordinate to REC-1 and is therefore variably located:
01 MAIN-AREA 03 REC-1. 05 FIELD-1 05 FIELD-3 05 FIELD-2 OCCURS 1 TO 5 TIMES DEPENDING ON FIELD-1
81
PIC X(05).
The compiler issues a message that lets you know that the actual length was used. This case requires that you set the value of the ODO object before using the group item as a receiving field. The following example shows how to define a variable-length table when the ODO object (LOCATION-TABLE-LENGTH below) is outside the group:
DATA DIVISION. FILE SECTION. FD LOCATION-FILE RECORDING MODE F BLOCK 0 RECORDS RECORD 80 CHARACTERS LABEL RECORD STANDARD. 01 LOCATION-RECORD. 05 LOC-CODE PIC XX. 05 LOC-DESCRIPTION PIC X(20). 05 FILLER PIC X(58). WORKING-STORAGE SECTION. 01 FLAGS. 05 LOCATION-EOF-FLAG PIC X(5) VALUE SPACE. 88 LOCATION-EOF VALUE "FALSE". 01 MISC-VALUES. 05 LOCATION-TABLE-LENGTH PIC 9(3) VALUE ZERO. 05 LOCATION-TABLE-MAX PIC 9(3) VALUE 100. ***************************************************************** *** L O C A T I O N T A B L E *** *** FILE CONTAINS LOCATION CODES. *** ***************************************************************** 01 LOCATION-TABLE. 05 LOCATION-CODE OCCURS 1 TO 100 TIMES DEPENDING ON LOCATION-TABLE-LENGTH PIC X(80).
RELATED CONCEPTS
Assigning values to a variable-length table on page 83 Loading a variable-length table Preventing overlay when adding elements to a variable table on page 699 Finding the length of data items on page 118 Enterprise COBOL Compiler and Runtime Migration Guide
RELATED REFERENCES
82
LOCATION-RECORD. 05 LOC-CODE PIC XX. 05 LOC-DESCRIPTION PIC X(20). 05 FILLER PIC X(58). . . . WORKING-STORAGE SECTION. 01 FLAGS. 05 LOCATION-EOF-FLAG PIC X(5) VALUE SPACE. 88 LOCATION-EOF VALUE "YES". 01 MISC-VALUES. 05 LOCATION-TABLE-LENGTH PIC 9(3) VALUE ZERO. 05 LOCATION-TABLE-MAX PIC 9(3) VALUE 100. ***************************************************************** *** L O C A T I O N T A B L E *** *** FILE CONTAINS LOCATION CODES. *** ***************************************************************** 01 LOCATION-TABLE. 05 LOCATION-CODE OCCURS 1 TO 100 TIMES DEPENDING ON LOCATION-TABLE-LENGTH PIC X(80). . . . PROCEDURE DIVISION. . . . Perform Test After Varying Location-Table-Length From 1 By 1 Until Location-EOF Or Location-Table-Length = Location-Table-Max Move Location-Record To Location-Code (Location-Table-Length) Read Location-File At End Set Location-EOF To True End-Read End-Perform
01
For example, in the code above, the ODO subject Y(1) is initialized to A, Y(2) to B, . . ., Y(5) to E, and finally the ODO object X is initialized to 3. Any subsequent reference to TABLE-THREE (such as in a DISPLAY statement) refers to X and the first three elements, Y(1) through Y(3), of the table.
RELATED TASKS
83
Searching a table
COBOL provides two search techniques for tables: serial and binary. To do serial searches, use SEARCH and indexing. For variable-length tables, you can use PERFORM with subscripting or indexing. To do binary searches, use SEARCH ALL and indexing. A binary search can be considerably more efficient than a serial search. For a serial search, the number of comparisons is of the order of n, the number of entries in the table. For a binary search, the number of comparisons is of the order of only the logarithm (base 2) of n. A binary search, however, requires that the table items already be sorted.
RELATED TASKS
Doing a serial search (SEARCH) Doing a binary search (SEARCH ALL) on page 85
84
Each dimension of the table has its own index (set to 1, 4, and 1, respectively). The innermost table (TABLE-ENTRY3) has an ascending key.
01 TABLE-ONE. 05 TABLE-ENTRY1 OCCURS 10 TIMES INDEXED BY TE1-INDEX. 10 TABLE-ENTRY2 OCCURS 10 TIMES INDEXED BY TE2-INDEX. 15 TABLE-ENTRY3 OCCURS 5 TIMES ASCENDING KEY IS KEY1 INDEXED BY TE3-INDEX. 20 KEY1 PIC X(5). 20 KEY2 PIC X(10). . . . PROCEDURE DIVISION. . . . SET TE1-INDEX TO 1 SET TE2-INDEX TO 4 SET TE3-INDEX TO 1 MOVE "A1234" TO KEY1 (TE1-INDEX, TE2-INDEX, TE3-INDEX + 2) MOVE "AAAAAAAA00" TO KEY2 (TE1-INDEX, TE2-INDEX, TE3-INDEX + 2) . . . SEARCH TABLE-ENTRY3 AT END MOVE 4 TO RETURN-CODE WHEN TABLE-ENTRY3(TE1-INDEX, TE2-INDEX, TE3-INDEX) = "A1234AAAAAAAA00" MOVE 0 TO RETURN-CODE END-SEARCH
85
SEARCH statement (Enterprise COBOL Language Reference) General relation conditions (Enterprise COBOL Language Reference)
If an entry is found in which each of the three keys is equal to the value to which it is compared (VALUE-1, VALUE-2, and VALUE-3, respectively), PART-1 of that entry is moved to OUTPUT-AREA. If no matching key is found in the entries in TABLE-A, the NOENTRY routine is performed.
You might often need to iteratively process the data in tables. For intrinsic functions that accept multiple arguments, you can use the subscript ALL to
86
reference all the items in the table or in a single dimension of the table. The iteration is handled automatically, which can make your code shorter and simpler. You can mix scalars and array arguments for functions that accept multiple arguments:
Compute Table-Median = Function Median(Arg1 Table-One(ALL))
Using intrinsic functions (built-in functions) on page 40 Converting data items (intrinsic functions) on page 112 Evaluating data items (intrinsic functions) on page 115
RELATED REFERENCES
The following example computes various salary values for all the employees whose salaries are encoded in Employee-Table:
01 Employee-Table. 05 Emp-Count 05 Emp-Record 10 Emp-Name 10 Emp-Idme 10 Emp-Salary Pic s9(4) usage binary. Occurs 1 to 500 times depending on Emp-Count. Pic x(20). Pic 9(9). Pic 9(7)v99. = = = = = Function Function Function Function Function Max(Emp-Salary(ALL)) Ord-Max(Emp-Salary(ALL)) Mean(Emp-Salary(ALL)) Range(Emp-Salary(ALL)) Sum(Emp-Salary(ALL))
. . . Procedure Division. Compute Max-Salary Compute I Compute Avg-Salary Compute Salary-Range Compute Total-Payroll
87
88
IF statement (Enterprise COBOL Language Reference) EVALUATE statement (Enterprise COBOL Language Reference)
When one of two processing choices is no action, code the IF statement with or without ELSE. Because the ELSE clause is optional, you can code the IF statement as follows:
IF condition-q statement-1 END-IF
Such coding is suitable for simple cases. For complex logic, you probably need to use the ELSE clause. For example, suppose you have nested IF statements in which there is an action for only one of the processing choices. You could use the ELSE clause and code the null branch of the IF statement with the CONTINUE statement:
89
The EVALUATE statement is an expanded form of the IF statement that allows you to avoid nesting IF statements, a common source of logic errors and debugging problems.
RELATED TASKS
Using nested IF statements Using the EVALUATE statement on page 91 Coding conditional expressions on page 94
In the pseudocode above, an IF statement and a sequential structure are nested in one branch of the outer IF. In this structure, the END-IF that closes the nested IF is very important. Use END-IF instead of a period, because a period would end the outer IF structure also. The following figure shows the logic structure of the pseudocode above.
90
RELATED TASKS
91
object must correspond to a conditional expression or the word TRUE or FALSE in the set of selection subjects. (You can use the word ANY as a selection object to correspond to any type of selection subject.) The execution of the EVALUATE statement ends when one of the following conditions occurs: v The statements associated with the selected WHEN phrase are performed. v The statements associated with the WHEN OTHER phrase are performed. v No WHEN conditions are satisfied. WHEN phrases are tested in the order that they appear in the source program. Therefore, you should order these phrases for the best performance. First code the WHEN phrase that contains selection objects that are most likely to be satisfied, then the next most likely, and so on. An exception is the WHEN OTHER phrase, which must come last.
RELATED TASKS
EVALUATE statement (Enterprise COBOL Language Reference) General relation conditions (Enterprise COBOL Language Reference) Example: EVALUATE using THRU phrase: This example shows how you can code several conditions in a range of values to lead to the same processing action by coding the THRU phrase. Operands in a THRU phrase must be of the same class. In this example, CARPOOL-SIZE is the selection subject; 1, 2, and 3 THRU 6 are the selection objects:
EVALUATE CARPOOL-SIZE WHEN 1 MOVE "SINGLE" TO PRINT-CARPOOL-STATUS WHEN 2 MOVE "COUPLE" TO PRINT-CARPOOL-STATUS WHEN 3 THRU 6 MOVE "SMALL GROUP" TO PRINT-CARPOOL STATUS WHEN OTHER MOVE "BIG GROUP" TO PRINT-CARPOOL STATUS END-EVALUATE
92
The following example shows that you can code multiple WHEN phrases if several conditions should lead to the same action. Doing so gives you more flexibility than using only the THRU phrase, because the conditions do not have to evaluate to values in a range nor have the same class.
EVALUATE MARITAL-CODE WHEN "M" ADD 2 TO PEOPLE-COUNT WHEN "S" WHEN "D" WHEN "W" ADD 1 TO PEOPLE-COUNT END-EVALUATE
Example: EVALUATE testing several conditions: This example shows the use of the ALSO phrase to separate two selection subjects (True ALSO True) and to separate the two corresponding selection objects within each set of selection objects (for example, When A + B < 10 Also C = 10). Both selection objects in a WHEN phrase must satisfy the TRUE, TRUE condition before the associated action is performed. If both objects do not evaluate to TRUE, the next WHEN phrase is processed.
Identification Division. Program-ID. MiniEval. Environment Division. Configuration Section. Source-Computer. IBM-390. Data Division. Working-Storage Section. 01 Age Pic 999. 01 Sex Pic X. 01 Description Pic X(15). 01 A Pic 999. 01 B Pic 9999. 01 C Pic 9999. 01 D Pic 9999. 01 E Pic 99999. 01 F Pic 999999. Procedure Division. PN01. Evaluate True Also True When Age < 13 Also Sex = "M" Move "Young Boy" To Description When Age < 13 Also Sex = "F" Move "Young Girl" To Description When Age > 12 And Age < 20 Also Sex = "M" Move "Teenage Boy" To Description When Age > 12 And Age < 20 Also Sex = "F" Move "Teenage Girl" To Description When Age > 19 Also Sex = "M" Move "Adult Man" To Description When Age > 19 Also Sex = "F"
Chapter 5. Selecting and repeating program actions
93
Move "Adult Woman" To Description When Other Move "Invalid Data" To Description End-Evaluate Evaluate True Also True When A + B < 10 Also C = 10 Move "Case 1" To Description When A + B > 50 Also C = ( D + E ) / F Move "Case 2" To Description When Other Move "Case Other" To Description End-Evaluate Stop Run.
Defining switches and flags on page 95 Resetting switches and flags on page 96 Checking for incompatible data (numeric class test) on page 56 Comparing national (UTF-16) data on page 139 Testing for valid DBCS characters on page 143
RELATED REFERENCES
94
Class condition (Enterprise COBOL Language Reference) Rules for condition-name entries (Enterprise COBOL Language Reference) Sign condition (Enterprise COBOL Language Reference) Combined conditions (Enterprise COBOL Language Reference)
Example: switches
The following examples show how you can use level-88 items to test for various binary-valued (on-off) conditions in your program. For example, to test for the end-of-file condition for an input file named Transaction-File, you can use the following data definitions:
Working-Storage Section. 01 Switches. 05 Transaction-EOF-Switch 88 Transaction-EOF
The level-88 description says that a condition named Transaction-EOF is turned on when Transaction-EOF-Switch has value y. Referencing Transaction-EOF in the PROCEDURE DIVISION expresses the same condition as testing Transaction-EOF-
95
Switch = "y". For example, the following statement causes a report to be printed only if Transaction-EOF-Switch has been set to y:
If Transaction-EOF Then Perform Print-Report-Summary-Lines
Example: flags
The following examples show how you can use several level-88 items together with an EVALUATE statement to determine which of several conditions in a program is true. Consider for example a program that updates a master file. The updates are read from a transaction file. The records in the file contain a field that indicates which of the three functions is to be performed: add, change, or delete. In the record description of the input file, code a field for the function code using level-88 items:
01 Transaction-Input Record 05 Transaction-Type 88 Add-Transaction 88 Change-Transaction 88 Delete-Transaction Pic X. Value "A". Value "C". Value "D".
The code in the PROCEDURE DIVISION for testing these condition-names to determine which function is to be performed might look like this:
Evaluate True When Add-Transaction Perform Add-Master-Record-Paragraph When Change-Transaction Perform Update-Existing-Record-Paragraph When Delete-Transaction Perform Delete-Master-Record-Paragraph End-Evaluate
Using the SET statement and meaningful condition-names makes it easier for readers to follow your code. Example: set switch on Example: set switch off on page 97
96
01
Switches 05 Transaction-EOF-Switch Pic X Value space. 88 Transaction-EOF Value "y". . . . Procedure Division. 000-Do-Main-Logic. Perform 100-Initialize-Paragraph Read Update-Transaction-File At End Set Transaction-EOF to True End-Read
The following example shows how to assign a value to a field in an output record based on the transaction code of an input record:
01 Input-Record. 05 Transaction-Type Pic X(9). 01 Data-Record-Out. 05 Data-Record-Type Pic X. 88 Record-Is-Active Value "A". 88 Record-Is-Suspended Value "S". 88 Record-Is-Deleted Value "D". 05 Key-Field Pic X(5). . . . Procedure Division. Evaluate Transaction-Type of Input-Record When "ACTIVE" Set Record-Is-Active to TRUE When "SUSPENDED" Set Record-Is-Suspended to TRUE When "DELETED" Set Record-Is-Deleted to TRUE End-Evaluate
97
RELATED TASKS
Choosing inline or out-of-line PERFORM Coding a loop on page 99 Looping through a table on page 100 Executing multiple paragraphs or sections on page 100
RELATED REFERENCES
98
Else Perform 400-Print-Transaction-Errors * End-If is a required scope terminator End-If Perform 410-Re-Initialize-Fields * End-Read is a required scope terminator End-Read End-Perform
Coding a loop
Use the PERFORM . . . TIMES statement to execute a paragraph a specified number of times.
PERFORM 010-PROCESS-ONE-MONTH 12 TIMES INSPECT . . .
In the example above, when control reaches the PERFORM statement, the code for the paragraph 010-PROCESS-ONE-MONTH is executed 12 times before control is transferred to the INSPECT statement. Use the PERFORM . . . UNTIL statement to execute a paragraph until a condition you choose is satisfied. You can use either of the following forms:
PERFORM . . . WITH TEST AFTER . . . . UNTIL . . . PERFORM . . . [WITH TEST BEFORE] . . . UNTIL . . .
Use the PERFORM . . . WITH TEST AFTER . . . UNTIL statement if you want to execute the paragraph at least once, and test before any subsequent execution. This statement is equivalent to a do-until structure:
In the following example, the implicit WITH TEST BEFORE phrase provides a do-while structure:
PERFORM 010-PROCESS-ONE-MONTH UNTIL MONTH GREATER THAN 12 INSPECT . . .
When control reaches the PERFORM statement, the condition MONTH GREATER THAN 12 is tested. If the condition is satisfied, control is transferred to the INSPECT statement. If the condition is not satisfied, 010-PROCESS-ONE-MONTH is executed, and the condition is tested again. This cycle continues until the condition tests as true. (To make your program easier to read, you might want to code the WITH TEST BEFORE clause.)
99
The following section of code shows an example of looping through a table to check for invalid data:
PERFORM TEST AFTER VARYING WS-DATA-IX FROM 1 BY 1 UNTIL WS-DATA-IX = 12 IF WS-DATA (WS-DATA-IX) EQUALS SPACES SET SERIOUS-ERROR TO TRUE DISPLAY ELEMENT-NUM-MSG5 END-IF END-PERFORM INSPECT . . .
When control reaches the PERFORM statement above, WS-DATA-IX is set equal to 1 and the PERFORM statement is executed. Then the condition WS-DATA-IX = 12 is tested. If the condition is true, control drops through to the INSPECT statement. If the condition is false, WS-DATA-IX is increased by 1, the PERFORM statement is executed, and the condition is tested again. This cycle of execution and testing continues until WS-DATA-IX is equal to 12. The loop above controls input-checking for the 12 fields of item WS-DATA. Empty fields are not allowed in the application, so the section of code loops and issues error messages as appropriate.
100
Joining data items (STRING) Splitting data items (UNSTRING) on page 103 Manipulating null-terminated strings on page 106 Referring to substrings of data items on page 107 Tallying and replacing data items (INSPECT) on page 111 Converting data items (intrinsic functions) on page 112 Evaluating data items (intrinsic functions) on page 115 Chapter 7, Processing data in an international environment, on page 121
101
Only that portion of the receiving field into which data is written by the STRING statement is changed. Example: STRING statement
RELATED TASKS
PIC X(15). PIC X(35). PIC PIC PIC PIC PIC PIC X(6). $$,$$$.99. $$,$$$.99. X(8). $$,$$$.99. X(8).
The record RCD-01 contains the following information (the symbol b indicates a blank space):
J.B.bSMITHbbbbb 444bSPRINGbST.,bCHICAGO,bILL.bbbbbb A14275 $4,736.85 $2,400.00 09/22/76 $2,336.85 10/22/76
In the PROCEDURE DIVISION, these settings occur before the STRING statement: v RPT-LINE is set to SPACES. v LINE-POS, the data item to be used as the POINTER field, is set to 4. Here is the STRING statement:
STRING LINE-NO SPACE CUST-INFO INV-NO SPACE DATE-DUE SPACE DELIMITED BY SIZE BAL-DUE DELIMITED BY DEC-POINT INTO RPT-LINE WITH POINTER LINE-POS.
102
Because the POINTER field LINE-POS has value 4 before the STRING statement is performed, data is moved into the receiving field RPT-LINE beginning at character position 4. Characters in positions 1 through 3 are unchanged. The sending items that specify DELIMITED BY SIZE are moved in their entirety to the receiving field. Because BAL-DUE is delimited by DEC-POINT, the moving of BAL-DUE to the receiving field stops when a decimal point (the value of DEC-POINT) is encountered.
STRING results
When the STRING statement is performed, items are moved into RPT-LINE as shown in the table below.
Item LINE-NO Space CUST-INFO INV-NO Space DATE-DUE Space Portion of BAL-DUE that precedes the decimal point Positions 4-8 9 10 - 59 60 - 65 66 67 - 74 75 76 - 81
After the STRING statement is performed, the value of LINE-POS is 82, and RPT-LINE has the values shown below.
103
v Action to be taken if all of the receiving fields are filled before the end of the sending data item is reached (ON OVERFLOW phrase) The sending data item and the delimiters in the DELIMITED BY phrase must be of category alphabetic, alphanumeric, alphanumeric-edited, DBCS, national, or national-edited. Receiving data items can be of category alphabetic, alphanumeric, numeric, DBCS, or national. If numeric, a receiving data item must be zoned decimal or national decimal. If a receiving data item has: v USAGE DISPLAY, the sending item and each delimiter item in the statement must have USAGE DISPLAY, and each literal in the statement must be alphanumeric v USAGE NATIONAL, the sending item and each delimiter item in the statement must have USAGE NATIONAL, and each literal in the statement must be national v USAGE DISPLAY-1, the sending item and each delimiter item in the statement must have USAGE DISPLAY-1, and each literal in the statement must be DBCS Example: UNSTRING statement
RELATED CONCEPTS
UNSTRING statement (Enterprise COBOL Language Reference) Classes and categories of data (Enterprise COBOL Language Reference)
* *
* *
104
01
* *
UNSTRING statement control fields: 77 DBY-1 PIC X. 77 CTR-1 PIC S9(3). 77 CTR-2 PIC S9(3). 77 CTR-3 PIC S9(3). 77 CTR-4 PIC S9(3). 77 DLTR-1 PIC X. 77 DLTR-2 PIC X. 77 CHAR-CT PIC S9(3). 77 FLDS-FILLED PIC S9(3).
In the PROCEDURE DIVISION, these settings occur before the UNSTRING statement: v A period (.) is placed in DBY-1 for use as a delimiter. v CHAR-CT (the POINTER field) is set to 3. v The value zero (0) is placed in FLDS-FILLED (the TALLYING field). v Data is read into record INV-RCD, whose format is as shown below.
Because the POINTER field CHAR-CT has value 3 before the UNSTRING statement is performed, the two character positions of the CONTROL-CHARS field in INV-RCD are ignored.
UNSTRING results
When the UNSTRING statement is performed, the following steps take place: 1. Positions 3 through 18 (FOUR-PENNY-NAILS) of INV-RCD are placed in ITEM-NAME, left justified in the area, and the four unused character positions are padded with spaces. The value 16 is placed in CTR-1. 2. Because ALL SPACES is coded as a delimiter, the five contiguous space characters in positions 19 through 23 are considered to be one occurrence of the delimiter. 3. Positions 24 through 29 (707890) are placed in INV-NO. The delimiter character slash (/) is placed in DLTR-1, and the value 6 is placed in CTR-2.
Chapter 6. Handling strings
105
4. Positions 31 through 33 (BBA) are placed in INV-CLASS. The delimiter is SPACE, but because no field has been defined as a receiving area for delimiters, the space in position 34 is bypassed. 5. Positions 35 through 40 (475120) are placed in M-UNITS. The value 6 is placed in CTR-3. The delimiter is SPACE, but because no field has been defined as a receiving area for delimiters, the space in position 41 is bypassed. 6. Positions 42 through 46 (00122) are placed in FIELD-A and right justified in the area. The high-order digit position is filled with a zero (0). The delimiter is SPACE, but because no field was defined as a receiving area for delimiters, the space in position 47 is bypassed. 7. Positions 48 through 53 (000379) are placed in DISPLAY-DOLS. The period (.) delimiter in DBY-1 is placed in DLTR-2, and the value 6 is placed in CTR-4. 8. Because all receiving fields have been acted on and two characters in INV-RCD have not been examined, the ON OVERFLOW statement is executed. Execution of the UNSTRING statement is completed. After the UNSTRING statement is performed, the fields contain the values shown below.
Field DISPLAY-REC WORK-REC CHAR-CT (the POINTER field) FLDS-FILLED (the TALLYING field) Value 707890 FOUR-PENNY-NAILS 475120000122BBA 55 6 000379
v Use an UNSTRING statement to move characters in a null-terminated string to a target field, and get the character count:
WORKING-STORAGE SECTION. 01 source-field PIC X(1001). 01 char-count COMP-5 PIC 9(4). 01 target-area. 02 individual-char OCCURS 1 TO 1000 TIMES DEPENDING ON char-count PIC X. . . . PROCEDURE DIVISION. UNSTRING source-field DELIMITED BY X"00" INTO target-area COUNT IN char-count ON OVERFLOW DISPLAY "source not null terminated or target too short" END-UNSTRING
106
v Use a SEARCH statement to locate trailing null or space characters. Define the string being examined as a table of single characters. v Check each character in a field in a loop (PERFORM). You can examine each character in a field by using a reference modifier such as source-field (I:1). Example: null-terminated strings
RELATED TASKS
You code a reference modifier in parentheses immediately after the data item. As the example shows, a reference modifier can contain two values that are separated by a colon, in this order: 1. Ordinal position (from the left) of the character that you want the substring to start with
Chapter 6. Handling strings
107
2. (Optional) Length of the desired substring in character positions The reference-modifier position and length for an item that has USAGE DISPLAY are expressed in terms of single-byte characters. The reference-modifier position and length for items that have USAGE DISPLAY-1 or NATIONAL are expressed in terms of DBCS character positions and national character positions, respectively. If you omit the length in a reference modifier (coding only the ordinal position of the first character, followed by a colon), the substring extends to the end of the item. Omit the length where possible as a simpler and less error-prone coding technique. You can refer to substrings of USAGE DISPLAY data items, including alphanumeric groups, alphanumeric-edited data items, numeric-edited data items, display floating-point data items, and zoned decimal data items, by using reference modifiers. When you reference-modify any of these data items, the result is of category alphanumeric. When you reference-modify an alphabetic data item, the result is of category alphabetic. You can refer to substrings of USAGE NATIONAL data items, including national groups, national-edited data items, numeric-edited data items, national floating-point data items, and national decimal data items, by using reference modifiers. When you reference-modify any of these data items, the result is of category national. For example, suppose that you define a national decimal data item as follows:
01 NATL-DEC-ITEM Usage National Pic 999 Value 123.
You can use NATL-DEC-ITEM in an arithmetic expression because NATL-DEC-ITEM is of category numeric. But you cannot use NATL-DEC-ITEM(2:1) (the national character 2, which in hexadecimal notation is NX"0032") in an arithmetic expression, because it is of category national. You can refer to substrings of table entries, including variable-length entries, by using reference modifiers. To refer to a substring of a table entry, code the subscript expression before the reference modifier. For example, assume that PRODUCT-TABLE is a properly coded table of character strings. To move D to the fourth character in the second string in the table, you can code this statement:
MOVE 'D' to PRODUCT-TABLE (2), (4:1)
You can code either or both of the two values in a reference modifier as a variable or as an arithmetic expression. Example: arithmetic expressions as reference modifiers on page 110 Because numeric function identifiers can be used anywhere that arithmetic expressions can be used, you can code a numeric function identifier in a reference modifier as the leftmost character position or as the length, or both. Example: intrinsic functions as reference modifiers on page 110 Each number in the reference modifier must have a value of at least 1. The sum of the two numbers must not exceed the total length of the data item by more than 1 character position so that you do not reference beyond the end of the substring.
108
If the leftmost character position or the length value is a fixed-point noninteger, truncation occurs to create an integer. If either is a floating-point noninteger, rounding occurs to create an integer. The following options detect out-of-range reference modifiers, and flag violations with a runtime message: v SSRANGE compiler option v CHECK runtime option
RELATED CONCEPTS
Reference modifiers Unicode and the encoding of language characters on page 125
RELATED TASKS
SSRANGE on page 347 Reference modification (Enterprise COBOL Language Reference) Function definitions (Enterprise COBOL Language Reference)
Reference modifiers
Reference modifiers let you easily refer to a substring of a data item. For example, assume that you want to retrieve the current time from the system and display its value in an expanded format. You can retrieve the current time with the ACCEPT statement, which returns the hours, minutes, seconds, and hundredths of seconds in this format:
HHMMSSss
However, you might prefer to view the current time in this format:
HH:MM:SS
Without reference modifiers, you would have to define data items for both formats. You would also have to write code to convert from one format to the other. With reference modifiers, you do not need to provide names for the subfields that describe the TIME elements. The only data definition you need is for the time as returned by the system. For example:
01 REFMOD-TIME-ITEM PIC X(8).
* * * * * *
109
Example: arithmetic expressions as reference modifiers Example: intrinsic functions as reference modifiers
RELATED TASKS
Assigning input from a screen or file (ACCEPT) on page 37 Referring to substrings of data items on page 107 Using national data (Unicode) in COBOL on page 126
RELATED REFERENCES
The program counts the number of leading spaces and, using arithmetic expressions in a reference modifier, moves the right-justified characters into another field, justified to the left:
MOVE SPACES TO LEFTY MOVE ZERO TO I INSPECT RIGHTY TALLYING I FOR LEADING SPACE. IF I IS LESS THAN LENGTH OF RIGHTY THEN MOVE RIGHTY ( I + 1 : LENGTH OF RIGHTY - I ) TO LEFTY END-IF
The MOVE statement transfers characters from RIGHTY, beginning at the position computed as I + 1 for a length that is computed as LENGTH OF RIGHTY - I, into the field LEFTY.
If you want to use a noninteger function in a position that requires an integer function, you can use the INTEGER or INTEGER-PART function to convert the result to an integer. For example:
Move Customer-Record(Function Integer(Function Sqrt(I)): ) to WS-name
RELATED REFERENCES
INTEGER (Enterprise COBOL Language Reference) INTEGER-PART (Enterprise COBOL Language Reference)
110
111
COUNTR after 2 4 0
In the following example, the INSPECT statement examines and replaces characters in data item DATA-3. Each character that precedes the first instance of a quotation mark (") is replaced by the character 0.
77 COUNTR PIC 9 VALUE ZERO. 01 DATA-3 PIC X(8). . . . INSPECT DATA-3 REPLACING CHARACTERS BY ZEROS BEFORE INITIAL QUOTE DATA-3 before 456"ABEL ANDES"12 "TWAS BR COUNTR after 0 0 0 DATA-3 after 000"ABEL 00000"12 "TWAS BR
The following example shows the use of INSPECT CONVERTING with AFTER and BEFORE phrases to examine and replace characters in data item DATA-4. All characters that follow the first instance of the character / but that precede the first instance of the character ? (if any) are translated from lowercase to uppercase.
01 DATA-4 PIC X(11). . . . INSPECT DATA-4 CONVERTING "abcdefghijklmnopqrstuvwxyz" TO "ABCDEFGHIJKLMNOPQRSTUVWXYZ" AFTER INITIAL "/" BEFORE INITIAL"?" DATA-4 before a/five/?six r/Rexx/RRRr zfour?inspe DATA-4 after a/FIVE/?six r/REXX/RRRR zfour?inspe
112
RELATED TASKS
Converting to uppercase or lowercase (UPPER-CASE, LOWER-CASE) Transforming to reverse order (REVERSE) Converting to numbers (NUMVAL, NUMVAL-C) Converting from one code page to another on page 115
The code above displays the following messages on the system logical output device:
Hello HELLO hello HELLO World! WORLD! world! WORLD!
The DISPLAY statements do not change the actual contents of Item-1, but affect only how the letters are displayed. However, the MOVE statement causes uppercase letters to replace the contents of Item-2.
RELATED TASKS
Assigning input from a screen or file (ACCEPT) on page 37 Displaying values on a screen or in a file (DISPLAY) on page 38
For example, the statement above reverses the order of the characters in Orig-cust-name. If the starting value is JOHNSONbbb, the value after the statement is performed is bbbNOSNHOJ, where b represents a blank space.
RELATED CONCEPTS
113
01 01 01 . .
R S Total . Compute
Pic x(20) Value "- 1234.5678". Pic x(20) Value " $12,345.67CR". Usage is Comp-1. Total = Function Numval(R) + Function Numval-C(S)
Use NUMVAL-C when the argument includes a currency symbol or comma or both, as shown in the example above. You can also place an algebraic sign before or after the character string, and the sign will be processed. The arguments must not exceed 18 digits when you compile with the default option ARITH(COMPAT) (compatibility mode) nor 31 digits when you compile with ARITH(EXTEND) (extended mode), not including the editing symbols. NUMVAL and NUMVAL-C return long (64-bit) floating-point values in compatibility mode, and return extended-precision (128-bit) floating-point values in extended mode. A reference to either of these functions represents a reference to a numeric data item. At most 15 decimal digits can be converted accurately to long-precision floating point (as described in the related reference below about conversions and precision). If the argument to NUMVAL or NUMVAL-C has more than 15 digits, it is recommended that you specify the ARITH(EXTEND) compiler option so that an extended-precision function result that can accurately represent the value of the argument is returned. When you use NUMVAL or NUMVAL-C, you do not need to statically declare numeric data in a fixed format nor input data in a precise manner. For example, suppose you define numbers to be entered as follows:
01 X Pic S999V99 leading sign is separate. . . . Accept X from Console
The user of the application must enter the numbers exactly as defined by the PICTURE clause. For example:
+001.23 -300.00
Formats for numeric data on page 49 Data format conversions on page 54 Unicode and the encoding of language characters on page 125
RELATED TASKS
114
RELATED REFERENCES
Evaluating single characters for collating sequence Finding the largest or smallest data item on page 116 Finding the length of data items on page 118 Finding the date of compilation on page 119
115
You can use a one-character substring of a data item as the argument to ORD:
IF Function Ord(Customer-record(1:1)) IS > 194 THEN . . .
If you know the ordinal position in the collating sequence of a character, and want to find the character that it corresponds to, use the CHAR function with the integer ordinal position as the argument. CHAR returns the desired character. For example:
INITIALIZE Customer-Name REPLACING ALPHABETIC BY Function Char(65)
RELATED REFERENCES
CHAR (Enterprise COBOL Language Reference) ORD (Enterprise COBOL Language Reference)
The following statement assigns VALLEJObbb to the first 10 character positions of Customer-record, where b represents a blank space:
Move Function Max(Arg1 Arg2 Arg3) To Customer-record(1:10)
If you used MIN instead, then THOMASbbbb would be assigned. The functions ORD-MAX and ORD-MIN return an integer that represents the ordinal position (counting from the left) of the argument that has the largest or smallest value in the list of arguments that you supply. If you used the ORD-MAX function in the example above, the compiler would issue an error message because the reference to a numeric function is not in a valid place. The following statement is a valid use of ORD-MAX:
Compute x = Function Ord-max(Arg1 Arg2 Arg3)
The statement above assigns the integer 3 to x if the same arguments are used as in the previous example. If you used ORD-MIN instead, the integer 2 would be returned. The examples above might be more realistic if Arg1, Arg2, and Arg3 were successive elements of an array (table). If you specify a national item for any argument, you must specify all arguments as class national.
RELATED TASKS
116
Processing table items using intrinsic functions on page 86 Returning variable-length results with alphanumeric or national functions
RELATED REFERENCES
MAX (Enterprise COBOL Language Reference) MIN (Enterprise COBOL Language Reference) ORD-MAX (Enterprise COBOL Language Reference) ORD-MIN (Enterprise COBOL Language Reference)
This code has the following results: v R2 is evaluated to be larger than R1. v The string fbbbb is moved to R3, where b represents a blank space. (The unfilled character positions in R3 are padded with spaces.) v L evaluates to the value 5. If R1 contained g instead of e, the code would have the following results: v R1 would evaluate as larger than R2. v The string gbbbbbbbbb would be moved to R3. (The unfilled character positions in R3 would be padded with spaces.) v The value 10 would be assigned to L. If a program uses national data for function arguments, the lengths and values of the function results could likewise vary. For example, the following code is identical to the fragment above, but uses national data instead of alphanumeric data.
01 01 01 01 . . R1 Pic n(10) national value "e". R2 Pic n(05) national value "f". R3 Pic n(20) national value spaces. L Pic 99 national. . Move Function Max(R1 R2) to R3 Compute L = Function Length(Function Max(R1 R2))
This code has the following results, which are similar to the first set of results except that these are for national characters: v R2 is evaluated to be larger than R1. v The string NX"0066 0020 0020 0020 0020" (the equivalent in national characters of fbbbb, where b represents a blank space), shown here in hexadecimal notation with added spaces for readability, is moved to R3. The unfilled character positions in R3 are padded with national spaces.
Chapter 6. Handling strings
117
v L evaluates to the value 5, the length in national character positions of R2. You might be dealing with variable-length output from alphanumeric or national functions. Plan your program accordingly. For example, you might need to think about using variable-length files when the records that you are writing could be of different lengths:
File Section. FD Output-File Recording Mode V. 01 Short-Customer-Record Pic X(50). 01 Long-Customer-Record Pic X(70). Working-Storage Section. 01 R1 Pic x(50). 01 R2 Pic x(70). . . . If R1 > R2 Write Short-Customer-Record from R1 Else Write Long-Customer-Record from R2 End-if
RELATED TASKS
Finding the largest or smallest data item on page 116 Performing arithmetic on page 57
RELATED REFERENCES
You can also use the LENGTH OF special register, which returns the length in bytes even for national data. Coding either Function Length(Customer-name) or LENGTH OF Customer-name returns the same result for alphanumeric items: the length of Customer-name in bytes. You can use the LENGTH function only where arithmetic expressions are allowed. However, you can use the LENGTH OF special register in a greater variety of contexts. For example, you can use the LENGTH OF special register as an argument to an intrinsic function that accepts integer arguments. (You cannot use an intrinsic function as an operand to the LENGTH OF special register.) You can also use the LENGTH OF special register as a parameter in a CALL statement.
118
RELATED TASKS
Performing arithmetic on page 57 Creating variable-length tables (DEPENDING ON) on page 81 Processing table items using intrinsic functions on page 86
RELATED REFERENCES
LENGTH (Enterprise COBOL Language Reference) LENGTH OF (Enterprise COBOL Language Reference)
You can instead use the WHEN-COMPILED special register to determine the date and time of compilation in the following format:
MM/DD/YYhh.mm.ss
The WHEN-COMPILED special register supports only a two-digit year, and carries the time out only to seconds. You can use this special register only as the sending field in a MOVE statement.
RELATED REFERENCES
119
120
Unicode and the encoding of language characters on page 125 National groups on page 129
RELATED TASKS
Using national data (Unicode) in COBOL on page 126 Converting to or from national (Unicode) representation on page 134
Copyright IBM Corp. 1991, 2009
121
Processing UTF-8 data on page 137 Processing Chinese GB 18030 data on page 138 Comparing national (UTF-16) data on page 139 Coding for use of DBCS support on page 141 Appendix C, Converting double-byte character set (DBCS) data, on page 703
RELATED REFERENCES
COBOL statements and national data Intrinsic functions and national data on page 124 CODEPAGE on page 310 NSYMBOL on page 331 Classes and categories of data (Enterprise COBOL Language Reference) Data categories and PICTURE rules (Enterprise COBOL Language Reference) MOVE statement (Enterprise COBOL Language Reference) General relation conditions (Enterprise COBOL Language Reference)
ADD
All identifiers can be numeric items that have USAGE NATIONAL. identifier-3 (GIVING) can be numeric-edited with USAGE NATIONAL. identifier-2, identifier-3, identifier-4, identifier-5; literal-2, literal-3 identifier-1 can be numeric or numeric-edited with USAGE NATIONAL. arithmetic-expression can contain numeric items that have USAGE NATIONAL. operand-1, operand-2 of the REPLACING phrase identifier-1
CALL
COMPUTE
Chapter 18, Compiler-directing statements, on page 363 Displaying values on a screen or in a identifier-1 is converted to EBCDIC only if the CONSOLE file (DISPLAY) on page 38 mnemonic-name is specified directly or indirectly.
122
Table 15. COBOL statements and national data (continued) COBOL statement DIVIDE Can be national All identifiers can be numeric items that have USAGE NATIONAL. identifier-3 (GIVING) and identifier-4 (REMAINDER) can be numeric-edited with USAGE NATIONAL. identifier-1; identifier-2 or literal-1 of the REPLACING phrase Comment For more information Using COMPUTE and other arithmetic statements on page 58
INITIALIZE
Examples: initializing data items on If you specify REPLACING NATIONAL or REPLACING page 30 NATIONAL-EDITED, identifier-2 or literal-1 must be valid as a sending operand in a move to identifier-1. If any of these (other than identifier-2, the TALLYING identifier) have USAGE NATIONAL, all must be national. Tallying and replacing data items (INSPECT) on page 111
INSPECT
All identifiers and literals. (identifier-2, the TALLYING integer data item, can have USAGE NATIONAL.) Method-name as identifier-2 or literal-1; identifier-3 or literal-2 in the BY VALUE phrase Merge keys Both the sender and receiver, or only the receiver
INVOKE
MERGE MOVE
The COLLATING SEQUENCE phrase does not apply. Implicit conversions are performed for valid MOVE operands.
Setting sort or merge criteria on page 221 Assigning values to elementary data items (MOVE) on page 34 Assigning values to group data items (MOVE) on page 35 Using COMPUTE and other arithmetic statements on page 58
MULTIPLY
All identifiers can be numeric items that have USAGE NATIONAL. identifier-3 (GIVING) can be numeric-edited with USAGE NATIONAL. Both the key data item and its object of comparison The key data item and its object of comparison must be compatible according to the rules of comparison. If the object of comparison is of class national, the key must be also. The COLLATING SEQUENCE phrase does not apply. If identifier-3, the receiving data item, is national, all identifiers and literals (other than identifier-4, the POINTER identifier) must be national.
SORT STRING
Sort keys All identifiers and literals. (identifier-4, the POINTER integer data item, can have USAGE NATIONAL.)
Setting sort or merge criteria on page 221 Joining data items (STRING) on page 101
123
Table 15. COBOL statements and national data (continued) COBOL statement SUBTRACT Can be national All identifiers can be numeric items that have USAGE NATIONAL. identifier-3 (GIVING) can be numeric-edited with USAGE NATIONAL. All identifiers and literals. (identifier-6 and identifier-7, the COUNT and TALLYING integer data items, respectively, can have USAGE NATIONAL.) identifier-1 (the generated XML document); identifier-2 (the source field or fields); identifier-4 or literal-4 (the namespace identifier); identifier-5 or literal-5 (the namespace prefix) identifier-1 (the XML document) If identifier-4, a receiving data item, has USAGE NATIONAL, the sending data item and each delimiter must have USAGE NATIONAL, and each literal must be national. Comment For more information Using COMPUTE and other arithmetic statements on page 58
UNSTRING
XML GENERATE
XML PARSE
Chapter 28, Processing XML input, The XML-NTEXT special on page 503 register contains national character document fragments during parsing. XML-NNAMESPACE and XML-NNAMESPACE-PREFIX special registers contain the associated namespace identifier and namespace prefix, if any, in national characters.
RELATED TASKS
Defining numeric data on page 45 Displaying numeric data on page 47 Using national data (Unicode) in COBOL on page 126 Comparing national (UTF-16) data on page 139
RELATED REFERENCES
CODEPAGE on page 310 Classes and categories of data (Enterprise COBOL Language Reference)
124
Table 16. Intrinsic functions and national character data (continued) Intrinsic function LENGTH LOWER-CASE, UPPER-CASE NUMVAL, NUMVAL-C MAX, MIN ORD-MAX, ORD-MIN REVERSE Function type Integer National Numeric National Integer National For more information Finding the length of data items on page 118 Converting to uppercase or lowercase (UPPER-CASE, LOWER-CASE) on page 113 Converting to numbers (NUMVAL, NUMVAL-C) on page 113 Finding the largest or smallest data item on page 116 Finding the largest or smallest data item on page 116 Transforming to reverse order (REVERSE) on page 113
You can use national decimal arguments wherever zoned decimal arguments are allowed. You can use national floating-point arguments wherever display floating-point arguments are allowed. (See the related reference below about arguments for a complete list of intrinsic functions that can take integer or numeric arguments.)
RELATED TASKS
Defining numeric data on page 45 Using national data (Unicode) in COBOL on page 126
RELATED REFERENCES
Arguments (Enterprise COBOL Language Reference) Classes and categories of data (Enterprise COBOL Language Reference)
125
One or more encoding units are used to represent a character from a coded character set. For UTF-16, an encoding unit takes 2 bytes of storage. Any character defined in any EBCDIC, ASCII, or EUC code page is represented in one UTF-16 encoding unit when the character is converted to the national data representation. Cross-platform considerations: Enterprise COBOL and COBOL for AIX support UTF-16 in big-endian format in national data. COBOL for Windows supports UTF-16 in little-endian format (UTF-16LE) in national data. If you are porting Unicode data that is encoded in UTF-16LE representation to Enterprise COBOL from another platform, you must convert that data to UTF-16 in big-endian format to process the data as national data.
RELATED TASKS
Storage of character data on page 133 Character sets and code pages (Enterprise COBOL Language Reference)
In addition, you can define national groups that contain only data items that explicitly or implicitly have USAGE NATIONAL, and that behave in the same way as elementary category national data items in most operations. These declarations affect the amount of storage that is needed.
RELATED CONCEPTS
Unicode and the encoding of language characters on page 125 National groups on page 129
RELATED TASKS
Defining national data items on page 127 Using national literals on page 127 Using national-character figurative constants on page 128 Defining national numeric data items on page 129 Using national groups on page 130 Converting to or from national (Unicode) representation on page 134 Comparing national (UTF-16) data on page 139
RELATED REFERENCES
Storage of character data on page 133 Classes and categories of data (Enterprise COBOL Language Reference)
126
NSYMBOL on page 331 BLANK WHEN ZERO clause (Enterprise COBOL Language Reference)
127
v NX'hexadecimal-digits' Each of the following MOVE statements sets the national data item Y to the UTF-16 value of the characters AB:
01 Y pic . . . Move Move Move NN usage national. NX"00410042" to Y N"AB" to Y "AB" to Y
Do not use alphanumeric hexadecimal literals in contexts that call for national literals, because such usage is easily misunderstood. For example, the following statement also results in moving the UTF-16 characters AB (not the hexadecimal bit pattern C1C2) to Y, where Y is defined as USAGE NATIONAL:
Move X"C1C2" to Y
You cannot use national literals in the SPECIAL-NAMES paragraph or as program-names. You can use a national literal to name an object-oriented method in the METHOD-ID paragraph or to specify a method-name in an INVOKE statement.
RELATED TASKS
128
The IF statement above evaluates as false even though each of its operands was set to HIGH-VALUE. Before an elementary alphanumeric operand is compared to a national operand, the alphanumeric operand is treated as though it were moved to a temporary national data item, and the alphanumeric characters are converted to the corresponding national characters. When X'FF' is converted to UTF-16, however, the UTF-16 item gets a substitution character value and so does not compare equally to NX'FFFF'.
RELATED TASKS
Converting to or from national (Unicode) representation on page 134 Comparing national (UTF-16) data on page 139
RELATED REFERENCES
Figurative constants (Enterprise COBOL Language Reference) DISPLAY-OF (Enterprise COBOL Language Reference) Support for Unicode: Using Unicode Services
National groups
National groups, which are specified either explicitly or implicitly with the GROUP-USAGE NATIONAL clause, contain only data items that have USAGE NATIONAL. In most cases, a national group item is processed as though it were redefined as an elementary category national item described as PIC N(m), where m is the number of national (UTF-16) characters in the group. For some operations on national groups, however (just as for some operations on alphanumeric groups), group semantics apply. Such operations (for example, MOVE CORRESPONDING and INITIALIZE) recognize or process the elementary items within the national group.
129
Where possible, use national groups instead of alphanumeric groups that contain USAGE NATIONAL items. National groups provide several advantages for the processing of national data compared to the processing of national data within alphanumeric groups: v When you move a national group to a longer data item that has USAGE NATIONAL, the receiving item is padded with national characters. By contrast, if you move an alphanumeric group that contains national characters to a longer alphanumeric group that contains national characters, alphanumeric spaces are used for padding. As a result, mishandling of data items could occur. v When you move a national group to a shorter data item that has USAGE NATIONAL, the national group is truncated at national-character boundaries. By contrast, if you move an alphanumeric group that contains national characters to a shorter alphanumeric group that contains national characters, truncation might occur between the 2 bytes of a national character. v When you move a national group to a national-edited or numeric-edited item, the content of the group is edited. By contrast, if you move an alphanumeric group to an edited item, no editing takes place. v When you use a national group as an operand in a STRING, UNSTRING, or INSPECT statement: The group content is processed as national characters rather than as single-byte characters. TALLYING and POINTER operands operate at the logical level of national characters. The national group operand is supported with a mixture of other national operand types. By contrast, if you use an alphanumeric group that contains national characters in these contexts, the characters are processed byte by byte. As a result, invalid handling or corruption of data could occur. USAGE NATIONAL groups: A group item can specify the USAGE NATIONAL clause at the group level as a convenient shorthand for the USAGE of each of the elementary data items within the group. Such a group is not a national group, however, but an alphanumeric group, and behaves in many operations, such as moves and compares, like an elementary data item of USAGE DISPLAY (except that no editing or conversion of data occurs).
RELATED TASKS
Assigning values to group data items (MOVE) on page 35 Joining data items (STRING) on page 101 Splitting data items (UNSTRING) on page 103 Tallying and replacing data items (INSPECT) on page 111 Using national groups
RELATED REFERENCES
130
01
Nat-Group-1 GROUP-USAGE NATIONAL. 02 Group-1. 04 Month PIC 99. 04 DayOf PIC 99. 04 Year PIC 9999. 02 Group-2 GROUP-USAGE NATIONAL. 04 Amount PIC 9(4).99 USAGE NATIONAL.
In the example above, Nat-Group-1 is a national group, and its subordinate groups Group-1 and Group-2 are also national groups. A GROUP-USAGE NATIONAL clause is implied for Group-1, and USAGE NATIONAL is implied for the subordinate items in Group-1. Month, DayOf, and Year are national decimal items, and Amount is a numeric-edited item that has USAGE NATIONAL. You can subordinate national groups within alphanumeric groups as in the following example:
01 Alpha-Group-1. 02 Group-1. 04 Month PIC 99. 04 DayOf PIC 99. 04 Year PIC 9999. 02 Group-2 GROUP-USAGE NATIONAL. 04 Amount PIC 9(4).99.
In the example above, Alpha-Group-1 and Group-1 are alphanumeric groups; USAGE DISPLAY is implied for the subordinate items in Group-1. (If Alpha-Group-1 specified USAGE NATIONAL at the group level, USAGE NATIONAL would be implied for each of the subordinate items in Group-1. However, Alpha-Group-1 and Group-1 would be alphanumeric groups, not national groups, and would behave like alphanumeric groups during operations such as moves and compares.) Group-2 is a national group, and USAGE NATIONAL is implied for the numeric-edited item Amount. You cannot subordinate alphanumeric groups within national groups. All elementary items within a national group must be explicitly or implicitly described as USAGE NATIONAL, and all group items within a national group must be explicitly or implicitly described as GROUP-USAGE NATIONAL.
RELATED CONCEPTS
Using national groups as elementary items Using national groups as group items on page 132
RELATED REFERENCES
131
01 01
Edited-date PIC NN/NN/NNNN USAGE NATIONAL. Group-1 GROUP-USAGE NATIONAL. 02 Month PIC 99 VALUE 06. 02 DayOf PIC 99 VALUE 23. 02 Year PIC 9999 VALUE 2009. . . . MOVE Group-1 to Edited-date.
If Group-1 were instead an alphanumeric group in which each of its subordinate items had USAGE NATIONAL (specified either explicitly with a USAGE NATIONAL clause on each elementary item, or implicitly with a USAGE NATIONAL clause at the group level), a group move, rather than an elementary move, would occur. Neither editing nor conversion would take place during the move. The value in the first eight character positions of Edited-date after the move would be 06232009 in national characters, and the value in the remaining two character positions would be 4 bytes of alphanumeric spaces.
RELATED TASKS
Assigning values to group data items (MOVE) on page 35 Comparing national data and alphanumeric-group operands on page 141 Using national groups as group items
RELATED REFERENCES
Because only Trans-quanN in Group-OneN above is numeric, only Trans-quanN receives the value 15. The other subordinate items are unchanged. The table below summarizes the cases where national groups are processed with group semantics.
Table 17. National group items that are processed with group semantics Language feature Uses of national group items Comment Elementary items within the national group are processed like elementary items that have USAGE NATIONAL within an alphanumeric group. The national group item is in effect shorthand for the set of host variables that are subordinate to the group item.
CORRESPONDING phrase Specify a national group item for of the ADD, SUBTRACT, processing as a group in or MOVE statement accordance with the rules of the CORRESPONDING phrase. Host variable in EXEC SQL statement Specify a national group item as a host variable.
132
Table 17. National group items that are processed with group semantics (continued) Language feature INITIALIZE statement Uses of national group items Specify a national group for processing as a group in accordance with the rules of the INITIALIZE statement. Use the name of a national group item to qualify the names of elementary data items and of subordinate group items in the national group. Comment Elementary items within the national group are initialized like elementary items that have USAGE NATIONAL within an alphanumeric group. Follow the same rules for qualification as for an alphanumeric group.
Name qualification
THROUGH phrase of the To specify a national group item in The result is an alphanumeric RENAMES clause the THROUGH phrase, use the same group item. rules as for an alphanumeric group item. FROM phrase of the XML GENERATE statement Specify a national group item in the FROM phrase for processing as a group in accordance with the rules of the XML GENERATE statement. Elementary items within the national group are processed like elementary items that have USAGE NATIONAL within an alphanumeric group.
RELATED TASKS
Initializing a structure (INITIALIZE) on page 32 Initializing a table (INITIALIZE) on page 76 Assigning values to elementary data items (MOVE) on page 34 Assigning values to group data items (MOVE) on page 35 Finding the length of data items on page 118 Generating XML output on page 543 Using national group items in SQL statements on page 422
RELATED REFERENCES
Qualification (Enterprise COBOL Language Reference) RENAMES clause (Enterprise COBOL Language Reference)
133
Table 18. Encoding and size of alphanumeric, DBCS, and national data (continued) Characteristic DISPLAY DISPLAY-1 NATIONAL
1. Use the CODEPAGE compiler option to specify the EBCDIC code page that is applicable to alphanumeric or DBCS data. 2. Most characters are represented in UTF-16 using one encoding unit. In particular, the following characters are represented using a single UTF-16 encoding unit per character: v COBOL characters A-Z, a-z, 0-9, space, + - * / = $ , ; . ( ) > < : v All characters that are converted from an EBCDIC or ASCII code page
RELATED CONCEPTS
Converting alphanumeric, DBCS, and integer to national (MOVE) Converting alphanumeric or DBCS to national (NATIONAL-OF) on page 135 Converting national to alphanumeric (DISPLAY-OF) on page 136 Overriding the default code page on page 136 Comparing national (UTF-16) data on page 139
RELATED REFERENCES
134
You can likewise move the following kinds of data to numeric-edited data items that have USAGE NATIONAL: v Alphanumeric v Display floating-point (floating-point of USAGE DISPLAY) v Numeric-edited of USAGE DISPLAY v Integer of USAGE DISPLAY For complete rules about moves to national data, see the related reference about the MOVE statement. For example, the MOVE statement below moves the alphanumeric literal "AB" to the national data item UTF16-Data:
01 UTF16-Data Pic N(2) Usage National. . . . Move "AB" to UTF16-Data
After the MOVE statement above, UTF16-Data contains NX'00410042', the national representation of the alphanumeric characters AB. If padding is required in a receiving data item that has USAGE NATIONAL, the default UTF-16 space character (NX'0020') is used. If truncation is required, it occurs at the boundary of a national-character position.
RELATED TASKS
Assigning values to elementary data items (MOVE) on page 34 Assigning values to group data items (MOVE) on page 35 Displaying numeric data on page 47 Coding for use of DBCS support on page 141
RELATED REFERENCES
Processing UTF-8 data on page 137 Processing Chinese GB 18030 data on page 138 Processing alphanumeric data items that contain DBCS data on page 143
RELATED REFERENCES
135
Processing UTF-8 data on page 137 Processing Chinese GB 18030 data on page 138
RELATED REFERENCES
Conversion exceptions
Implicit or explicit conversion between national data and alphanumeric data can fail and generate a severity-3 Language Environment condition. Failure can occur if the code page that you specified implicitly or explicitly is not a valid code page. A character that does not have a counterpart in the target CCSID does not result in a conversion exception. Such a character is converted to a substitution character in the target code page.
RELATED REFERENCES
136
The example above works correctly because the input code page is specified. Data-in-Greek is converted as data represented in CCSID 00875 (Greek). However, the following statement results in an incorrect conversion unless all the characters in the item happen to be among those that have a common representation in both the Greek and the English code pages:
Move Data-in-Greek to Data-in-Unicode
The MOVE statement above converts Data-in-Greek to Unicode representation based on the CCSID 00037 (U.S. English) to UTF-16 conversion. This conversion does not produce the expected results because Data-in-Greek is encoded in CCSID 00875. If you can correctly set the CODEPAGE compiler option to CCSID 00875 (that is, the rest of your program also handles EBCDIC data in Greek), you can code the same example correctly as follows:
CBL CODEPAGE(00875) * . . . 01 Data-in-Unicode pic N(100) usage national. 01 Data-in-Greek pic X(100). * . . . Read Greek-file into Data-in-Greek * . . . process Data-in-Greek here ... * . . . or do the following (if need to process data in Unicode): Move Data-in-Greek to Data-in-Unicode * . . . process Data-in-Unicode Move function Display-of(Data-in-Unicode) to Data-in-Greek Write Greek-record from Data-in-Greek
137
Usage note: Use care if you use reference modification to refer to data encoded in UTF-8. UTF-8 characters are encoded with a varying number of bytes per character. Avoid operations that might split a multibyte character.
RELATED TASKS
Referring to substrings of data items on page 107 Converting to or from national (Unicode) representation on page 134 Parsing XML documents encoded in UTF-8 on page 525
RELATED TASKS
Converting to or from national (Unicode) representation on page 134 Coding for use of DBCS support on page 141
138
RELATED REFERENCES
two class national operands class national and class numeric operands on page 140 national numeric and other numeric operands on page 140 national and other character-string operands on page 140 national data and alphanumeric-group operands on page 141
RELATED REFERENCES
Relation conditions (Enterprise COBOL Language Reference) General relation conditions (Enterprise COBOL Language Reference) National comparisons (Enterprise COBOL Language Reference) Group comparisons (Enterprise COBOL Language Reference)
139
When you compare operands that have unequal lengths, the shorter operand is treated as if it were padded on the right with default UTF-16 space characters (NX'0020') to the length of the longer operand. The PROGRAM COLLATING SEQUENCE clause does not affect the comparison of two class national operands.
RELATED CONCEPTS
140
These operands are treated as if they were moved to an elementary national data item. The characters are converted to national (UTF-16) representation, and the comparison proceeds with two national character operands.
RELATED TASKS
When the IF statement above is executed, the 10 bytes of the national literal N"AB123" are compared byte by byte to the content of Group-XN. The items compare equally, and EQUAL is displayed.
RELATED REFERENCES
RELATED TASKS
Declaring DBCS data on page 142 Using DBCS literals on page 142 Testing for valid DBCS characters on page 143
141
Processing alphanumeric data items that contain DBCS data on page 143 Appendix C, Converting double-byte character set (DBCS) data, on page 703
RELATED REFERENCES
142
An alphanumeric literal that contains DBCS characters is processed byte by byte, that is, with semantics appropriate for single-byte characters, except when it is converted explicitly or implicitly to national data representation, as for example in an assignment to or comparison with a national data item.
RELATED TASKS
DBCS on page 317 NSYMBOL on page 331 QUOTE/APOST on page 340 DBCS literals (Enterprise COBOL Language Reference)
Joining data items (STRING) on page 101 Splitting data items (UNSTRING) on page 103 Referring to substrings of data items on page 107 Converting to or from national (Unicode) representation on page 134
Chapter 7. Processing data in an international environment
143
144
Choosing file organization and access mode on page 147 Allocating files on page 149 Checking for input or output errors on page 150
145
Line-sequential file organization Line-sequential files are sequential files that reside on the hierarchical file system (HFS) and that contain only characters as data. Each record ends with a newline character. The only access (record transmission) mode allowed for line-sequential files is sequential. Indexed file organization Each record in the file contains a special field whose contents form the record key. The position of the key is the same in each record. The index component of the file establishes the logical arrangement of the file, an ordering by record key. The actual physical arrangement of the records in the file is not significant to your COBOL program. An indexed file can also use alternate indexes in addition to the record key. These keys let you access the file using a different logical ordering of the records. The access (record transmission) modes allowed for indexed files are sequential, random, or dynamic. When you read or write indexed files sequentially, the sequence is that of the key values. Relative file organization Records in the file are identified by their location relative to the beginning of the file. The first record in the file has a relative record number of 1, the tenth record has a relative record number of 10, and so on. The access (record transmission) modes allowed for relative files are sequential, random, or dynamic. When relative files are read or written sequentially, the sequence is that of the relative record number. With IBM Enterprise COBOL for z/OS, requests to the operating system for the storage and retrieval of records from input-output devices are handled by the two access methods QSAM and VSAM, and the UNIX file system. The device type upon which you elect to store your data could affect the choices of file organization available to you. Direct-access storage devices provide greater flexibility in the file organization options. Sequential-only devices limit organization options but have other characteristics, such as the portability of tapes, that might be useful. Sequential-only devices Terminals, printers, card readers, and punches are called unit-record devices because they process one line at a time. Therefore, you must also process records one at a time sequentially in your program when it reads from or writes to unit-record devices. On tape, records are ordered sequentially, so your program must process them sequentially. Use QSAM physical sequential files when processing tape files. The records on tape can be fixed length or variable length. Direct-access storage devices Direct-access storage devices hold many records. The record arrangement of files stored on these devices determines the ways that your program can process the data. When using direct-access devices, you have greater flexibility within your program, because your can use several types of file organization: v Sequential (VSAM or QSAM) v Line sequential (UNIX)
146
Allocating files on page 149 Chapter 9, Processing QSAM files, on page 151 Chapter 10, Processing VSAM files, on page 179 Chapter 11, Processing line-sequential files, on page 207 Choosing file organization and access mode
1. The data itself is in variable format but can be read into and written from COBOL fixed-length records.
RELATED REFERENCES
Format for coding input and output on page 148 Allowable control characters on page 208
147
The user-supplied information in the code above is as follows: (1) filename Any legal COBOL name. You must use the same file-name in the SELECT clause and in the FD entry, and on the READ, OPEN, and CLOSE statements. In addition, the file-name is required if you use the START or DELETE statements. This name is not necessarily the actual name of the data set as known to the system. Each file requires its own SELECT clause, FD entry, and input-output statements. (2) assignment-name Any name you choose, provided that it follows COBOL and system naming rules. The name can be 1-30 characters long if it is a user-defined word, or 1-160 characters long if it is a literal. You code the name part of the assignment-name on a DD statement, in an ALLOCATE command (TSO) or as an environment variable (for example, in an export command) (UNIX). (3) org The organization can be SEQUENTIAL, LINE SEQUENTIAL, INDEXED, or RELATIVE. This clause is optional for QSAM files. (4) access The access mode can be SEQUENTIAL, RANDOM, or DYNAMIC. For sequential file processing, including line-sequential, you can omit this clause. (5) file-status The COBOL file status key. You can specify the file status key as a
148
two-character category alphanumeric or category national item, or as a two-digit zoned decimal (USAGE DISPLAY) or national decimal (USAGE NATIONAL) item. (6) recordname The name of the record used in the WRITE and REWRITE statements. (7) fieldlength The logical length of the field. (8) type The record format of the file. If you break the record entry beyond the level-01 description, each element should map accurately against the fields in the record. (9) iomode The INPUT or OUTPUT mode. If you are only reading from a file, code INPUT. If you are only writing to it, code OUTPUT or EXTEND. If you are both reading and writing, code I-O, except for organization LINE SEQUENTIAL.
RELATED TASKS
Chapter 9, Processing QSAM files, on page 151 Chapter 10, Processing VSAM files, on page 179 Chapter 11, Processing line-sequential files, on page 207
Allocating files
For any type of file (sequential, line sequential, indexed, or relative) in your z/OS or UNIX applications, you can define the external name with either a ddname or an environment-variable name. The external name is the name in the assignment-name of the ASSIGN clause. If the file is in the HFS, you can use either a DD definition or an environment variable to define the file by specifying its path name with the PATH keyword. The environment-variable name must be uppercase. The allowable attributes for its value depend on the organization of the file being defined. Because you can define the external name in either of two ways, the COBOL run time goes through the following steps to find the definition of the file: 1. If the ddname is explicitly allocated, it is used. The definition can be from a DD statement in JCL, an ALLOCATE command from TSO/E, or a user-initiated dynamic allocation. 2. If the ddname is not explicitly allocated and an environment variable of the same name is set, the value of the environment variable is used. The file is dynamically allocated using the attributes specified by the environment variable. At a minimum, you must specify either the PATH() or DSN() option. All options and attributes must be in uppercase, except for the path-name suboption of the PATH option, which is case sensitive. You cannot specify a temporary data-set name in the DSN() option. File status code 98 results from any of the following: v The contents (including a value of null or all blanks) of the environment variable are not valid. v The dynamic allocation of the file fails. v The dynamic deallocation of the file fails.
Chapter 8. Processing files
149
The COBOL run time checks the contents of the environment variable at each OPEN statement. If a file with the same external name was dynamically allocated by a previous OPEN statement, and the contents of the environment variable have changed since that OPEN, the run time dynamically deallocates the previous allocation and reallocates the file using the options currently set in the environment variable. If the contents of the environment variable have not changed, the run time uses the current allocation. 3. If neither a ddname nor an environment variable is defined, the following steps occur: a. If the allocation is for a QSAM file and the CBLQDA runtime option is in effect, CBLQDA dynamic allocation processing takes place for those eligible files. This type of implicit dynamic allocation persists for the life of the run unit and cannot be reallocated. b. Otherwise, the allocation fails. The COBOL run time deallocates all dynamic allocations at run unit termination, except for implicit CBLQDA allocations.
RELATED TASKS
Setting and accessing environment variables on page 438 Defining and allocating QSAM files on page 166 Dynamically creating QSAM files on page 163 Allocating VSAM files on page 200
Handling errors in input and output operations on page 235 Coding ERROR declaratives on page 238 Using file status keys on page 239
150
Labels for QSAM files on page 174 z/OS DFSMS: Using Data Sets (Access methods)
RELATED TASKS
Defining QSAM files and records in COBOL Coding input and output statements for QSAM files on page 161 Handling errors in QSAM files on page 165 Working with QSAM files on page 166 Processing QSAM ASCII files on tape on page 177 Processing ASCII file labels on page 178
151
The ASSIGN clause name can include an S- before the external name to document that the file is a QSAM file. Both the ORGANIZATION and ACCESS MODE clauses are optional.
RELATED TASKS
A recording mode of S is not supported for files in the HFS. The above example is appropriate for such a file.
RELATED CONCEPTS
Logical records
RELATED TASKS
Requesting fixed-length format on page 153 Requesting variable-length format on page 154 Requesting spanned format on page 156 Requesting undefined format on page 158 Defining QSAM files and records in COBOL on page 151
RELATED REFERENCES
Logical records
COBOL uses the term logical record in a slightly different way than z/OS QSAM. For format-V and format-S files, a QSAM logical record includes a 4-byte prefix in front of the user data portion of the record that is not included in the definition of a COBOL logical record. For format-F and format-U files, and for HFS byte-stream files, the definitions of QSAM logical record and COBOL logical record are identical.
152
In this information, QSAM logical record refers to the QSAM definition, and logical record refers to the COBOL definition.
RELATED REFERENCES
of of of of
format-F records format-V records on page 155 format-S records on page 157 format-U records on page 159
Requesting variable-length format on page 154 Requesting spanned format on page 156 Requesting undefined format on page 158 Establishing record formats on page 152
RELATED REFERENCES
Layout of format-F records Layout of format-F records: The layout of format-F QSAM records is shown below.
153
RELATED CONCEPTS
Requesting fixed-length format on page 153 z/OS DFSMS: Using Data Sets (Fixed-length record formats)
RELATED REFERENCES
Layout of format-V records on page 155 Layout of format-S records on page 157 Layout of format-U records on page 159
154
example, when you specify a MOVE statement for a format-V record read in by the READ statement, the size of the record moved corresponds to its level-01 record description. When you specify a READ statement for a format-V file followed by a MOVE of the level-01 record, the actual record length is not used. The program will attempt to move the number of bytes described by the level-01 record description. If this number exceeds the actual record length and extends outside the area addressable by the program, results are unpredictable. If the number of bytes described by the level-01 record description is shorter than the physical record read, truncation of bytes beyond the level-01 description occurs. To find the actual length of a variable-length record, specify data-name-1 in format 3 of the RECORD clause of the File Definition (FD).
RELATED TASKS
Requesting fixed-length format on page 153 Requesting spanned format on page 156 Requesting undefined format on page 158 Establishing record formats on page 152
RELATED REFERENCES
FILE SECTION entries on page 14 Layout of format-V records Enterprise COBOL Compiler and Runtime Migration Guide (Moving from the VS COBOL II run time) Layout of format-V records: Format-V QSAM records have control fields that precede the data. The QSAM logical record length is determined by adding 4 bytes (for the control fields) to the record length defined in your program, but you must not include these 4 bytes in the description of the record and record length.
CC
The first 4 bytes of each block contain control information. LL Represents 2 bytes designating the length of the block (including the CC field). BB Represents 2 bytes reserved for system use.
cc
The first 4 bytes of each logical record contain control information. ll Represents 2 bytes designating the logical record length (including the cc field). bb Represents 2 bytes reserved for system use.
The block length is determined as follows: v Unblocked format-V records: CC + cc + the data portion
Chapter 9. Processing QSAM files
155
v Blocked format-V records: CC + the cc of each record + the data portion of each record The operating system provides the control bytes when the file is written; the control byte fields do not appear in your description of the logical record in the DATA DIVISION of your program. COBOL allocates input and output buffers large enough to accommodate the control bytes. These control fields in the buffer are not available for you to use in your program. When variable-length records are written on unit record devices, control bytes are neither printed nor punched. They appear, however, on other external storage devices, as well as in buffer areas of storage. If you move V-mode records from an input buffer to a WORKING-STORAGE area, theyll be moved without the control bytes. Files in the HFS are never blocked.
RELATED CONCEPTS
Layout of format-F records on page 153 Layout of format-S records on page 157 Layout of format-U records on page 159
156
v You have greater flexibility when you want to transfer logical records between direct access storage types. You will, however, have additional overhead in processing format-S files. Format-S files and READ INTO: When you specify a READ INTO statement for a format-S file, the compiler generates a MOVE statement that uses the size of the record that it just read for that file. If the record just read does not correspond to the level-01 record description, you might not get the result that you expect. All other rules of the MOVE statement apply.
RELATED CONCEPTS
Requesting fixed-length format on page 153 Requesting variable-length format on page 154 Requesting undefined format on page 158 Establishing record formats on page 152
RELATED REFERENCES
FILE SECTION entries on page 14 Layout of format-S records Spanned blocked and unblocked files: A spanned blocked QSAM file is made up of blocks, each containing one or more logical records or segments of logical records. A spanned unblocked file is made up of physical blocks, each containing one logical record or one segment of a logical record. In a spanned blocked file, a logical record can be either fixed or variable in length, and its size can be smaller than, equal to, or larger than the physical block size. There are no required relationships between logical records and physical block sizes. In a spanned unblocked file, the logical records can be either fixed or variable in length. When the physical block contains one logical record, the block length is determined by the logical record size. When a logical record has to be segmented, the system always writes the largest physical block possible. The system segments the logical record when the entire logical record cannot fit on a track.
RELATED CONCEPTS
Requesting spanned format on page 156 Layout of format-S records: Spanned records are preceded by control fields, as shown below.
157
Each block is preceded by a 4-byte block descriptor field (BDF in the image). There is only one block descriptor field at the beginning of each physical block. Each segment of a record in a block, even if the segment is the entire record, is preceded by a 4-byte segment descriptor field (SDF in the image). There is one segment descriptor field for each record segment in the block. The segment descriptor field also indicates whether the segment is the first, the last, or an intermediate segment. You do not describe these fields in the DATA DIVISION of your COBOL program, and the fields are not available for you to use in your program.
RELATED TASKS
Layout of format-F records on page 153 Layout of format-V records on page 155 Layout of format-U records on page 159
158
v If you omit the RECORD clause, the maximum record length is determined to be the size of the largest level-01 record description entry associated with the file. Format-U files and READ INTO: When you specify a READ INTO statement for a format-U file, the compiler generates a MOVE statement that uses the size of the record that it just read for that file. If the record just read does not correspond to the level-01 record description, you might not get the result that you expect. All other rules of the MOVE statement apply.
RELATED TASKS
Requesting fixed-length format on page 153 Requesting variable-length format on page 154 Requesting spanned format on page 156 Establishing record formats on page 152
RELATED REFERENCES
FILE SECTION entries on page 14 Layout of format-U records Layout of format-U records: With format-U, each block of external storage is handled as a logical record. There are no record-length or block-length fields.
RELATED CONCEPTS
Layout of format-F records on page 153 Layout of format-V records on page 155 Layout of format-S records on page 157
159
If your program uses QSAM files on tape, use a physical block size of at least 12 to 18 bytes. Otherwise, the block will be skipped over when a parity check occurs during one of the following actions: v Reading a block of records of fewer than 12 bytes v Writing a block of records of fewer than 18 bytes Larger blocks generally give you better performance. Blocks of only a few kilobytes are particularly inefficient; you should choose a block size of at least tens of kilobytes. If you specify record blocking and omit the block size, the system will pick a block size that is optimal for device utilization and for data transfer speed. Letting z/OS determine block size: To maximize performance, do not explicitly set the block size for a blocked file in your COBOL source program. For new blocked data sets, it is simpler to allow z/OS to supply a system-determined block size. To use this feature, follow these guidelines: v Code BLOCK CONTAINS 0 in your source program. v Do not code RECORD CONTAINS 0 in your source program. v Do not code a BLKSIZE value in the JCL DD statement. Setting block size explicitly: If you prefer to set a block size explicitly, your program will be most flexible if you follow these guidelines: v Code BLOCK CONTAINS 0 in your source program. v Code a BLKSIZE value in the ddname definition (the JCL DD statement). For extended-format data sets on z/OS, z/OS DFSMS adds a 32-byte block suffix to the physical record. If you specify a block size explicitly (using JCL or ISPF), do not include the size of this block suffix in the block size. This block suffix is not available for you to use in your program. z/OS DFSMS allocates the space used to read in the block suffix. However, when you calculate how many blocks of an extended-format data set will fit on a track of a direct-access device, you need to include the size of the block suffix in the block size. If you specify a block size that is larger than 32760 directly in the BLOCK CONTAINS clause or indirectly with the use of BLOCK CONTAINS n RECORDS, the OPEN of the data set fails with file status code 90 unless you define the data set to be on tape. For existing blocked data sets, it is simplest to: v Code BLOCK CONTAINS 0 in your source program. v Not code a BLKSIZE value in the ddname definition. When you omit the BLKSIZE from the ddname definition, the block size is automatically obtained by the system from the data-set label. Taking advantage of LBI: You can improve the performance of tape data sets by using the large block interface (LBI) for large block sizes. When the LBI is available, the COBOL run time automatically uses this facility for those tape files for which you use system-determined block size. LBI is also used for those files for which you explicitly define a block size in JCL or a BLOCK CONTAINS clause. Use of the LBI allows block sizes to exceed 32760 if the tape device supports it. The LBI is not used in all cases. An attempt to use a block size greater than 32760 in the following cases is diagnosed at compile time or results in a failure at OPEN: v Spanned records
160
v OPEN I-O Using a block size that exceeds 32760 might result in your not being able to read the tape on another system. A tape that you create with a block size greater than 32760 can be read only on a system that has a tape device that supports block sizes greater than 32760. If you specify a block size that is too large for the file, the device, or the operating system level, a runtime message is issued. To limit a system-determined block size to 32760, do not specify BLKSIZE anywhere, and set one of the following items to 32760: v The BLKSZLIM keyword on the DD statement for the data set v BLKSZLIM for the data class by using the BLKSZLIM keyword (must be set by your systems programmer) v A block-size limit for the system in the DEVSUPxx member of SYS1.PARMLIB by using the keyword TAPEBLKSZLIM (must be set by your systems programmer) The block-size limit is the first nonzero value that the compiler finds by checking these items. If no BLKSIZE or BLKSZLIM value is available from any source, the system limits BLKSIZE to 32760. You can then enable block sizes larger than 32760 in one of two ways: v Specify a BLKSZLIM value greater than 32760 in the DD statement for the file and use BLOCK CONTAINS 0 in your COBOL source. v Specify a value greater than 32760 for the BLKSIZE in the DD statement or in the BLOCK CONTAINS clause in your COBOL source. BLKSZLIM is device-independent. Block size and the DCB RECFM subparameter: Under z/OS, you can code the S or T option in the DCB RECFM subparameter: v Use the S (standard) option in the DCB RECFM subparameter for a format-F record with only standard blocks (ones that have no truncated blocks or unfilled tracks in the file, except for the last block of the file). S is also supported for records on tape. It is ignored if the records are not on DASD or tape. Using this standard block option might improve input-output performance, especially for direct-access devices. v The T (track overflow) option for QSAM files is no longer useful.
RELATED TASKS
Defining QSAM files and records in COBOL on page 151 z/OS DFSMS: Using Data Sets
RELATED REFERENCES
FILE SECTION entries on page 14 BLOCK0 on page 307 BLOCK CONTAINS clause (Enterprise COBOL Language Reference)
161
OPEN
Initiates the processing of files. You can open all QSAM files as INPUT, OUTPUT, or EXTEND (depending on device capabilities). You can also open QSAM files on direct access storage devices as I-O. You cannot open HFS files as I-O; a file status of 37 results if you attempt to do so.
READ
Reads a record from the file. With sequential processing, your program reads one record after another in the same order in which they were entered when the file was created. Creates a record in the file. Your program writes new records to the end of the file. Updates a record. You cannot update a file in the HFS using REWRITE.
RELATED TASKS
Opening QSAM files Adding records to QSAM files on page 163 Updating QSAM files on page 164 Writing QSAM files to a printer or spooled data set on page 164 Closing QSAM files on page 165
RELATED REFERENCES
OPEN statement (Enterprise COBOL Language Reference) READ statement (Enterprise COBOL Language Reference) WRITE statement (Enterprise COBOL Language Reference) REWRITE statement (Enterprise COBOL Language Reference) CLOSE statement (Enterprise COBOL Language Reference) File status key (Enterprise COBOL Language Reference)
162
Use the REVERSED option of the OPEN statement to process tape files in reverse order. The file is positioned at the end, and READ statements read the data records in reverse order, starting with the last record. The REVERSED option is supported only for files that have fixed-length records.
RELATED TASKS
Dynamically creating QSAM files Ensuring that file attributes match your program on page 170
RELATED REFERENCES
163
To add records to a file opened as I-O, you must first close the file and open it as EXTEND.
RELATED REFERENCES
READ statement (Enterprise COBOL Language Reference) WRITE statement (Enterprise COBOL Language Reference)
164
Specify the number of lines the page is advanced with an integer (or an identifier with a mnemonic-name) following ADVANCING. If you omit the ADVANCING phrase from a WRITE statement, the effect is as if you had coded:
AFTER ADVANCING 1 LINE
RELATED REFERENCES
165
COBOL provides these ways for you to intercept and handle certain QSAM input and output errors: v End-of-file phrase (AT END) v EXCEPTION/ERROR declarative v FILE STATUS clause v INVALID KEY phrase If you do not code a FILE STATUS key or a declarative, serious QSAM processing errors will cause a message to be issued and a Language Environment condition to be signaled, which will cause an abend if you specify the runtime option ABTERMENC(ABEND). If you use the FILE STATUS clause or the EXCEPTION/ERROR declarative, code EROPT=ACC in the DCB of the DD statement for that file. Otherwise, your COBOL program will not be able to continue processing after some error conditions. If you use the FILE STATUS clause, be sure to check the key and take appropriate action based on its value. If you do not check the key, your program might continue, but the results will probably not be what you expected.
RELATED TASKS
Defining and allocating QSAM files Retrieving QSAM files on page 169 Ensuring that file attributes match your program on page 170 Using striped extended-format QSAM data sets on page 172
RELATED REFERENCES
166
Restriction: You cannot create a PDS or PDSE by using an environment variable. You can optionally specify the following attributes in any order after DSN: v v v v v v v v A disposition value, one of: NEW, OLD, SHR, or MOD TRACKS or CYL SPACE(nnn,mmm) VOL(volume-serial) UNIT(type) KEEP, DELETE, CATALOG, or UNCATALOG STORCLAS(storage-class) MGMTCLAS(management-class)
v DATACLAS(data-class) You can use either an environment variable or a DD definition to define a file in the HFS. To do so, define one of the following items with a name that matches the external name in the ASSIGN clause: v A DD allocation that uses PATH='absolute-path-name' and FILEDATA=BINARY v An environment variable with a value PATH(pathname), where pathname is an absolute path name (starting with /) For compatibility with releases of COBOL before COBOL for OS/390 & VM Version 2 Release 2, you can also specify FILEDATA=TEXT when using a DD allocation for HFS files, but this use is not recommended. To process text files in the HFS, use LINE SEQUENTIAL organization. If you do use QSAM to process text files in the HFS, you cannot use environment variables to define the files. When you define a QSAM file, use the parameters as shown below.
Table 20. QSAM file allocation What you want to do Name the file. Select the type and quantity of input-output devices to be allocated for the file. DD parameter to use DSNAME (data-set name) UNIT EV keyword to use DSN UNIT for type only
Give instructions for the volume in VOLUME (or let the system VOL which the file will reside and for choose an output volume) volume mounting. Allocate the type and amount of space the file needs. (Only for direct-access storage devices.) Specify the type and some of the contents of the label associated with the file. Indicate whether you want to catalog, pass, or keep the file after the job step is completed. Complete any data control block information that you want to add. SPACE SPACE for the amount of space (primary and secondary only); TRACKS or CYL for the type of space n/a
LABEL
DISP
NEW, OLD, SHR, MOD plus KEEP, DELETE, CATALOG, or UNCATALOG n/a
DCB subparameters
167
Some of the information about the QSAM file must always be coded in the FILE-CONTROL paragraph, the FD entry, and other COBOL clauses. Other information must be coded in the DD statement or environment variable for output files. For input files, the system can obtain information from the file label (for standard label files). If DCB information is provided in the DD statement for input files, it overrides information on the data-set label. For example, the amount of space allocated for a new direct-access device file can be set in the DD statement by the SPACE parameter. You cannot express certain characteristics of QSAM files in the COBOL language, but you can code them in the DD statement for the file by using the DCB parameter. Use the subparameters of the DCB parameter to provide information that the system needs for completing the data set definition, including the following items: v Block size (BLKSIZE=), if BLOCK CONTAINS 0 RECORDS was coded at compile time (recommended) v Options to be executed if an error occurs in reading or writing a record v TRACK OVERFLOW or standard blocks v Mode of operation for a card reader or punch DCB attributes coded for a DD DUMMY do not override those coded in the FD entry of your COBOL program. Example: setting and accessing environment variables on page 440
RELATED TASKS
Setting block sizes on page 159 Defining QSAM files and records in COBOL on page 151 Allocating files on page 149
RELATED REFERENCES
Parameters for creating QSAM files on page 169 MVS Program Management: Users Guide and Reference
168
DSNAME= DSN=
UNIT=
VOLUME= ( [PRIVATE] [,RETAIN] [,vol-sequence-num] [,volume-count] ... VOL= ... ,SER=(volume-serial[,volume-serial]...) ( ,REF= dsname *.ddname *.stepname.ddname *.stepname.procstep.ddname SPACE= ( TRK ,(primary-quantity[,secondary-quantity][,directory-quantity])) CYL average-record-length LABEL= ( [Data-set-sequence-number,] NL SL SUL DISP=
( NEW MOD ,DELETE ,KEEP ,PASS ,CATLG ,DELETE ,KEEP ,CATLG ) yyddd yyyy/ddd ,RETPD=xxxx ,EXPDT= (
DCB=
( subparameter-list )
RELATED TASKS
169
RELATED TASKS
170
RELATED TASKS
Processing existing files Processing new files on page 172 Opening QSAM files on page 162
RELATED REFERENCES
The easiest way to define variable-length (format-V) records in a program is to use the RECORD IS VARYING FROM integer-1 TO integer-2 clause in the FD entry and set an appropriate value for integer-2. Express the integer sizes in bytes regardless of the underlying USAGE of the data items in the record. For example, assume that you determine that the length attribute of the data set is 104 bytes (LRECL=104). Remembering that the maximum record length is determined from the RECORD IS VARYING clause and not from the level-01 record descriptions, you could define a format-V file in your program with this code:
FILE SECTION. FD COMMUTER-FILE-MST RECORDING MODE IS V RECORD IS VARYING FROM 4 TO 100 CHARACTERS. 01 COMMUTER-RECORD-A PIC X(4). 01 COMMUTER-RECORD-B PIC X(75).
Assume that the existing file in the previous example was format-U instead of format-V. If the 104 bytes are all user data, you could define the file in your program with this code:
FILE SECTION. FD COMMUTER-FILE-MST RECORDING MODE IS U RECORD IS VARYING FROM 4 TO 104 CHARACTERS. 01 COMMUTER-RECORD-A PIC X(4). 01 COMMUTER-RECORD-B PIC X(75).
To define fixed-length records in your program, either code the RECORD CONTAINS integer clause, or omit this clause and code all level-01 record descriptions to be the same fixed size. In either case, use a value that equals the value of the length attribute of the data set. If you intend to use the same program to process different files at run time, and those files have differing fixed lengths, avoid record-length conflicts by coding RECORD CONTAINS 0. If the existing file is an ASCII data set (DCB=(OPTCD=Q)), you must use the CODE-SET clause in the FD entry for the file.
171
RELATED TASKS
Processing new files Requesting fixed-length format on page 153 Requesting variable-length format on page 154 Requesting undefined format on page 158 Opening QSAM files on page 162
RELATED REFERENCES
Processing existing files on page 171 Requesting fixed-length format on page 153 Requesting variable-length format on page 154 Requesting undefined format on page 158 Opening QSAM files on page 162 Dynamically creating QSAM files on page 163
RELATED REFERENCES
172
A striped extended-format QSAM data set is an extended-format QSAM data set that is spread over multiple volumes, thus allowing parallel data access. For you to gain the maximum benefit from using QSAM striped data sets, z/OS DFSMS needs to be able to allocate the required number of buffers above the 16-MB line. When you develop applications that contain files allocated to QSAM striped data sets, follow these guidelines: v Avoid using a QSAM striped data set for a file that cannot have buffers allocated above the 16-MB line. v Omit the RESERVE clause in the FILE-CONTROL entry for the file. Doing so lets z/OS DFSMS determine the optimum number of buffers for the data set. v Compile your program with the DATA(31) and RENT compiler options, and make the load module AMODE 31. v Specify the ALL31(ON) runtime option if the file is an EXTERNAL file with format-F, format-V, or format-U records. Note that all striped data sets are extended-format data sets, but not all extended-format data sets are striped.
RELATED TASKS
173
Allocating files on page 149 Defining and allocating QSAM files on page 166 z/OS DFSMS: Using Data Sets (Using HFS data sets)
174
There are two kinds of labels: standard and nonstandard. Enterprise COBOL does not support nonstandard user labels. In addition, standard user labels contain user-specified information about the associated data set. Standard labels consist of volume labels and groups of data-set labels. Volume labels precede or follow data on the volume, and identify and describe the volume. The data-set labels precede or follow each data set on the volume, and identify and describe the data set. v The data-set labels that precede the data set are called header labels. v The data-set labels that follow the data set are called trailer labels. They are similar to the header labels, except that they also contain a count of blocks in the data set. v The data-set label groups can optionally include standard user labels. v The volume label groups can optionally include standard user labels.
RELATED TASKS
175
Table 22. Handling of QSAM user labels When the file is opened as: INPUT And: USE . . . LABEL declarative is coded for the OPEN option or for the file. USE . . . LABEL declarative is coded for the OPEN option or for the file. CLOSE statement is performed for the file that has reached the AT END condition. Result: The label is read and control is passed to the LABEL declarative. A buffer area for the label is provided, and control is passed to the LABEL declarative. Control is passed to the LABEL declarative for processing trailer labels.
OUTPUT
INPUT or I-O
You can specify a special exit by using the statement GO TO MORE-LABELS. When this statement results in an exit from a label DECLARATIVE SECTION, the system takes one of the following actions: v Writes the current beginning or ending label, and then reenters the USE section at its beginning to create more labels. After creating the last label, the system exits by performing the last statement of the section. v Reads an additional beginning or ending label, and then reenters the USE section at its beginning to check more labels. When processing user labels, the system reenters the section only if there is another user label to check. Hence, a program path that flows through the last statement in the section is not needed. If a GO TO MORE-LABELS statement is not performed for a user label, the DECLARATIVE SECTION is not reentered to check or create any immediately succeeding user labels.
RELATED CONCEPTS
The format of the label for a direct-access volume is the almost the same as the format of the label group for a tape volume label group. The difference is that a data-set label of the initial DASTO volume label consists of the data set control block (DSCB). The DSCB appears in the volume table of contents (VTOC) and contains the equivalent of the tape data set header and trailer, in addition to control information such as space allocation.
176
Defining the record formats: Process QSAM ASCII tape files with any of these record formats: v Fixed length (format F) v Undefined (format U) v Variable length (format V) If you use variable-length records, you cannot explicitly code format D; instead, code RECORDING MODE V. The format information is internally converted to D mode. D-mode records have a 4-byte record descriptor for each record.
177
Defining the ddname: Under z/OS, processing ASCII files requires special JCL coding. Code these subparameters of the DCB parameter in the DD statement: BUFOFF=[L|n] L n A 4-byte block prefix that contains the block length (including the block prefix) The length of the block prefix: v For input, from 0 through 99 v For output, either 0 or 4 Use this value if you coded BLOCK CONTAINS 0. BLKSIZE=n n The size of the block, including the length of the block prefix
LABEL=[AL|AUL|NL] AL AUL NL OPTCD=Q Q This value is required for ASCII files and is the default if the file is created using Enterprise COBOL. American National Standard (ANS) labels ANS and user labels No labels
RELATED TASKS
178
The term file in this VSAM documentation refers to either a COBOL file or a VSAM data set. If you have complex requirements or frequently use VSAM, review the VSAM publications for your operating system.
RELATED CONCEPTS
Defining VSAM file organization and records on page 181 Coding input and output statements for VSAM files on page 187 Handling errors in VSAM files on page 195 Protecting VSAM files with a password on page 196
Copyright IBM Corp. 1991, 2009
179
Working with VSAM data sets under z/OS and z/OS UNIX on page 197 Improving VSAM performance on page 203
RELATED REFERENCES
z/OS DFSMS: Using Data Sets z/OS DFSMS Macro Instructions for Data Sets z/OS DFSMS: Access Method Services for Catalogs
VSAM files
The physical organization of VSAM data sets differs considerably from the organizations used by other access methods. VSAM data sets are held in control intervals (CI) and control areas (CA). The size of the CI and CA is normally determined by the access method, and the way in which they are used is not visible to you. You can use three types of file organization with VSAM: VSAM sequential file organization (Also referred to as VSAM ESDS (entry-sequenced data set) organization.) In VSAM sequential file organization, the records are stored in the order in which they were entered. VSAM entry-sequenced data sets are equivalent to QSAM sequential files. The order of the records is fixed. VSAM indexed file organization (Also referred to as VSAM KSDS (key-sequenced data set) organization.) In a VSAM indexed file (KSDS), the records are ordered according to the collating sequence of an embedded prime key field, which you define. The prime key consists of one or more consecutive characters in the records. The prime key uniquely identifies the record and determines the sequence in which it is accessed with respect to other records. A prime key for a record might be, for example, an employee number or an invoice number. VSAM relative file organization (Also referred to as VSAM fixed-length or variable-length RRDS (relative-record data set) organization.) A VSAM relative-record data set (RRDS) contains records ordered by their relative key. The relative key is the relative record number, which represents the location of the record relative to where the file begins. The relative record number identifies the fixed- or variable-length record. In a VSAM fixed-length RRDS, records are placed in a series of fixed-length slots in storage. Each slot is associated with a relative record number. For example, in a fixed-length RRDS containing 10 slots, the first slot has a relative record number of 1, and the tenth slot has a relative record number of 10. In a VSAM variable-length RRDS, the records are ordered according to their relative record number. Records are stored and retrieved according to the relative record number that you set. Throughout this documentation, the term VSAM relative-record data set (or RRDS) is used to mean both relative-record data sets with fixed-length records and with variable-length records, unless they need to be differentiated.
180
The following table compares the characteristics of the different types of VSAM data sets.
Table 25. Comparison of VSAM data-set types Characteristic Order of records Access Alternate indexes Entry-sequenced data set (ESDS) Order in which they are written Sequential Can have one or more alternate indexes, although not supported in COBOL Key-sequenced data set (KSDS) Collating sequence by key field By key through an index Can have one or more alternate indexes RBA can change. Relative-record data set (RRDS) Order of relative record number By relative record number, which is handled like a key Cannot have alternate indexes
Relative byte address RBA cannot change. (RBA) and relative record number (RRN) of a record Space for adding records Uses space at the end of the data set
Uses distributed free space for inserting records and changing their lengths in place
For fixed-length RRDS, uses empty slots in the data set For variable-length RRDS, uses distributed free space and changes the lengths of added records in place Space from a deleted record can be reused.
You cannot delete a record, but you can reuse its space for a record of the same length. Can have spanned records Can be reused unless it has an alternate index, is associated with key ranges, or exceeds 123 extents per volume
Space from a deleted or shortened record is automatically reclaimed in a control interval. Can have spanned records
Can be reused Can be reused unless it has an alternate index, is associated with key ranges, or exceeds 123 extents per volume
RELATED TASKS
Specifying sequential organization for VSAM files on page 182 Specifying indexed organization for VSAM files on page 182 Specifying relative organization for VSAM files on page 184 Defining VSAM files on page 197
181
Table 26. VSAM file organization, access mode, and record format File organization VSAM sequential (ESDS) VSAM indexed (KSDS) VSAM relative (RRDS) Sequential access Yes Yes Yes Random access No Yes Yes Dynamic access No Yes Yes Fixed length Yes Yes Yes Variable length Yes Yes Yes
RELATED TASKS
Specifying sequential organization for VSAM files Specifying indexed organization for VSAM files Specifying relative organization for VSAM files on page 184 Specifying access modes for VSAM files on page 185 Defining record lengths for VSAM files on page 185 Using file status keys on page 239 Using VSAM status codes (VSAM files only) on page 241 Defining VSAM files on page 197
In the example above, data-name is the name of the prime key field as you define it in the record description entry in the DATA DIVISION. The prime key data item can be class alphabetic, alphanumeric, DBCS, numeric, or national. If it has USAGE NATIONAL, the prime key can be category national, or can be a national-edited,
182
numeric-edited, national decimal, or national floating-point data item. The collation of record keys is based on the binary value of the keys regardless of the class or category of the keys. The following example shows the statements for a VSAM indexed file (KSDS) that is accessed dynamically. In addition to the primary key, COMMUTER-NO, an alternate key, LOCATION-NO, is specified:
SELECT I-FILE ASSIGN TO INDEXED-FILE ORGANIZATION IS INDEXED ACCESS IS DYNAMIC RECORD KEY IS IFILE-RECORD-KEY ALTERNATE RECORD KEY IS IFILE-ALTREC-KEY FILE STATUS IS FSTAT-CODE VSAM-CODE.
RELATED CONCEPTS
RECORD KEY clause (Enterprise COBOL Language Reference) Classes and categories of data (Enterprise COBOL Language Reference)
In the example above, data-name is the name of the alternate key field as you define it in the record description entry in the DATA DIVISION. Alternate key data items, like prime key data items, can be class alphabetic, alphanumeric, DBCS, numeric, or national. The collation of alternate keys is based on the binary value of the keys regardless of the class or category of the keys.
183
RELATED TASKS
You can use a randomizing routine to associate a key value in each record with the relative record number for that record. Although there are many techniques to convert a record key to a relative record number, the most commonly used is the division/remainder technique. With this technique, you divide the key by a value equal to the number of slots in the data set to produce a quotient and remainder. When you add one to the remainder, the result is a valid relative record number. Alternate indexes are not supported for VSAM RRDS.
RELATED CONCEPTS
184
RELATED TASKS
185
Because the concept of blocking has no meaning for VSAM files, you can omit the BLOCK CONTAINS clause. The clause is syntax-checked, but it has no effect on how the program runs.
RELATED TASKS
Defining fixed-length records Defining variable-length records Enterprise COBOL Compiler and Runtime Migration Guide
RELATED REFERENCES
Omit the RECORD clause, but code all level-01 records that are associated with the file as the same size; and code none with an OCCURS DEPENDING ON clause.
RELATED REFERENCES
Size of the largest level-01 The compiler determines record description entry the maximum record associated with the file length. integer-5 bytes The minimum record length is integer-4 bytes.
186
Table 28. Definition of VSAM variable-length records (continued) RECORD clause Omit the RECORD clause, but code multiple level-01 records that are associated with the file and are of different sizes or contain an OCCURS DEPENDING ON clause. Clause format Maximum record length Comments
Size of the largest level-01 The compiler determines record description entry the maximum record associated with the file length.
When you specify a READ INTO statement for a format-V file, the record size that is read for that file is used in the MOVE statement generated by the compiler. Consequently, you might not get the result you expect if the record read in does not correspond to the level-01 record description. All other rules of the MOVE statement apply. For example, when you specify a MOVE statement for a format-V record read in by the READ statement, the size of the record corresponds to its level-01 record description.
RELATED REFERENCES
All of the following factors determine which input and output statements you can use for a given VSAM data set: v Access mode (sequential, random, or dynamic) v File organization (ESDS, KSDS, or RRDS) v Mode of OPEN statement (INPUT, OUTPUT, I-O, or EXTEND) The following table shows the possible combinations of statements and open modes for sequential files (ESDS). The X indicates that you can use a statement with the open mode shown at the top of the column.
187
Table 29. I/O statements for VSAM sequential files Access mode Sequential COBOL statement OPEN WRITE START READ REWRITE DELETE CLOSE X X X X X X X OPEN INPUT X OPEN OUTPUT X X OPEN I-O X OPEN EXTEND X X
The following table shows the possible combinations of statements and open modes you can use with indexed (KSDS) files and relative (RRDS) files. The X indicates that you can use the statement with the open mode shown at the top of the column.
Table 30. I/O statements for VSAM relative and indexed files Access mode Sequential COBOL statement OPEN WRITE START READ REWRITE DELETE CLOSE Random OPEN WRITE START READ REWRITE DELETE CLOSE Dynamic OPEN WRITE START READ REWRITE DELETE CLOSE X X X X X X X X X X X X X X X X X X X X X X X X X X X X OPEN INPUT X OPEN OUTPUT X X X X X X X X X X OPEN I-O X OPEN EXTEND X X
The fields that you code in the FILE STATUS clause are updated by VSAM after each input-output statement to indicate the success or failure of the operation.
188
RELATED CONCEPTS
Opening a file (ESDS, KSDS, or RRDS) Reading records from a VSAM file on page 192 Updating records in a VSAM file on page 193 Adding records to a VSAM file on page 193 Replacing records in a VSAM file on page 194 Deleting records from a VSAM file on page 194 Closing VSAM files on page 194
RELATED REFERENCES
189
RELATED TASKS
Opening an empty file Opening a loaded file (a file with records) on page 191
RELATED REFERENCES
190
In addition to extra overhead for preparing files for use, random processing does not consider any user-defined free space. As a result, any future insertions might be inefficient. Sequential processing maintains user-defined free space. When you are loading an extended-format VSAM data set, file status 30 will occur for the OPEN if z/OS DFSMS system-managed buffering sets the buffering to local shared resources (LSR). To successfully load the VSAM data set in this case, specify ACCBIAS=USER in the DD AMP parameter for the VSAM data set to bypass system-managed buffering. Loading a VSAM data set with access method services: You can load or update a VSAM data set by using the access method services REPRO command. Use REPRO whenever possible.
RELATED TASKS
Statements to load records into a VSAM file z/OS DFSMS: Access Method Services for Catalogs (REPRO)
RELATED TASKS
Opening an empty file on page 190 Updating records in a VSAM file on page 193
191
RELATED TASKS
Opening an empty file on page 190 Working with VSAM data sets under z/OS and z/OS UNIX on page 197
RELATED REFERENCES
Statements to load records into a VSAM file on page 191 z/OS DFSMS: Access Method Services for Catalogs
When a direct READ is performed for a VSAM indexed file, based on an alternate index for which duplicates exist, only the first record in the data set (base cluster) with that alternate key value is retrieved. You need a series of READ NEXT statements to retrieve each of the data set records with the same alternate key. A file status code of 02 is returned if there are more records with the same alternate key value to be read; a code of 00 is returned when the last record with that key value has been read.
RELATED CONCEPTS
192
RELATED TASKS
Not applicable
Not applicable
RELATED REFERENCES
193
Sequential files are always written sequentially. For indexed files, you must write new records in ascending key sequence. If you open the file EXTEND, the record keys of the records to be added must be higher than the highest primary record key on the file when you opened the file. For relative files, the records must be in sequence. If you include a RELATIVE KEY data item in the SELECT clause, the relative record number of the record to be written is placed in that data item. Adding records randomly or dynamically: When you write records to an indexed data set and ACCESS IS RANDOM or ACCESS IS DYNAMIC, you can write the records in any order.
194
If you do not close a VSAM file, the file is automatically closed for you under the following conditions, except for files defined in any OS/VS COBOL programs in the run unit: v When the run unit ends normally, all open files defined in any COBOL programs in the run unit are closed. v When the run unit ends abnormally, if the TRAP(ON) runtime option has been set, all open files defined in any COBOL programs in the run unit are closed. v When Language Environment condition handling has completed and the application resumes in a routine other than where the condition occurred, open files defined in any COBOL programs in the run unit that might be called again and reentered are closed. You can change the location where a program resumes after a condition is handled. To make this change, you can, for example, move the resume cursor with the CEEMRCR callable service or use language constructs such as a C longjmp statement. v When you issue CANCEL for a COBOL subprogram, any open nonexternal files defined in that program are closed. v When a COBOL subprogram with the INITIAL attribute returns control, any open nonexternal files defined in that program are closed. v When a thread of a multithreaded application ends, both external and nonexternal files that were opened from within that same thread are closed. File status key data items in the DATA DIVISION are set when these implicit CLOSE operations are performed, but your EXCEPTION/ERROR and LABEL declaratives are not invoked. Errors: If you open a VSAM file in a multithreaded application, you must close it from the same thread of execution. Attempting to close the file from a different thread results in a close failure with file-status condition 90.
195
If you do not code a file status key or a declarative, serious VSAM processing errors will cause a message to be issued and a Language Environment condition to be signaled, which will cause an abend if you specify the runtime option ABTERMENC(ABEND).
RELATED TASKS
Handling errors in input and output operations on page 235 Using VSAM status codes (VSAM files only) on page 241
RELATED REFERENCES
z/OS DFSMS Macro Instructions for Data Sets (VSAM macro return and reason codes)
196
PASSWORD IS BASE-PASS ALTERNATE RECORD KEY IS EMPL-PHONE PASSWORD IS PATH1-PASS . . . WORKING-STORAGE SECTION. 01 BASE-PASS PIC X(8) VALUE "25BSREAD". 01 PATH1-PASS PIC X(8) VALUE "25ATREAD".
Working with VSAM data sets under z/OS and z/OS UNIX
Be aware of special coding considerations for VSAM files under z/OS and z/OS UNIX for access method services (IDCAMS) commands, environment variables, and JCL. A VSAM file is available if all of the following conditions are true: v You define it using access method services. v You define it for your program by providing a DD statement, an environment variable, or an ALLOCATE command. v It has previously contained a record. A VSAM file is unavailable if it has never contained a record, even if you have defined the file. You always get a return code of zero on completion of the OPEN statement for a VSAM sequential file. Use the access method services REPRO command to empty a file. Deleting records in this manner resets the high-use relative byte address (RBA) of the file to zero. The file is effectively empty and appears to COBOL as if it never contained a record.
RELATED TASKS
Defining files to the operating system on page 10 Defining VSAM files Creating alternate indexes on page 198 Allocating VSAM files on page 200 Sharing VSAM files through RLS on page 202
197
v Name of the catalog to contain this definition and its password (can use default name) v Organization (sequential, indexed, or relative) v Device and volumes that the data set will occupy v Space required for the data set v Record size and control interval sizes (CISIZE) v Passwords (if any) required for future access Depending on what kind of data set is in the cluster, also define the following information for each cluster: v For VSAM indexed data sets (KSDS), specify length and position of the prime key in the records. v For VSAM fixed-length relative-record data sets (RRDS), specify the record size as greater than or equal to the maximum size COBOL record:
DEFINE CLUSTER NUMBERED RECORDSIZE(n,n)
When you define a data set in this way, all records are padded to the fixed slot size n. If you use the RECORD IS VARYING ON data-name form of the RECORD clause, a WRITE or REWRITE uses the length specified in DEPENDING ON data-name as the length of the record to be transferred by VSAM. This data is then padded to the fixed slot size. READ statements always return the fixed slot size in the DEPENDING ON data-name. v For VSAM variable-length relative-record data sets (RRDS), specify the average size COBOL record expected and the maximum size COBOL record expected:
DEFINE CLUSTER NUMBERED RECORDSIZE(avg,m)
The average size COBOL record expected must be less than the maximum size COBOL record expected.
RELATED TASKS
Creating alternate indexes Allocating VSAM files on page 200 Specifying relative organization for VSAM files on page 184
RELATED REFERENCES
198
and the prime index). After you define an alternate index, make a catalog entry to establish the relationship (or path) between the alternate index and its base cluster. This path allows you to access the records of the base cluster through the alternate keys. To use an alternate index, do these steps: 1. Define the alternate index by using the DEFINE ALTERNATEINDEX command. In it, specify these items: v Name of the alternate index v v v v Name of its related VSAM indexed data set Location in the record of any alternate indexes and whether they are unique Whether alternate indexes are to be updated when the data set is changed Name of the catalog to contain this definition and its password (can use default name)
In your COBOL program, the alternate index is identified solely by the ALTERNATE RECORD KEY clause in the FILE-CONTROL paragraph. The ALTERNATE RECORD KEY definitions must match the definitions in the catalog entry. Any password entries that you cataloged should be coded directly after the ALTERNATE RECORD KEY phrase. 2. Relate the alternate index to the base cluster (the data set to which the alternate index gives you access) by using the DEFINE PATH command. In it, specify these items: v Name of the path v Alternate index to which the path is related v Name of the catalog that contains the alternate index The base cluster and alternate index are described by entries in the same catalog. 3. Load the VSAM indexed data set. 4. Build the alternate index by using (typically) the BLDINDEX command. Identify the input file as the indexed data set (base cluster) and the output file as the alternate index or its path. BLDINDEX reads all the records in the VSAM indexed data set (or base cluster) and extracts the data needed to build the alternate index. Alternatively, you can use the runtime option AIXBLD to build the alternate index at run time. However, this option might adversely affect performance. Example: entries for alternate indexes
RELATED TASKS
199
DD DD DD
The base cluster name is clustername. The name of the first alternate index path is path1. The name of the second alternate index path is path2. The ddname or environment variable name for the base cluster is specified with the ASSIGN clause. Passwords immediately follow their indexes. The key EM-PHONE relates to the first alternate index. The key EM-CITY relates to the second alternate index.
RELATED TASKS
200
In either case, dsname must be the same as the name used in the access method services DEFINE CLUSTER or DEFINE PATH command. DISP must be OLD or SHR because the data set is already cataloged. If you specify MOD when using JCL, the data set is treated as OLD. AMP is a VSAM JCL parameter that supplements the information that the program supplies about the data set. AMP takes effect when your program opens the VSAM file. Any information that you set through the AMP parameter takes precedence over the information that is in the catalog or that the program supplies. The AMP parameter is required only under the following circumstances: v You use a dummy VSAM data set. For example,
//ddname DD //ddname // DD DUMMY,AMP=AMORG DSN=VSAM.dsname,DISP=SHR, AMP=('BUFNI=4,BUFND=8')
You cannot specify AMP if you allocate a VSAM data set with an environment variable. For a VSAM base cluster, specify the same system-name (ddname or environment variable name) that you specify in the ASSIGN clause after the SELECT clause. When you use alternate indexes in your COBOL program, you must specify not only a system-name (using a DD statement or environment variable) for the base cluster, but also a system-name for each alternate index path. No language mechanism exists to explicitly declare system-names for alternate index paths within the program. Therefore, you must adhere to the following guidelines for forming the system-name (ddname or environment variable name) for each alternate index path: v Concatenate the base cluster name with an integer. v Begin with 1 for the path associated with the first alternate record defined for the file in your program (ALTERNATE RECORD KEY clause of the FILE-CONTROL paragraph). v Increment by 1 for the path associated with each successive alternate record definition for that file. For example, if the system-name of a base cluster is ABCD, the system-name for the first alternate index path defined for the file in your program is ABCD1, the system-name for the second alternate index path is ABCD2, and so on. If the length of the base cluster system-name together with the sequence number exceeds eight characters, the base cluster portion of the system-name is truncated on the right to reduce the concatenated result to eight characters. For example, if the system-name of a base cluster is ABCDEFGH, the system name of the first alternate index path is ABCDEFG1, the tenth is ABCDEF10, and so on.
RELATED TASKS
201
Preventing update problems with VSAM files in RLS mode Handling errors in VSAM files in RLS mode on page 203
RELATED REFERENCES
202
203
Table 33. Methods for improving VSAM performance (continued) Aspect of VSAM What you can do Buffering For sequential access, request more data buffers; for random access, request more index buffers. Specify both BUFND and BUFNI when ACCESS IS DYNAMIC. Avoid coding additional buffers unless your application will run interactively; then code buffers only when response-time problems arise that might be caused by delays in input and output. Loading records, using access methods services Use the access methods service The REPRO command can update an indexed data set as fast or faster than REPRO command when: v The target indexed data set any COBOL program under these conditions. already contains records. v The input sequential data set contains records to be updated or inserted into the indexed data set. If you use a COBOL program to load the file, use OPEN OUTPUT and ACCESS SEQUENTIAL. File access modes For best performance, access records sequentially. Dynamic access is less efficient than sequential access, but more efficient than random access. Random access results in increased EXCPs because VSAM must access the index for each request. This method compresses the key best. Rationale and comments The default is one index (BUFNI) and two data buffers (BUFND).
Key design
Design the key in the records so that the high-order portion is relatively constant and the low-order portion changes often.
Avoid using multiple alternate Updates must be applied through the indexes. primary paths and are reflected through multiple alternate paths, perhaps slowing performance. Use VSAM fixed-length relative data sets rather than VSAM variable-length relative data sets. Although not as space efficient, VSAM fixed-length relative data sets are more runtime efficient than VSAM variable-length relative data sets.
204
Table 33. Methods for improving VSAM performance (continued) Aspect of VSAM What you can do Control interval sizes (CISZ) Provide your system programmer with information about the data access and future growth of your VSAM data sets. From this information, your system programmer can determine the best control interval size (CISZ) and FREESPACE size (FSPC). Rationale and comments VSAM calculates CISZ to best fit the direct-access storage device (DASD) usage algorithm, which might not, however, be efficient for your application.
An average CISZ of 4K is suitable for most applications. A smaller CISZ means faster retrieval for random processing at the expense of inserts (that is, more CISZ splits and therefore Choose proper values for CISZ more space in the data set). A larger and FSPC to minimize control CISZ results in the transfer of more data across the channel for each READ. This is area (CA) splits. You can more efficient for sequential processing, diagnose the current number similar to a large OS BLKSIZE. of CA splits by issuing the LISTCAT ALL command on the Many control area (CA) splits are cluster, and then compress unfavorable for VSAM performance. (using EXPORT, IMPORT, or The FREESPACE value can affect CA REPRO) the cluster to omit all splits, depending on how the file is CA splits periodically. used.
RELATED TASKS
Specifying access modes for VSAM files on page 185 z/OS DFSMS: Using Data Sets (Building a resource pool, Selecting the optimal percentage of free space)
RELATED REFERENCES
205
206
Defining line-sequential files and records in COBOL Describing the structure of a line-sequential file on page 208 Coding input-output statements for line-sequential files on page 209 Handling errors in line-sequential files on page 212 Defining and allocating line-sequential files on page 209 UNIX System Services Users Guide
RELATED REFERENCES
The ASSIGN assignment-name clause must not include an organization field (S- or AS-) before the external name. The ACCESS phrase and the FILE STATUS clause are optional.
RELATED TASKS
207
Coding input-output statements for line-sequential files on page 209 Defining and allocating line-sequential files on page 209
RELATED REFERENCES
The newline character is treated as a record delimiter. The other control characters are treated as data and are part of the record.
RELATED TASKS
208
FILE SECTION. FD COMMUTER-FILE RECORD VARYING FROM 16 TO 80 CHARACTERS. 01 COMMUTER-RECORD. 05 COMMUTER-NUMBER PIC X(16). 05 COMMUTER-DESCRIPTION PIC X(64).
If you code the same fixed size and no OCCURS DEPENDING ON clause for any level-01 record description entries associated with the file, that fixed size is the logical record length. However, because blanks at the end of a record are not written to the file, the physical records might be of varying lengths.
RELATED TASKS
Defining line-sequential files and records in COBOL on page 207 Coding input-output statements for line-sequential files Defining and allocating line-sequential files
RELATED REFERENCES
Allocating files on page 149 Defining line-sequential files and records in COBOL on page 207
RELATED REFERENCES
209
OPEN
To initiate the processing of a file. You can open a line-sequential file as INPUT, OUTPUT, or EXTEND. You cannot open a line-sequential file as I-O.
READ
To read a record from a file. With sequential processing, a program reads one record after another in the same order in which the records were entered when the file was created.
WRITE
To create a record in a file. A program writes new records to the end of the file.
CLOSE
RELATED TASKS
Defining line-sequential files and records in COBOL on page 207 Describing the structure of a line-sequential file on page 208 Opening line-sequential files Reading records from line-sequential files Adding records to line-sequential files on page 211 Closing line-sequential files on page 211 Handling errors in line-sequential files on page 212
RELATED REFERENCES
OPEN statement (Enterprise COBOL Language Reference) READ statement (Enterprise COBOL Language Reference) WRITE statement (Enterprise COBOL Language Reference) CLOSE statement (Enterprise COBOL Language Reference)
Reading records from line-sequential files Adding records to line-sequential files on page 211 Closing line-sequential files on page 211 Defining and allocating line-sequential files on page 209
RELATED REFERENCES
OPEN statement (Enterprise COBOL Language Reference) CLOSE statement (Enterprise COBOL Language Reference)
210
Characters in the file record are read one at a time into the record area until one of the following conditions occurs: v The record delimiter (the EBCDIC newline character) is encountered. The delimiter is discarded and the remainder of the record area is filled with spaces. (Record area is longer than the file record.) v The entire record area is filled with characters. If the next unread character is the record delimiter, it is discarded. The next READ reads from the first character of the next record. (Record area is the same length as the file record.) Otherwise the next unread character is the first character to be read by the next READ. (Record area is shorter than the file record.) v End-of-file is encountered. The remainder of the record area is filled with spaces. (Record area is longer than the file record.)
RELATED TASKS
Opening line-sequential files on page 210 Adding records to line-sequential files Closing line-sequential files Defining and allocating line-sequential files on page 209
RELATED REFERENCES
OPEN statement (Enterprise COBOL Language Reference) WRITE statement (Enterprise COBOL Language Reference)
Opening line-sequential files on page 210 Reading records from line-sequential files on page 210 Closing line-sequential files Defining and allocating line-sequential files on page 209
RELATED REFERENCES
OPEN statement (Enterprise COBOL Language Reference) WRITE statement (Enterprise COBOL Language Reference)
211
If you do not close a line-sequential file, the file is automatically closed for you under the following conditions: v When the run unit ends normally. v When the run unit ends abnormally, if the TRAP(ON) runtime option is set. v When Language Environment condition handling is completed and the application resumes in a routine other than where the condition occurred, open files defined in any COBOL programs in the run unit that might be called again and reentered are closed. You can change the location where the program resumes (after a condition is handled) by moving the resume cursor with the Language Environment CEEMRCR callable service or using HLL language constructs such as a C longjmp call. File status codes are set when these implicit CLOSE operations are performed, but EXCEPTION/ERROR declaratives are not invoked.
RELATED TASKS
Opening line-sequential files on page 210 Reading records from line-sequential files on page 210 Adding records to line-sequential files on page 211 Defining and allocating line-sequential files on page 209
RELATED REFERENCES
Coding input-output statements for line-sequential files on page 209 Handling errors in input and output operations on page 235
212
Describing the sort or merge file on page 214 Describing the input to sorting or merging on page 215 Describing the output from sorting or merging on page 217 Requesting the sort or merge on page 220 Determining whether the sort or merge was successful on page 224 Stopping a sort or merge operation prematurely on page 225
Copyright IBM Corp. 1991, 2009
213
Improving sort performance with FASTSRT on page 225 Controlling sort behavior on page 228 DFSORT Application Programming Guide
RELATED REFERENCES
CICS SORT application restrictions on page 232 SORT statement (Enterprise COBOL Language Reference) MERGE statement (Enterprise COBOL Language Reference)
SORT statement (Enterprise COBOL Language Reference) MERGE statement (Enterprise COBOL Language Reference)
Sort-Work-1 is the name of the file in your program. Use this name to refer to the file.
214
2. Describe the sort file in an SD entry in the FILE SECTION of the DATA DIVISION. Every SD entry must contain a record description. For example:
DATA DIVISION. FILE SECTION. SD Sort-Work-1 RECORD CONTAINS 100 CHARACTERS. 01 SORT-WORK-1-AREA. 05 SORT-KEY-1 PIC X(10). 05 SORT-KEY-2 PIC X(10). 05 FILLER PIC X(80).
The file described in an SD entry is the working file used for a sort or merge operation. You cannot perform any input or output operations on this file and you do not need to provide a ddname definition for it.
RELATED REFERENCES
Input-File is the name of the file in your program. Use this name to refer to the file. 2. Describe the input file (or files when merging) in an FD entry in the FILE SECTION of the DATA DIVISION. For example:
DATA DIVISION. FILE SECTION. FD Input-File LABEL RECORDS ARE STANDARD BLOCK CONTAINS 0 CHARACTERS RECORDING MODE IS F RECORD CONTAINS 100 CHARACTERS. 01 Input-Record PIC X(100).
RELATED TASKS
Coding the input procedure on page 216 Requesting the sort or merge on page 220
RELATED REFERENCES
215
* * Assign name for a working file is treated as documentation. * Select Sort-Work-1 Assign To SortFile. Select Sort-Work-2 Assign To SortFile. Select Input-File Assign To InFile. . . . Data Division. File Section. SD Sort-Work-1 Record Contains 100 Characters. 01 Sort-Work-1-Area. 05 Sort-Key-1 Pic X(10). 05 Sort-Key-2 Pic X(10). 05 Filler Pic X(80). SD Sort-Work-2 Record Contains 30 Characters. 01 Sort-Work-2-Area. 05 Sort-Key Pic X(5). 05 Filler Pic X(25). FD Input-File Label Records Are Standard Block Contains 0 Characters Recording Mode is F Record Contains 100 Characters. 01 Input-Record Pic X(100). . . . Working-Storage Section. 01 EOS-Sw Pic X. 01 Filler. 05 Table-Entry Occurs 100 Times Indexed By X1 Pic X(30). . . .
RELATED TASKS
216
To transfer records to the sort program, all input procedures must contain at least one RELEASE or RELEASE FROM statement. To release A from X, for example, you can code:
MOVE X TO A. RELEASE A.
The following table compares the RELEASE and RELEASE FROM statements.
RELEASE MOVE EXT-RECORD TO SORT-EXT-RECORD PERFORM RELEASE-SORT-RECORD . . . RELEASE-SORT-RECORD. RELEASE SORT-RECORD RELEASE FROM PERFORM RELEASE-SORT-RECORD . . . RELEASE-SORT-RECORD. RELEASE SORT-RECORD FROM SORT-EXT-RECORD
RELATED REFERENCES
Restrictions on input and output procedures on page 219 RELEASE statement (Enterprise COBOL Language Reference)
Output-File is the name of the file in your program. Use this name to refer to the file. 2. Describe the output file (or files when merging) in an FD entry in the FILE SECTION of the DATA DIVISION. For example:
DATA DIVISION. FILE SECTION. FD Output-File LABEL RECORDS ARE STANDARD BLOCK CONTAINS 0 CHARACTERS RECORDING MODE IS F RECORD CONTAINS 100 CHARACTERS. 01 Output-Record PIC X(100).
RELATED TASKS
Coding the output procedure on page 218 Requesting the sort or merge on page 220
RELATED REFERENCES
217
Restrictions on input and output procedures on page 219 RETURN statement (Enterprise COBOL Language Reference)
218
PROCEDURE DIVISION. A-CONTROL SECTION. SORT OUR-FILE ON ASCENDING KEY SORT-KEY INPUT PROCEDURE IS B-INPUT OUTPUT PROCEDURE IS C-OUTPUT. . . . B-INPUT SECTION. MOVE . . .. . .. TO WS-SORT-REC. RELEASE OUR-SORT-REC FROM WS-SORT-REC. . . . C-OUTPUT SECTION. DISPLAY 'STARTING READS OF SORTED RECORDS: '. RETURN OUR-FILE AT END SET NO-MORE-SORT-RECORDS TO TRUE. PERFORM WITH TEST BEFORE UNTIL NO-MORE-SORT-RECORDS IF SORT-RETURN = 0 THEN DISPLAY 'OUR-SORT-REC = ' OUR-SORT-REC RETURN OUR-FILE AT END SET NO-MORE-SORT-RECORDS TO TRUE END-IF END-PERFORM.
Coding the input procedure on page 216 Coding the output procedure on page 218 Language Environment Programming Guide (Preparing to link-edit and run)
219
Sort or merge work Define a minimum of three data sets: SORTWK01, SORTWK02, SORTWK03, . . ., SORTWKnn (where nn is 99 or less). These data sets cannot be in the HFS. SYSOUT Define for sort diagnostic messages, unless you change the data-set name. (Change the name using either the MSGDDN keyword of the OPTION control statement in the SORT-CONTROL data set, or using the SORT-MESSAGE special register.) SORTCKPT Define if the sort or merge is to take checkpoints. Input and output Define input and output data sets, if any. SORTLIB (DFSORT library) Define the library that contains the sort modules, for example, SYS1.SORTLIB.
RELATED TASKS
Controlling sort behavior on page 228 Using checkpoint/restart with DFSORT on page 231
Changing DFSORT defaults with control statements on page 229 Controlling sort behavior on page 228
220
For SORT . . . USING or MERGE . . . USING, the compiler generates an input procedure to open the file (files), read the records, release the records to the sort or merge program, and close the file (files). The file (files) must not be open when the SORT or MERGE statement begins execution. For SORT . . . GIVING or MERGE . . . GIVING, the compiler generates an output procedure to open the file, return the records, write the records, and close the file. The file must not be open when the SORT or MERGE statement begins execution. The USING or GIVING files in a SORT or MERGE statement can be sequential files residing in the HFS. Example: describing sort and input files for SORT on page 215 If you want an input procedure to be performed on the sort records before they are sorted, use SORT . . . INPUT PROCEDURE. If you want an output procedure to be performed on the sorted records, use SORT . . . OUTPUT PROCEDURE. For example:
SORT Sort-Work-1 ON ASCENDING KEY Sort-Key-1 INPUT PROCEDURE EditInputRecords OUTPUT PROCEDURE FormatData.
Example: sorting with input and output procedures on page 222 Restriction: You cannot use an input procedure with the MERGE statement. The source of input to the merge operation must be a collection of already sorted files. However, if you want an output procedure to be performed on the merged records, use MERGE . . . OUTPUT PROCEDURE. For example:
MERGE Merge-Work ON ASCENDING KEY Merge-Key USING Input-File-1 Input-File-2 Input-File-3 OUTPUT PROCEDURE ProcessOutput.
In the FILE SECTION, you must define Merge-Work in an SD entry, and the input files in FD entries.
RELATED TASKS
SORT statement (Enterprise COBOL Language Reference) MERGE statement (Enterprise COBOL Language Reference)
221
There is no maximum number of keys, but the keys must be located in the first 4092 bytes of the record description. The total length of the keys cannot exceed 4092 bytes unless the EQUALS keyword is coded in the DFSORT OPTION control statement, in which case the total length of the keys must not exceed 4088 bytes. Restriction: A key cannot be variably located. 2. In the SORT or MERGE statement, specify the key fields to be used for sequencing by coding the ASCENDING or DESCENDING KEY phrase, or both. When you code more than one key, some can be ascending, and some descending. Specify the names of the keys in decreasing order of significance. The leftmost key is the primary key. The next key is the secondary key, and so on. SORT and MERGE keys can be of class alphabetic, alphanumeric, national, or numeric (but not numeric of USAGE NATIONAL). If it has USAGE NATIONAL, a key can be of category national or can be a national-edited or numeric-edited data item. A key cannot be a national decimal data item or a national floating-point data item. The collation order for national keys is determined by the binary order of the keys. If you specify a national data item as a key, any COLLATING SEQUENCE phrase in the SORT or MERGE statement does not apply to that key. You can mix SORT and MERGE statements in the same COBOL program. A program can perform any number of sort or merge operations. However, one operation must end before another can begin.
RELATED REFERENCES
DFSORT Application Programming Guide (SORT control statement) SORT statement (Enterprise COBOL Language Reference) MERGE statement (Enterprise COBOL Language Reference)
222
10 10 10
. . . PROCEDURE DIVISION. . . . SORT SORT-FILE ON ASCENDING KEY SORT-GRID-LOCATION SORT-SHIFT INPUT PROCEDURE 600-SORT3-INPUT OUTPUT PROCEDURE 700-SORT3-OUTPUT. . . . 600-SORT3-INPUT. PERFORM VARYING TAB-INDX FROM 1 BY 1 UNTIL TAB-INDX > 10 RELEASE SORT-RECORD FROM TAB-ENTRY(TAB-INDX) END-PERFORM. . . . 700-SORT3-OUTPUT. PERFORM VARYING TAB-INDX FROM 1 BY 1 UNTIL TAB-INDX > 10 RETURN SORT-FILE INTO TAB-ENTRY(TAB-INDX) AT END DISPLAY 'Out Of Records In SORT File' END-RETURN END-PERFORM.
RELATED TASKS
Specifying the collating sequence on page 9 Setting sort or merge criteria on page 221
RELATED REFERENCES
OBJECT-COMPUTER paragraph (Enterprise COBOL Language Reference) SORT statement (Enterprise COBOL Language Reference) Classes and categories of data (Enterprise COBOL Language Reference)
223
To sort on a windowed date field, use the DATE FORMAT clause to define a windowed date field; then use the field as the sort key. DFSORT will use the same century window as that used by the compilation unit. Specify the century window with the YEARWINDOW compiler option. DFSORT supports year-last windowed date fields, although the compiler itself does not provide automatic windowing for year-last windowed date fields in statements other than MERGE or SORT.
RELATED CONCEPTS
YEARWINDOW on page 360 DATE FORMAT clause (Enterprise COBOL Language Reference) DFSORT Application Programming Guide (OPTION control statement: Y2PAST)
224
If you do not reference SORT-RETURN anywhere in your program, the COBOL run time tests the completion code. If it is 16, COBOL issues a runtime diagnostic message. By default, DFSORT diagnostic messages are sent to the SYSOUT data set. If you want to change this default, use the MSGDDN parameter of the DFSORT OPTION control card or use the SORT-MESSAGE special register. If you test SORT-RETURN for one or more (but not necessarily all) SORT or MERGE statements, the COBOL run time does not check the completion code.
RELATED TASKS
Checking for sort errors with NOFASTSRT on page 227 Controlling sort behavior on page 228
RELATED REFERENCES
225
FASTSRT requirements for JCL FASTSRT requirements for sort input and output files
SKIPREC SUM Many DFSORT functions perform the same operations that are common in input or output procedures. Code the appropriate DFSORT control statements instead, and place them either in the IGZSRTCD or SORTCNTL data set. v Do not code the LINAGE clause for the output FD entry. v Do not code any INPUT declarative (for input files), OUTPUT declarative (for output files), or file-specific declaratives (for either input or output files) to apply to any FDs used in the sort. v Do not use a variable relative file as the input or output file. v Do not use a line-sequential file as the input or output file. v For either an input or an output file, the record descriptions of the SD and FD entry must define the same format (fixed or variable), and the largest records of the SD and FD entry must define the same record length. If you code a RELATIVE KEY clause for an output file, it will not be set by the sort. Performance tip: If you block your input and output records, the sort performance could be significantly improved.
226
QSAM requirements
v QSAM files must have a record format of fixed, variable, or spanned. v A QSAM input file can be empty. v To use the same QSAM file for both input and output, you must describe the file using two different DD statements. For example, in the FILE-CONTROL SECTION you might code this:
SELECT FILE-IN ASSIGN INPUTF. SELECT FILE-OUT ASSIGN OUTPUTF.
In the DATA DIVISION, you would have an FD entry for both FILE-IN and FILE-OUT, where FILE-IN and FILE-OUT are identical except for their names. In the PROCEDURE DIVISION, your SORT statement could look like this:
SORT file-name ASCENDING KEY data-name-1 USING FILE-IN GIVING FILE-OUT
Then in your JCL, assuming that data set INOUT has been cataloged, you would code:
//INPUTF DD DSN=INOUT,DISP=SHR //OUTPUTF DD DSN=INOUT,DISP=SHR
On the other hand, if you code the same file-name in the USING and GIVING phrases, or assign the input and output files the same ddname, then the file can be accepted for FASTSRT either for input or output, but not both. If no other conditions disqualify the file from being eligible for FASTSRT on input, then the file will be accepted for FASTSRT on input, but not on output. If the file was found to be ineligible for FASTSRT on input, it might be eligible for FASTSRT on output. A QSAM file that qualifies for FASTSRT can be accessed by the COBOL program while the SORT statement is being performed. For example, if the file is used for FASTSRT on input, you can access it in an output procedure; if it is used for FASTSRT on output, you can access it in an input procedure.
VSAM requirements
v A VSAM input file must not be empty. v VSAM files cannot be password-protected. v You cannot name the same VSAM file in both the USING and GIVING phrases. v A VSAM file that qualifies for FASTSRT cannot be accessed by the COBOL program until the SORT statement processing is completed. For example, if the file qualifies for FASTSRT on input, you cannot access it in an output procedure and vice versa. (If you do so, OPEN fails.)
RELATED TASKS
227
Table 34. Methods for checking for sort errors with NOFASTSRT FILE STATUS clause? No Yes ERROR declarative? No No Then do: No special coding. Any failure during the sort process causes the program to end abnormally. Test the SORT-RETURN special register after the SORT statement, and test the file status key. (Not recommended if you want complete file-status checking, because the file status code is set but COBOL cannot check it.) In the ERROR declarative, set the SORT-RETURN special register to 16 to stop the sort process and indicate that it was not successful. Test the SORT-RETURN special register after the SORT statement.
Maybe
Yes
RELATED TASKS
Determining whether the sort or merge was successful on page 224 Using file status keys on page 239 Coding ERROR declaratives on page 238 Stopping a sort or merge operation prematurely on page 225
To set or test Amount of main storage to be reserved Amount of main storage to be used Modal length of records in a file with variable-length records
SORT-CORE-SIZE special register OPTION (keyword RESINV) SORT-CORE-SIZE special register OPTION (keywords MAINSIZE or MAINSIZE=MAX) SORT-MODE-SIZE special register SMS=nnnnn
Name of sort control statement SORT-CONTROL special register data set (default IGZSRTCD) Name of sort message file (default SYSOUT) Number of sort records Sort completion code SORT-MESSAGE special register
SORT-FILE-SIZE special register OPTION (keyword FILSZ) SORT-RETURN special register None
228
Table 35. Methods for controlling sort behavior (continued) Use this special register or compiler option Or this control statement (and keyword if applicable) OPTION (keyword Y2PAST) SORT (keyword FORMAT=Y2x)
To set or test
Century window for sorting or YEARWINDOW c