CSV or otherwise delimited

Top  Previous  Next

GAMS programmers may be moving data to external routines that facilitate or require the reading of data in CSV (comma delimited) format (could also involve tab or space delimiters).  This may be done by writing a custom set of put file instructions or through Rutherford's Gams2csv as discussed below.  When writing a put file, data in CSV format are most simply generated by entering the Put file pc command.  This is done in the context of the regput.gms example above by adding a single line as follows (regputcsv.gms)

 

file tosass;

put tosass;

tosass2.pc=5;

loop(run,

  put run.tl;

  put @12;

put /;

loop(decwant,s=  fawelsum( "Agconswelf",decwant,run)/1000;put s:13:0;);

put /;

loop(decwant,s=  fawelsum( "Agprodwelf",decwant,run)/1000;put s:13:2;);

put /;

loop(decwant,

s=  fawelsum( "AGtotwelf",decwant,run)/1000;put s:13:0;);

put /;),

 

A portion of the resultant file (tosass2.put) contents is

 

"r1"

30,40,50

60.00,70.00,80.00

7.56,15.11,22.67

"r2"

40,50,60

70.00,80.00,90.00

7.56,15.11,22.67

"r3"

50,60,70

80.00,90.00,100.00

7.56,15.11,22.67

Notes:

When put commands are run with the pc=5 option then the labels are encased in quotes and the numbers are separated by commas.
When put commands are run with the pc=5 all the spacing and field widths in the put file will be suppressed.
Space delimited files can be generated with pc=4 as discussed in the Output via Put Commands chapter.
Tab delimited files can be generated with pc=6 as discussed in Output via Put Commands chapter.