Tuesday, March 16, 2010

Datastage Routines for Packed Decimal & Signed Integer


In our project, we have a requirement to send data to Mainframe System from datastage. We were having issues creating packed decimal(comp-3) and signed integer.

To resolve this, we have written 2 routines to perform the following
  • Convert integer to Packed Decimal(Comp-3)
  • Convert integer to signed integer
We realized that we cannot directly use the ICONV function with MP tag since it only works for the positive integer.So we had to manipulate the last nibble to get the correct value.

If you are interested here's the complete code of the routines:

Routine 1: Integer to Packed Decimal
This Routine converts Integer to packed-decimal value
It takes 2 Arguments
  1. inIntValue -> Input integer value
  2. inLength -> Input Length of the unpacked decimal field
Routine Code:
$INCLUDE DSINCLUDE JOBCONTROL.H
*** Declaration ***
vIntValue=inIntValue
vUnpackLength=inLength
vPackLength=int(((vUnpackLength +1)/2)+.5)
vRevSign=0
vZero=Iconv(00,"MP")

If vIntValue >= 0 Then vRevSign=1; vIntValue=Neg(vIntValue)
Else vRevSign=0

vPackDeciValue=iconv(vIntValue,"MP")

*** Pad leading zeroes
A=0
Loop
vZero=vZero:Iconv(00,"MP")
A=A+1
until A=vPackLength
Repeat

vPackDeciValue=right(vZero:vPackDeciValue,vPackLength)
*vPackDeciValue=vZero:vPackDeciValue

*** Manipulating the last nibble based on the sign
vLastByte=right(vPackDeciValue,1)

Begin case
Case vRevSign=1
vLastByte=char(vLastByte - 1)
End Case

*** To output zero as positive **
If inIntValue=0 Then vLastByte=char(12)
outPackDeciValue=left(vPackDeciValue,vPackLength-1):vLastByte
Ans=outPackDeciValue


Routine 2: Integer to Signed Integer
This Routine converts Integer to Signed Integer value
It takes 2 Arguments
  1. inIntValue -> Input Integer value
  2. inLength -> Input Length of the field without the sign
Routine Code:
$INCLUDE DSINCLUDE JOBCONTROL.H
*** Declaration
vZero=0

*** Generate zeroes
A=0
Loop
vZero=vZero:0
A=A+1
until A=inLength
Repeat

If inIntValue>=0 Then vRevSign=1
Else vRevSign=0

vIntPositiveData = ABS(inIntValue)

*** Pad Zeroes
vIntPositiveData = Right(vZero:vIntPositiveData, inLength)

vIntData = LEFT(vIntPositiveData, inLength -1)
vLastByte = RIGHT(vIntPositiveData , 1)

*** Pack the sign into the last byte
Begin case
Case vRevSign=1
vLastByteHex = Char(192 + vLastByte)
Case vRevSign=0
vLastByteHex = Char(208 + vLastByte)
End Case

outSignedIntValue = EBCDIC(vIntData ):vLastByteHex
Ans = outSignedIntValue


No comments:

Post a Comment