Is there a way to $include csv files into parameters/sets?

Problems with syntax of GAMS
Post Reply
AndrewC
User
User
Posts: 17
Joined: 5 years ago

Is there a way to $include csv files into parameters/sets?

Post by AndrewC »

I often have data read in using $include files, formatted using the a.b.c 10 format

parameter myData(a,b,c) /
$include myData.txt
/;

myData.txt:
a1.b1.c1 10
a2.b2.c2 20


Is there a way to do this with csv files?

parameter myData(a,b,c) /
$include myData.csv
/;

myData.csv:
a1,b1,c1,10
a2,b2,c2,20

The motivation behind this is sometimes the data files are empty. I have switched to trying csv2gdx but if the csv file is empty it generates a gdx that exists but is empty and I then get a crash when trying to GDXIN $load the data from it (which makes sense).
In my txt data I use the $onEmpty so I can get around this. So if I can find a way to use $include myData.csv I could probably continue to do this.

All help appriciated

Thanks
User avatar
bussieck
Moderator
Moderator
Posts: 1042
Joined: 7 years ago

Re: Is there a way to $include csv files into parameters/sets?

Post by bussieck »

Check out $on/offDelim: https://www.gams.com/latest/docs/UG_Dol ... onoffdelim. For large CSV file, I also recommend $offListing (https://www.gams.com/latest/docs/UG_Dol ... offlisting) to keep the lst file small.

-Michael
User avatar
dirkse
Moderator
Moderator
Posts: 215
Joined: 7 years ago
Location: Fairfax, VA

Re: Is there a way to $include csv files into parameters/sets?

Post by dirkse »

Hello,

The empty GDX file may go back to cases where csv2gdx encounters an error and returns an error code. For example, with an empty (zero-length) CSV file I get an empty GDX file if csv2gdx fails but a nice GDX file if csv2gdx runs cleanly.

Code: Select all

fractal:/home/sdirkse/models/csv$ csv2gdx emptyNoHeader.csv  ID=d  index='1,2' value=3  || echo "csv2gdx failed"
*** ErrNr = 9 Msg = No ColCount and NoHeader
CSV2GDX time = 0ms
csv2gdx failed
fractal:/home/sdirkse/models/csv$ csv2gdx emptyNoHeader.csv  ID=d  index='1,2' value=3 colCount=3 || echo "csv2gdx failed"
No errors, CSV2GDX time = 3ms
fractal:/home/sdirkse/models/csv$ gdxdump emptyNoHeader.gdx 
$onEmpty

Parameter d(*,*) / /;

Set Dim1(*) / /;

Set Dim2(*) / /;

$offEmpty
Post Reply