Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CSV file output from COBOL

Status
Not open for further replies.

mattyp75

Programmer
Aug 28, 2002
16
GB
Does anyone know of any ways to create a csv file output from a COBOL prog? This is with varaiable length fields, depending on what the fields contain. So if a field contains 10 it will output 10 as 2 bytes, whereas if it contains 1000 it will output the 4 bytes. If anyone knows anyway to do this outside of COBOL that might be of use also. I anyone can help please let me know,
thanks,
Matt
 
Putting the value into a display field, then moving that to a string, and removing leading spaces in a function call, then using 'string ... "," delimited by size into .. pointer ..' has worked for me for ages, so if I can do that, you should have enough pointers to get it going.
Hint: use some extra performs to make it general.

HTH
TonHu
 
What type of cobol are you using? You need to define the output file as "line sequential" . Just use the string command or define a fixed field record with a comma character between fields. It will not hurt to have leading zeros in numeric fields. Make sure you have no commas in the fields themselves.
 
I'm using IBM COBOL FOR OS/390 & VM 2.2.1 (in an MVS mainframe environment).
TonHu, what version of COBOL is that for?
I can probably do this, just the way I'd be doing it would be byte by byte as I can't really see how my output format would look tho.
I've done the fixed field length with leading zeroes and I agree with you, I'm just thinking of file size in that circumstance as the dat ahas to be transfered after this and the smaller the file size the better.
I've not really used pointers all that much. Some example code would be helpful.
Thanks for your help,
Matt
 
Here is a program that creates CSV output.
Code:
 Data Division.
 File Section.
 FD  INPUT-FILE.
     *>
     *>   Tran Code    Type of Tran
     *>
     *>       1        New Stock
     *>       2        Sale or Return
     *>       3        Delete Stock
     *>       5        Receive Stock
     *>       6        Transfer Stock
     *>
 01  IT-RECORD.
     05  IT-TRANSACTION-CODE     Pic X(01).
         88  NEW-STOCK-TRANSACTION          Value '1'.
         88  SALES-TRANSACTION              Value '2'.
         88  DELETE-TRANSACTION             Value '3'.
         88  RECEIVE-TRANSACTION            Value '5'.
         88  TRANSFER-TRANSACTION           Value '6'.
     05  IT-DATE                 Pic 9(06).
     05  redefines IT-DATE.
         10  SALES-YEAR          Pic 9(02).
         10  SALES-MONTH         Pic 9(02).
         10  SALES-DAY           Pic 9(02).
     05  IT-MASTER-KEY.
         10  IT-STORE-NBR        Pic 9(02).
         10  IT-SKU              Pic X(02)    Comp-X.
         10  IT-WIDTH-CODE       Pic 9(02).
     05  IT-BASIC-DATA.
         10  IT-DEPT             Pic X(02).
         10  IT-COLOR-CODE       Pic X(02).
         10  IT-CLASS-CODE       Pic X(02).
         10  IT-SIZE-RUN-CODE    Pic X(02).
         10  IT-BRAND-CODE       Pic X(02).
         10  IT-COST             Pic S9(5)v99 Comp-3.
         10  IT-RETAIL           Pic S9(7)v99 Comp-3.
         10  IT-MARKDOWNS-WTD    Pic S9(5)v99 Comp-3.
         10  IT-MARKDOWNS-MTD    Pic S9(7)v99 Comp-3.
         10  IT-MARKDOWNS-YTD    Pic S9(7)v99 Comp-3.
         10  IT-STYLE            Pic X(10).
         10  IT-REORDER-FLAG     Pic X(01).
         10  IT-DESCRIPTION      Pic X(20).
         10  IT-AISLE-BIN.
             15  IT-AISLE        Pic 9(03).
             15  IT-BIN          Pic 9(05).
         10                      Pic X(06).
     05  IT-BUCKETS.
         10  IT-MODEL-SIZE-RUN.
             15  IT-MODEL        Pic 9(04) Comp occurs 20.
         10  IT-AGE-BUCKETS.
             15  IT-AGE          Pic 9(04) Comp occurs 20.
         10  IT-STOCK-BUCKETS.
             15  IT-SOH          Pic S9(4) Comp occurs 20.
         10  IT-WEEK-BUCKETS.
             15  IT-WTD-SALES    Pic S9(4) Comp occurs 20.
         10  IT-MONTH-BUCKETS.
             15  IT-MTD-SALES    Pic S9(4) Comp occurs 20.
         10  IT-YEAR-BUCKETS.
             15  IT-YTD-SALES    Pic S9(4) Comp occurs 20.
         10  IT-LY-BUCKETS.
             15  IT-LY-SALES     Pic S9(4) Comp occurs 20.
     05  redefines IT-BUCKETS.
         10  IT-CASHIER          Pic X(04).
         10  IT-SALES-UNITS      Pic S9(4) Comp.
         10  IT-UNITS-OVERSOLD   Pic 9(04) Comp.
         10  IT-SALES-RETAIL     Pic S9(7)v99 Comp-3.
         10  IT-SIZE-X.
             15  IT-SIZE         Pic 9(02)v9.
         10  IT-SALES-TIME.
             15  IT-SALES-HOUR   Pic X(02).
             15  IT-SALES-MINUTE Pic X(02).
         10  IT-SALES-PERSON     Pic X(05).
     05  redefines IT-BUCKETS.
         10  IT-TRANS-BUCKETS.
             15  IT-TRANS-QTY    Pic S9(4) Comp occurs 20.
         10  IT-TO-STORE-NBR     Pic 9(02).
         10  IT-TO-STORE-COST    Pic S9(5)v99 Comp-3.
         10  IT-TO-STORE-RETAIL  Pic S9(7)v99 Comp-3.
     05  IT-CASE-PACKS.
         10  IT-CASE-PACK        Pic X(02) occurs 10.

 FD  OUTPUT-FILE.
 01  OUTPUT-RECORD               Pic X(999).

 Working-Storage Section.
 01  WS-WORD                     Pic X(20) Value Space.
 01  redefines WS-WORD.
     05  WW-DATE                 Pic 99/99/99.
 01  redefines WS-WORD.
     05  WW-HOUR                 Pic X(02).
     05  WW-COLON                Pic X(01).
     05  WW-MINUTE               Pic X(02).
 01  redefines WS-WORD.
     05  WW-STORE                Pic X(02).
 01  redefines WS-WORD.
     05  WW-SKU                  Pic 9(05).
 01  redefines WS-WORD.
     05  WW-AMT                  Pic ZZZZ9.99.
 01  redefines WS-WORD.
     05  WW-QTY                  Pic ------9.

 01  TR-CODE-TABLE.
     05              Pic X(15) Value '1New Stock     '.
     05              Pic X(15) Value '2Sale or Return'.
     05              Pic X(15) Value '3Delete Stock  '.
     05              Pic X(15) Value '5Receive Stock '.
     05              Pic X(15) Value '6Transfer Stock'.
 01  redefines TR-CODE-TABLE.
 78  TC-OCC          Value Length of TR-CODE-TABLE / 15.
     05  TC-DATA occurs TC-OCC times Indexed by TCX.
         10  TC-CODE         Pic X(01).
         10  TC-NAME         Pic X(14).

 78  LINE-0  Value 'JackRabbit Inventory Control'.

 78  LINE-1  Value 'Inventory Transaction List'.

 78  LINE-2  Value ',"Date","Time","Store","SKU","Width",'
     & '"Brand","Style","Color","Department","Class",'
     & '"Size Run Code","Reorder?","Description",'
     & '"YTD Markdowns","Cost","Retail","Qty","Clerk",'
     & '"Oversold","Price","Size"'.

 01.
     05  WS-UNITS                Pic S9(6) Comp.
     05  RX                      Pic 9(02) Comp-5.
     05  X1                      Pic 9(02) Comp-5.
     05  X2                      Pic 9(02) Comp-5.
     05  X3                      Pic 9(02) Comp-5.
     05  INPUT-CNT               Pic 9(07) Value Zero.
     05  OUTPUT-CNT              Pic 9(07) Value Zero.

 Procedure Division.
 000-BEGIN.
     Open Input INPUT-FILE
     Open Output OUTPUT-FILE
     Write OUTPUT-RECORD from LINE-0
     Write OUTPUT-RECORD from LINE-1
     Write OUTPUT-RECORD from LINE-2
     Perform Until Exit
         Read INPUT-FILE
             At End
                 Exit Perform
         End-Read
         Perform 100-FORMAT-DETAIL
         Write OUTPUT-RECORD
         Display OUTPUT-CNT at 0201
     End-Perform
     Close INPUT-FILE
     Close OUTPUT-FILE
     Stop Run
     .

 100-FORMAT-DETAIL.
     Move Space                       to OUTPUT-RECORD
     Move Zero                        to RX
     Set TCX                          to 1
     Search TC-DATA
         at End
             Move IT-TRANSACTION-CODE to WS-WORD
             Move '--unknown'         to WS-WORD(3:9)
         When TC-CODE(TCX) = IT-TRANSACTION-CODE
             Move TC-NAME(TCX)        to WS-WORD
     End-Search
     Perform A00-MOVE-ALPHA
     Move IT-DATE                     to WW-DATE
     Perform A00-MOVE-ALPHA
     If IT-TRANSACTION-CODE = '2'
         Move IT-SALES-HOUR           to WW-HOUR
         Move ':'                     to WW-COLON
         Move IT-SALES-MINUTE         to WW-MINUTE
     End-If
     Perform A00-MOVE-ALPHA
     Perform 110-BASIC-DATA
     Evaluate IT-TRANSACTION-CODE
         When '2'
             Perform 120-SALES
         When '5'
             Perform 130-RECEIPTS
         When '6'
             Perform 140-TRANSFERS
     End-Evaluate
     .

 110-BASIC-DATA.
     Move IT-STORE-NBR     to WW-STORE
     Perform N00-MOVE-NUMERIC
     Move IT-SKU           to WW-SKU
     Perform N00-MOVE-NUMERIC
     Move IT-WIDTH-CODE    to WW-STORE
     Perform N00-MOVE-NUMERIC
     Move IT-BRAND-CODE    to WS-WORD
     Perform A00-MOVE-ALPHA
     Move IT-STYLE         to WS-WORD
     Perform A00-MOVE-ALPHA
     Move IT-COLOR-CODE    to WS-WORD
     Perform A00-MOVE-ALPHA
     Move IT-DEPT          to WS-WORD
     Perform A00-MOVE-ALPHA
     Move IT-CLASS-CODE    to WS-WORD
     Perform A00-MOVE-ALPHA
     Move IT-SIZE-RUN-CODE to WS-WORD
     Perform A00-MOVE-ALPHA
     Move 'N'              to WS-WORD
     If IT-REORDER-FLAG = '*'
         Move 'Y'          to WS-WORD
     End-If
     Perform A00-MOVE-ALPHA
     Move IT-DESCRIPTION   to WS-WORD
     Perform A00-MOVE-ALPHA
     Move IT-MARKDOWNS-YTD to WW-AMT
     Perform N00-MOVE-NUMERIC
     Move IT-COST          to WW-AMT
     Perform N00-MOVE-NUMERIC
     Move IT-RETAIL        to WW-AMT
     Perform N00-MOVE-NUMERIC
     .

 122-SALES.
     Move IT-SALES-UNITS    to WW-QTY
     Perform N00-MOVE-NUMERIC
     Move IT-CASHIER        to WS-WORD
     Perform N00-MOVE-NUMERIC
     Move IT-UNITS-OVERSOLD to WW-QTY
     Perform N00-MOVE-NUMERIC
     Move IT-SALES-RETAIL   to WW-AMT
     Perform N00-MOVE-NUMERIC
     Move IT-SIZE-X         to WS-WORD
     Perform A00-MOVE-ALPHA
     .

 123-RECEIPTS.
     Move Zero          to WS-UNITS
     Perform Varying X1 from 1 by 1 Until X1 > 20
         Add IT-SOH(X1) to WS-UNITS
     End-Perform
     Move WS-UNITS      to WW-QTY
     Perform N00-MOVE-NUMERIC
     .

 124-TRANSFERS.
     Move Zero                to WS-UNITS
     Perform Varying X1 from 1 by 1 Until X1 > 20
         Add IT-TRANS-QTY(X1) to WS-UNITS
     End-Perform
     Move WS-UNITS            to WW-QTY
     Perform N00-MOVE-NUMERIC
     Move 'To Store:'         to WS-WORD
     Move IT-TO-STORE-NBR     to WS-WORD(11:2)
     Perform A00-MOVE-ALPHA
     Move 'Cost:'             to WS-WORD
     Perform A00-MOVE-ALPHA
     Move IT-TO-STORE-COST    to WW-AMT
     Perform N00-MOVE-NUMERIC
     Move 'Retail:'           to WS-WORD
     Perform A00-MOVE-ALPHA
     Move IT-TO-STORE-RETAIL  to WW-AMT
     Perform N00-MOVE-NUMERIC
     .

 A00-MOVE-ALPHA.
     If RX > Zero
         Move ','            to OUTPUT-RECORD(RX:1)
     End-If
     Add 1                   to RX
     Move '"'                to OUTPUT-RECORD(RX:1)
     Add 1                   to RX
     If WS-WORD not = Space
         Move Zero           to X2
         Inspect WS-WORD Tallying X2 for Leading Spaces
         Perform Varying X3 from Length of WS-WORD by -1   
                   Until WS-WORD(X3:1) not = Space
             Continue
         End-Perform
         Subtract X2       from X3
         Add 1               to X2
         Move WS-WORD(X2:X3) to OUTPUT-RECORD(RX:X3)
         Add X3              to RX
         Move Space          to WS-WORD
     End-If
     Move '"'                to OUTPUT-RECORD(RX:1)
     Add 1                   to RX
     .

 N00-MOVE-NUMERIC.
     If RX > Zero
         Move ','        to OUTPUT-RECORD(RX:1)
     End-If
     Add 1               to RX
     If WS-WORD = Space
         Exit Paragraph
     End-If
     Move Zero           to X2
     Inspect WS-WORD Tallying X2 for Leading Spaces
     Perform Varying X3 from Length of WS-WORD by -1 
               Until WS-WORD(X3:1) not = Space
         Continue
     End-Perform
     Subtract X2       from X3
     Add 1               to X2
     Move WS-WORD(X2:X3) to OUTPUT-RECORD(RX:X3)
     Add X3              to RX
     Move Space          to WS-WORD
     .
 
Thanks, tat is very helpful. I'll try coding that into my program and let you know how I get on,
thanks,
Matt
 
We use an application called decision analyzer that does this pretty well. Can output reports, CSV, fixed field length, XSL, Database files. Product made near Princeton, NJ. Works with database or vsam. If you do not like my post feel free to point out your opinion or my errors.
 
My 2 cents.................
FD CFS-REG-REN
RECORD IS VARYING IN SIZE
FROM 10 TO 512 CHARACTERS
DEPENDING ON CHARACTERSPLUS2.
01 SEQREC PIC x(512).


01 CHARACTERSPLUS2 PIC 999.
01 STRING-AREA PIC X(512).
01 STR-AREA REDEFINES STRING-AREA.
05 STA PIC X OCCURS 512.

MOVE SPACES TO STRING-AREA.
STRING
'"' DELIMITED BY SIZE
BGEOG DELIMITED BY size
'","' DELIMITED BY size
BPO DELIMITED by ' '
'","' DELIMITED BY size
BACCT DELIMITED BY size
'"' DELIMITED BY size
INTO STRING-AREA.
MOVE 512 TO CHARACTERSPLUS2.
PERFORM COUNT-DOWN THRU COUNT-DOWN-EXIT.
WRITE SEQREC FROM string-area.

COUNT-DOWN.
IF STA (CHARACTERSPLUS2) NOT = " "
GO TO COUNT-DOWN-EXIT.
IF CHARACTERSPLUS2 = ZEROES
DISPLAY 'NO DATA IN RECORD'
DISPLAY STRING-AREA
ACCEPT ACTION.
SUBTRACT 1 FROM CHARACTERSPLUS2.
GO TO COUNT-DOWN.
COUNT-DOWN-EXIT.
EXIT.
Enjoy Bob
 
ShadowFox:

STRING is ok if you only have a few fields. In Microfocus COBOL, LINE SEQUENTIAL (text) file records are automatically variable length, terminated by the system after the last non-blank.
 
The string has worked well for me over time - one other comment, don't forget that CSV may need to "escape" an embedded delimiter - for example, if one's alphanumeric field contains a comma (e.g. the field separator), one now needs to enclose the field with a text delimiter such as double quotes. So what does one do when there is a double quote in the alphanumeric field? This depends on the sophistication of the target application's CSV routines and may take some tuning - I have typically coded an "escaping" double quote, then the original double quote in the text.

For example, assume the field delimiter is a comma (,) and the text delimiter is a double quote ("), and the alpha numeric field is (Furthermore, the result "44" is invalid.)
The resultant ("Furthermore, the result ""44"" is invalid.",) should work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top