Monday, April 20, 2015

COMP-3 variable in cobol. Calculate the number of bytes for COMP-3 variable.

COMP3 in cobol enables a computer to store 2 digits in each storage position, except for the rightmost position where the sign is stored. Each digit takes half a byte.

Point to remember:  Even if we specify 'S' or not in the variable declaration,  the sign is stored.

Suppose we move the digit 1265875 into a field 9(7). In display mode, cobol will occupy 7 bytes, ie, 7 storage positions.
If we use COMP-3 in the variable declaration,  it will take only 4 Bytes like below.


Had it been a negative number, then The rightmost digit would store 'D' in place of  C.

How many bytes does COMP-3 take ?
To calculate the byte-length of a comp-3 field, start with the total number of digits and divide by 2 giving a result (discarding the remainder if any), then add 1 to the result.
Note: For "normal" processing the maximum number of digits is 18 . If we use compiler option as the ARITH(EXTEND) , the compiler will allow us to extend it to 31 bytes

Just to share, if we use Odd number of digits for packed decimal, the processing becomes 5-20 % faster than what the speed would be if we use even number of digits!. (Source : Some Informative journal in comp-3 in google )

Friday, April 10, 2015

Create Dataset name dynamically using system date and time or timestamp - using EZACFSM1

Many times we face the scenario, where we are in need to create a file suffixed with today's date ,time ,ie  the system date and time. It can be done in various ways. In this post we will see how to use one utility EZACSFM1 to achieve the same. It is a good utility and accepts many parameters which can be used tactfully to achieve good results.

//SYSOUT DD SYSOUT=(,INTRDR)   <= submit the below job via internal reader
//                          DISP=(,CATLG,DELETE),
//                         SPACE=(CYL,(10,10),RLSE),
//                         DCB=(RECFM=FB,LRECL=10,BLKSIZE=0)
(Tested code. Runs fine)
We are submitting the IEFBR14 job to create the dataset  TEST.DATA.DMON.M04 via internal reader as mentioned above.
If you see, i have  kept 'D&DAY' and  'M&LMON' in blue. Just to keep our attention there. The utility EZACSFM1 substitutes the value of day and month values there which will be created dynamically. &day, &lmon are the parameters of the utility.
If we submit the job, two jobs will be submitted. The first job will submit the second job creating the dataset with IEFBR14 dynamically with the date and time parameters.

To display the values in spool in sysout, try this piece of code.We will come across more options.
<Tested code >
//SYSOUT    DD  SYSOUT=*          
//SYSIN     DD  *                
Some more parameters which EZACFSM1 takes. There are many more.

Test these small piece of code.M sure, you will find it very useful!!!

Thursday, April 9, 2015


When Defining the cobol variables, the first thing which we see around is the USAGE clause. It only signifies how the data item will be internally stored. What will be its size.? In most instances we would see that a variable has been declared as(just for example) PIC X(10), and there is no USAGE clause. When the usage clause is not specified ,then it is assumed by the compiler that the USAGE IS DISPLAY.This is the default.
The other kind of USAGE is COMPUTATIONAL. As the name suggests, this is used when we want to perform numeric operations on the data items. I found some of the points to be worth remembering. Will be highlighting them in Red in this post.

Can we use USAGE IS DISPLAY for NUMERIC items? 
For text items, or for numeric items that are not going to be used in a computation (like Phone Numbers,postal code), the USAGE IS DISPLAY will not be harmful; but for numeric items upon which some arithmetic operation will be performed,the default usage is definitely not the best way.
When we use numeric items (like pic 9(3) )with default usage, internally they will be stored in ASCII.
When performing operations on these items, computer need to convert them to binary.Once done, computer need to convert it back to ASCII. These conversions unnecessarily slows down performance. Hence COMP is preferable in these scenarios.

Further more, Computational Usage clause can be classified into the following fields depending on the numeric items we use in our program.
Internal floating-point (USAGE COMP-1, USAGE COMP-2)
Internal decimal (USAGE PACKED-DECIMAL or COMP-3)

The maximum length of a comp item is 18 digits.

Q.Why do we use COMP in usage clause. When to use COMP ?
We know computers are Binary machines. So binary numbers will be more favorite to computers for doing calculations.Hence always use COMP for the variables which will be involved in numeric calculations.
Also it is used  for defining internal counters, subscripts.

PIC Clause of a COMP data item will only contain 9 or S.
 01 WS-LEN          PIC S9(02)  COMP.

Now, when we declare the variables in comp, the compiler allocates spaces to them in words.which can be a half word(2 bytes),  Full word(4 bytes), Double word(8 bytes)

So if the PIC specified is 9(1) COMP or 9(2) COMP or 9(3) or  COMP OR 9(4) COMP, the space allocated by the compiler will be half word (2 bytes).

From PIC 9(5) COMP to PIC 9(9) COMP, the space allocated will be 4 bytes

For a PIC clause with more than 9 bytes, compiler will allocate  double word(8 bytes).

In short,
S9(01) - S9(04)      Half word.
S9(05) - S9(09)      Full word.
S9(10) -  S9(18)     Double word.

Q.When to use COMP-1 and COMP-2 ? 
When we want to use Fractional numbers, numbers having decimal digits, then COMP-1 is the choice.Thus USAGE IS COMP-1 is used to store floating point numbers(real numbers)
COMP-1 uses 4 bytes of storage.
Exactly in the same way, we need to use COMP-2 for extremely big numbers requiring high precision.
COMP-2 occupies 8 bytes of storage. Hence no need to use PIC clause since size is already pre defined.
Remember: No picture clause is needed when defining COMP-1 and COMP-2 items.

Read COMP3 Variables Here.