Beginner Question: Using csv as an input

Problems with syntax of GAMS
mjcecili
User
User
Posts: 12
Joined: 4 years ago

Re: Beginner Question: Using csv as an input

Post by mjcecili »

Hi Renger,

Thanks a lot, I got it. You have been so helpful.

I am sorry if I bother you, but you have a small example of writting results to a CSV file?

Kind regards. :)
mjcecili
User
User
Posts: 12
Joined: 4 years ago

Re: Beginner Question: Using csv as an input

Post by mjcecili »

I am trying to read the following CSV file (hourlyDataInput3.csv):

p,Demand,WindGen
1,17.64,4.52
2,21.13,3.33
3,22.48,5.74
4,22.01,4.81

The code is:

* definitions
sets
p /1*4/
;

parameters
pDemand (p)
pWindGen (p)
;

table Hourly(*,*) "Demand and Wind Generation per year"
$ondelim
$include hourlyDataInput3.csv
$offdelim
;

pDemand(p) = Hourly (p,'Demand');
pWindGen(p) = Hourly (p,'WindGen');

display pDemand, pWindGen;


However, I have mistakes. Could you help me to fix this. Thanks in advance. :)
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Beginner Question: Using csv as an input

Post by Renger »

Hi
This runs fine on my computer. Did you include the correct csv file? What is the error you get?
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
mjcecili
User
User
Posts: 12
Joined: 4 years ago

Re: Beginner Question: Using csv as an input

Post by mjcecili »

Hi Renger,

You are rigth, I found the problem. I had the input files in a different directory.

I am sorry to bother again. Now, I want o read a CSV file that does not have any header. I mean, it is bidimensional (i,j) and it has just the values for each i,j

The file (Cost.csv) is like:
1,1,1,1,1
0,1,0,1,0
1,1,1,1,1

And my program is like this:

sets
i generation units /1*3/
j time periods /1*5/
;

table Cdata(i,j) cost for unit i at period j
$ondelim
$include Cost.csv
$offdelim
;

display Cdata;


However, I am having some errors about duplicated values.

By the way, do you have a short example of writing to CSV files?


Thanks a lot :D
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Beginner Question: Using csv as an input

Post by Renger »

Hi
You need the labels for the table (e.g. i and j) otherwise Gams assumes the first column and row as set elements (therefore the duplicates).
If you want to write a csv file you can do stuff like this

Code: Select all

file myfile /output.csv/;
put myfile;
loop(i, 
   loop(j, 
      put i.tl  ... 
     etc.
Have a closer look at the put documentation.
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
mjcecili
User
User
Posts: 12
Joined: 4 years ago

Re: Beginner Question: Using csv as an input

Post by mjcecili »

Hi,

Thank you. Is there any other way to read the file? I mean, without the labels?

Kind regards. :)
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Beginner Question: Using csv as an input

Post by Renger »

Not that I am aware of.
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
mjcecili
User
User
Posts: 12
Joined: 4 years ago

Re: Beginner Question: Using csv as an input

Post by mjcecili »

Thanks a lot Renger!!! :D
Post Reply