Showing posts with label Cobol. Show all posts
Showing posts with label Cobol. Show all posts

COBOL FUNCTIONS




COBOL functions offer a new way to manipulate data that was not available in older versions. We will look at a series of COBOL functions in this handout. Functions are available for: mathematics, character manipulation, date/time and statistics and financial work. They can be used throughout the Procedure Division. Functions are elementary data items that will return a numeric (whole or decimal) or an alphanumeric. A function cannot be used as a receiving operand. When using a function, you must always use the word FUNCTION followed by the specific function you are using following by any required or optional arguments.
We are going to look at some common function use in this handout. Please check HELP for more FUNCTIONS.
UPPER-CASE & LOWER-CASE FUNCTIONS:
If you want to compare two fields and you are not sure of what case the fields contain, you can look at the upper case version of both fields or the lower case version of both fields so that you are comparing like configurations.
IF FUNCTION UPPER-CASE(FLD1) = FUNCTION UPPER-CASE(FLD2)
>     PERFORM B-300-MATCH-ROUT.
IF FUNCTION LOWER-CASE(FLD1) = FUNCTION LOWER-CASE(FLD2)
    PERFORM B-300-MATCH-ROUT.
If you are concerned that a user might enter data incorrectly at the screen, you can covert it using this function.
MOVE FUNCTION UPPER-CASE(STATE-IN) TO STATE-FOR-FILE.
LENGTH FUNCTION:
The length function gets the length of non-numeric fields so that you can find out the length of the field.
COMPUTE LEN1 = FUNCTION LENGTH(FLD-IN).

REVERSE FUNCTION:
If you need to reverse the data in a field you can use the REVERSE function. This means if the data was entered as XYZ, the new field will now be ZYX.
MOVE FUNCTION REVERSE(FLDA) TO FLDB.
SUM, MAX, MIN FUNCTION:
Will get the sum, max or min of the arguments in parenthesis.
COMPUTE TOTAL-WS = FUNCTION SUM(FLDA FLDB FLDC FLDD).
COMPUTE MAX-WS = FUNCTION MAX(FLDA FLDB FLDC FLDD).
COMPUTE MIN-WS = FUNCTION MIN(FLDA FLDB FLDC FLDD).
CONVERT TO NUMERIC FUNCTION:
This will convert an edited field to a numeric for use in a calculation. ANS-WS will contain 2367886 with an assumed decimal point between the two 8s. Its PIC would be S9(5)V99. Note, this should be done as a mathematical conversion prior to a MOVE statement, it should not be done in the MOVE etc.
COMPUTE ANS-WS = FUNCTION NUMVAL-C("$23,678.86 CR").
If you just want to convert an alphanumeric field to numeric, you would use NUMVAL.
COMPUTE ANS-WS = FUNCTION NUMVAL(FLDX).
If you had a table of numeric data and you wanted to sum that data or find the maximum value in that data, you can do it with the function and instead of using a subscript to move through the table, you can simply code ALL.
COMPUTE MAX-AMT-WS = FUNCTION MAX(NUM-TABLE(ALL)).
COMPUTE TOT-AMT-WS = FUNCTION SUM(NUM-TABLE(ALL)).
INTEGER FUNCTIONS:
This will take the integer and assign it to INT-ANS-WS. Obviously you could have a field name in the parenthesis. If the decimal is greater than 5, it will round. If you want to truncate, use INTEGER-PART.
COMPUTE INT-ANS-WS = FUNCTION INTEGER(2123.456). returns 2123
COMPUTE INT-ANS-WS = FUNCTION INTEGER(45.7). returns 46
COMPUTE INT-ANS-WS = FUNCTION INTEGER-PART(123.456). returns 123
COMPUTE INT-ANS-WS = FUNCTION INTEGER-PART(63.78). returns 63
REMAINDER VALUE FUNCTION:
COMPUTE REM-WS = FUNCTION REM(42,5). returns 2 since 42/5 has a remainder of 2.
DATE, TIME FUNCTIONS:
You need to note that date functions use the Gregorian calendar which means that January 1, 1601 is day 1 of the calendar.
FUNCTION CURRENT-DATE will get the current data and return it as a field that takes 21 characters - PIC X(21). The format is yyyymmddhhmmsstt+hhmm. Note that tt is hundredths of seconds and the hhmm at the end is the relation to Greenwich Mean Time.
FUNCTION DATE-OF-INTEGER(Gregorian date in days) will return yyyymmdd while FUNCTION DAY-OF-INTEGER(Gregorian date in days) will return yyyyddd - the days here are in Julian ranging from 1 to 366.
FUNCTION INTEGER-OF-DATE(date as yyyymmdd) will return the Gregorian date.
FUNCTION DATE-TO-YYYYMMDD(date,n) will convert a date to a four digit year. The date is given in YYMMDD and the n is an integer that defines the ending year of the relevant 100 year interval in relation to the current date. The way I understand this is that since we are in 2000, making n 15 would mean the date is in the hundred year range ending in 2015. You should read up on this if you ever use it!!! FUNCTION YEAR-TO-YYYY works the same way, but just with years.

File Status Key in Cobol


Introduction
This document provides a summary of the two-byte File-Status-Key (sometimes referred to as file return code or file status code). For the COBOL environment if the FILE STATUS clause is specified in the FILE-CONTROL entry, a value is placed in the specified status key (the 2-character data item named in the FILE STATUS clause) during execution of any request on that file. The value indicates the status of that request. The value is placed in the status key before execution of any EXCEPTION/ERROR declarative or INVALID KEY/AT END phrase associated with the request.

The following tables contain file-status-key content that may be set by Micro Focus COBOL (Mainframe Express or Net Express) or an IBM Mainframe (MVS or OS/390).

File Status Key Overview

The ANS/85 standard provides for a two-byte file-status-key. The first character of the file-status-key is known as status-key-1. The second character is known as status-key-2. The combinations of possible values and their meanings are shown below with some comments where vendor additions or extensions may occur.

Note: If status-key-1 is a nine (9) then status key 2 is "implementor-defined". Many vendors take advantage of the x"00' to x'FF' (or 0-255) binary value. Therefore, the file status key may not always be a numeric value.
Status
Key-1
 
Condition Status
Key-2
 
Description




0Successful
Completion
0No further information
  2The input-output statement was successfully executed, but a duplicate key was detected. For a READ statement the key value for the current key of reference was equal to the value of the same key in the next record within the current key of reference. For a REWRITE or WRITE statement, the record just written created a duplicate key value for at least one alternate record key for which duplicates are allowed.
  4A READ statement was successfully executed, but the length of the record being processed did not conform to the fixed file attributes for that file.
  5An OPEN statement is successfully executed but the referenced optional file is not present at the time the OPEN statement is executed. If the open mode is I-O, the file has been created. Under MVS, the file has been created if the open mode is I-O or EXTEND. This does not apply to VSAM sequential files. File status 0 is returned.
  7For a CLOSE statement with the NO REWIND, REEL/UNIT, or FOR REMOVAL phrase or for an OPEN statement with the NO REWIND phrase, the referenced file was on a non-reel/unit medium.




1At END0A sequential READ statement was attempted and no next logical record existed in the file because the end of the file had been reached, or the first READ was attempted on an optional input file that was not present.
  4A sequential READ statement was attempted for a relative file and the number of significant digits in the relative record number was larger than the size of the relative key data item described for the file.




2Invalid key1A sequence error exists for a sequentially accessed indexed file. The prime record key value has been changed by the program between the successful execution of a READ statement and the execution of the next REWRITE statement for that file, or the ascending requirements for successive record key values were violated.
  2An attempt was made to write a record that would create a duplicate key in a relative file; or an attempt was made to write or rewrite a record that would create a duplicate prime record key or a duplicate alternate record key without the DUPLICATES phrase in an indexed file. This key value applies to an indexed file in which the alternate key has been declared 'UNIQUE'.
  3An attempt was made to randomly access a record that does not exist in the file, or a START or random READ statement was attempted on an optional input file that was not present.
  4An attempt was made to write beyond the externally defined boundaries of a relative or indexed file. Or, a sequential WRITE statement was attempted for a relative file and the number of significant digits in the relative record number was larger than the size of the relative key data item described for the file.

Cobol Accept

COBOL Accept

>ACCEPT Statement in COBOL
The ACCEPT statement in COBOL can be used for low volume input from a specified device. The ACCEPT statement in COBOL has three general formats.
>COBOL ACCEPT Verb
The COBOL ACCEPT and DISPLAY verbs are used to read from the keyboard and write to the screen. Simple examples using the COBOL ACCEPT and DISPLAY verbs are shown.
>COBOL ACCEPT Statement
The COBOL ACCEPT statement transfers data into the specified identifier. There is no editing or error checking of the incoming data. The COBOL ACCEPT statement has seven formats.
>COBOL ACCEPT Statement
The COBOL ACCEPT statement is described as it is presented in the Compaq COBOL User Manual. The COBOL ACCEPT statement (Formats 3, 4, and 5) are described.

Cobol Data types


Data types

Standard COBOL provides the following data types:
Data typeSample declarationNotes
CharacterPIC X(20)
PIC A(4)9(5)X(7)
Alphanumeric and alphabetic-only
Single-byte character set (SBCS)
Edited characterPIC X99BAXXFormatted and inserted characters
Numeric fixed-point binaryPIC S999V99
[USAGE] COMPUTATIONAL

or
BINARY
Binary 16, 32, or 64 bits (2, 4, or 8 bytes)
Signed or unsigned. Conforming compilers limit the maximum value of variables based on the picture clause and not the number of bits reserved for storage.
Numeric fixed-point packed decimalPIC S999V99
PACKED-DECIMAL
1 to 18 decimal digits (1 to 10 bytes)
Signed or unsigned
Numeric fixed-point zoned decimalPIC S999V99
[USAGE DISPLAY]
1 to 18 decimal digits (1 to 18 bytes)
Signed or unsigned
Leading or trailing sign, overpunch or separate
Numeric floating-pointPIC S9V999ES99Binary floating-point
Edited numericPIC +Z,ZZ9.99
PIC $***,**9.99CR
Formatted characters and digits
Group (record)01 CUST-NAME.
  05 CUST-LAST PIC X(20).
  05 CUST-FIRST PIC X(20).
Aggregated elements
Table (array)OCCURS 12 TIMESFixed-size array, row-major order
Up to 7 dimensions
Variable-length tableOCCURS 0 to 12 TIMES
DEPENDING ON CUST-COUNT
Variable-sized array, row-major order
Up to 7 dimensions
Renames (variant or union data)66 RAW-RECORD
  RENAMES CUST-RECORD
Character data overlaying other variables
Condition name88 IS-RETIRED-AGE
  VALUES 65 THRU 150
Boolean value
dependent upon another variable
Array index[USAGE] INDEXArray subscript
Most vendors provide additional types, such as:
Data typeSample declarationNotes
Numeric floating-point
single precision
PIC S9V9999999ES99
[USAGE] COMPUTATIONAL-1
Binary floating-point (32 bits, 7+ digits)
(IBM extension)
Numeric floating-point
double precision
PIC S9V999ES99
[USAGE] COMPUTATIONAL-2
Binary floating-point (64 bits, 16+ digits)
(IBM extension)
Numeric fixed-point packed decimalPIC S9V999
[USAGE] COMPUTATIONAL-3
same as PACKED DECIMAL
(IBM extension)
Numeric fixed-point binaryPIC S999V99
[USAGE] COMPUTATIONAL-4
same as COMPUTATIONAL or BINARY
(IBM extension)
Numeric fixed-point binary
(native binary)
PIC S999V99
[USAGE] COMPUTATIONAL-5
Binary 16, 32, or 64 bits (2, 4, or 8 bytes)
Signed or unsigned. The maximum value of variables based on the number of bits reserved for storage and not on the picture clause.
(IBM extension)
Numeric fixed-point binary
in native byte order
PIC S999V99
[USAGE] COMPUTATIONAL-4
Binary 16, 32, or 64 bits (2, 4, or 8 bytes)
Signed or unsigned
Numeric fixed-point binary
in big-endian byte order
PIC S999V99
[USAGE] COMPUTATIONAL-5
Binary 16, 32, or 64 bits (2, 4, or 8 bytes)
Signed or unsigned
Wide characterPIC G(20)Alphanumeric
Double-byte character set (DBCS)
Edited wide characterPIC G99BGGGFormatted and inserted wide characters
Edited floating-pointPIC +9.9(6)E+99Formatted characters, decimal digits, and exponent
Data pointer[USAGE] POINTERData memory address
Code pointer[USAGE] PROCEDURE-POINTERCode memory address
Bit fieldPIC 1(n) [USAGE] COMPUTATIONAL-5n can be from 1 to 64, defining an n-bit integer
Signed or unsigned
Index[USAGE] INDEXBinary value corresponding to an occurrence of a table element
May be linked to a specific table using INDEXED BY