Thursday, July 28, 2016

Removing spaces from delimited record

When you create a delimited file in COBOL, you are stuck with the fact that the extra spaces in a field is there to stay. Removing those unwanted spaces and squeezing the data using COBOL is a tricky task, which am not sure if it will even be possible. 
This brings me to achieving this result using SORT. 

Below example shows a semicolon delimited file created by a COBOL reporting program. 
We have the first step adding Quotes to fields which is supposed to have spaces in them like name, address etc. Do make sure that when you create your COBOL layout, have an additional leading and trailing space for the quotes. Else the field might get truncated on addition of quotes.
The second step uses the quotes to not squeeze the valid spaces in a field.

//*****************************************************************
//STEP0001 EXEC PGM=SORT
//*****************************************************************
//*  
//SORTIN   DD *   
123   ;SAMUEL JACKSON     ; ALABAMA ST  ;23456 
1456  ;PETER SAMUEL       ; DO NOT MAIL ;
2343  ;HARRY POTTER       ; OHIO ST     ;23356 
/*           
//SORTOUT  DD DSN=&&TEMP,            
//            DISP=(,PASS,DELETE),           
//            UNIT=SYSDA,                    
//            SPACE=(CYL,(1,1),RLSE),         
//            DCB=(RECFM=FB,LRECL=50,BLKSIZE=0)       
//SYSIN    DD *       
  OPTION COPY             
  OUTREC FIELDS=(1,7,
                 8,19,JFY=(SHIFT=LEFT,LEAD=C'"',TRAIL=C'"'),27,1,
                 28,13,JFY=(SHIFT=LEFT,LEAD=C'"',TRAIL=C'"'),41,6)
//SYSOUT   DD SYSOUT=* 
//SYSPRINT DD SYSOUT=*
//*         
//******************************************************************
//STEP0002 EXEC PGM=SORT 
//******************************************************************
//*      
//SORTIN   DD DISP=SHR,DSN=&&TEMP       
//SORTOUT  DD SYSOUT=*           
//SYSIN    DD *                    
  OPTION COPY         
  INREC FIELDS=(1,50,SQZ=(SHIFT=LEFT,PAIR=QUOTE))
  OUTREC FINDREP=(IN=C'"',OUT=C'')            
//SYSOUT   DD SYSOUT=*                     
//SYSPRINT DD SYSOUT=*                           
//*          


Output after Step 1
123   ;"SAMUEL JACKSON"   ;"ALABAMA ST" ;23456 
1456  ;"PETER SAMUEL"     ;"DO NOT MAIL";      
2343  ;"HARRY POTTER"     ;"OHIO ST"    ;23356 

Output after Step 2
123;SAMUEL JACKSON;ALABAMA ST;23456   
1456;PETER SAMUEL;DO NOT MAIL;        
2343;HARRY POTTER;OHIO ST;23356       


Note: All this effort to save space will help only if you convert this to a VB file
Use the below Sort step to do that


  OPTION COPY   
  OUTFIL FTOV,VLTRIM=C' '