If you want to load data from an excel spreadsheet, please find attached one method that works. This is from an actual model that reads the data from columns in the spreadsheet. The key structure is first to define the set and parameter names that the model will be reading from the excel file, then to identify the location of the data through the $call command, then create the gdx file where the data will be stored through the $gdxin command and lastly import the variable and parameter data into the model via the $load command.
Define the sets and parameters to be read from excel
$call "gdxxrw %modelname%.xlsx
$gdxin %modelname%.gdx
$load
First step, identify the set and parameters you will be using in your model and reading from the file. Next, you will note in the string attached there is %modelname% as in the model I defined the model name so I would not have to rewrite it. You should insert the name of your spreadsheet here so it would read $call “gdxxrw mymodelname.xlsx “. You then need to define the data to be read, is it a set or parameter, in this case my first data to be read was SlsPosition, and I used dset because there was a risk of duplicates in the data and the dset command removes duplicates, otherwise you should simply use set or parameter as the designation. Next is the name of the data that you will be using in your model, then where is it in the excel file, in my case the Rng= identifies in spreadsheet mymodelname.xlsx, tab in the spreadhsheet named ModelInput!, in column AC2:AC100 is all the data to be imported as set SlsPosition while removing any duplicates. Lastly you need to define the dimensions of the data, both row and columns, and in this case the data is 1 dimensional so I set rdim=1. You should review McCarl’s on this as it will provide you guidance on the defaults and how to specifiy the rdim and cdim values. This last area can give you an unable to read error if you get the dimensions wrong. In fact any error on range or dimensions etc… will give you that error command.
The last word of advice is if your data set is large and it takes a while to open the spreadsheet you need to write these commands all on one line, you may not hit enter and continue writing on the next line. This is cumbersome as I have provided only a portion of the data read in this example and it makes the model a little hard to read but then you only open the file once and you can speed up the import process.
Set SlsPosition,SlsLevel,FileNum,Name,JobTitle,JobNum,DeptName,DeptNum,SlsZip;
Parameter SlsCompCalc(FileNum,SlsFactors);
$call “gdxxrw %modelname%.xlsx dset SlsPosition Rng=ModelInput!AC2:AC100 rdim=1 dset SlsLevel Rng=ModelInput!AD2:AD100 rdim=1 dset=FileNum Rng=ModelInput!A2:A100 rdim=1 dset=Name Rng=ModelInput!B2:B100 rdim=1 dset=JobTitle Rng=ModelInput!AB2:AB100 rdim=1 dset=JobNum Rng=ModelInput!H2:H100 rdim=1 dset=DeptName Rng=ModelInput!AF2:AF100 rdim=1 dset=DeptNum Rng=ModelInput!AE2:AE100 rdim=1 set=DeptNum2DeptName Rng=ModelInput!AE2:AF100 rdim=2 dset=SlsZip Rng=ModelInput!Q2:Q100 rdim=1 par=SlsCompCalc Rng=ModelInput!S1:V100 rdim=1 cdim=1”
$gdxin %modelname%.gdx
$load SlsPosition
$load SlsLevel
$load FileNum
$load Name
$load JobTitle
$load JobNum
$load DeptName
$load DeptNum
$load SlsZip
$load SlsCompCalc
With all that being said, reading from excel works pretty good but truthfully is quite cumbersome and is pretty poor if you have a large excel file. In our models we tend to read everything in via include files using the include command, where you all you do is include filename and in the .inc file itself you specify the set or parameter and the data associated with it. Below is an example of the include file as you will see it is exactly as you would have the data if you wrote it into the model and in this approach there is no need to define the set or parameter in the model as it is defined in the include file. It is also wise to use quotation marks around data types that have spaces or other characters in them to ensure the data is read and the spaces ignored, otherwise they could create a reading issue. This method reads much faster than an excel spreadsheet, almost instantly, and it avoids any excel limitation on rows or columns as excel is capped at around 1 million rows and something like 20000 columns. Our models almost always use this approach as it is far more efficient than excel.
Include File example of data in the file
set SalesID/
118509
117856
119324
119332
/;
set ID2EmpName(SalesID,EmpName)/
118509 . “Joe Blow”
117856 . “John Smith”
119324 . “Jerry White”
119332 . “Rod Stewart”
set EmpZip(ZipCodes)/
33027
36064
70810
/;
set Emp2Zip(SalesID,EmpZip)/
118509 . 33027
117856 . 36064
119324 . 70810
119332 . 70810
/;
parameter SalesQuota(SalesID)/
118509 603119
117856 867162
119324 346750
119332 880844
/;
Regards,
John A. Ryan john.ryan@orm-tech.com
From: gamsworld@googlegroups.com [mailto:gamsworld@googlegroups.com] On Behalf Of Ines TR
Sent: Saturday, July 06, 2013 8:50 AM
To: gamsworld@googlegroups.com
Subject: Re: How to import excel file in GAMS?
Hello Alireza!
I found your answer to this post.
I am having the same problem as Annoisa regarding being a beginner and unable to import into GAMS from Excel.
I´ve tried a few things and nothing works. I wonder if you can have a look at the following code and identify what might be wrong with it.
The error message I get is : Unable to open gdx file for $GDXIN. It seems to me that the gdx file is not being generated.
SET
i
r ;
alias(i,j);
$CALL GDXXRW.EXE I=InputGAMS.xlsx O=InputGAMS.gdx skipempty=0 trace=2 index=Index!A1’
$GDXIN InputGAMS.gdx
$LOAD i r
PARAMETERS
d(i) annual demand of product i
c(j) capacity at supplier j
B(j,r) busines vol interval
k(j,r) upper limit r for j dsct sched
P(i,j) price data
Q(i,j) quality data
L(i,j) lateness data
S(i,j) sustainability data
$GDXIN InputGAMS.gdx
$load d c b k p q l s
$gdxin
Thanks so much!
Ines
El viernes, 17 de mayo de 2013 16:50:31 UTC-5, Alireza Bahari escribió:
Hi
Try this by the attachments:
First of all locate the .xls file in the following path:
E:\Data\MatrixInputs.XLS
Have a look at the sheet named “index” and in rows, 5,6,7 and 17 of my cod to discover how importing data from excel is performed.
The 5 last rows export data to Excel
Close the excel file when GAMS is runing.
On Fri, May 17, 2013 at 11:50 PM, nadia moshahedi wrote:
thank you. but I still have some problem. I figure out that inorder to import data from excell to gams, I should install gdxxrw file. I Am I right or I did not get the point? I look for it every where in the internet, but I could not find any thing. All the links were unavailable.
did you install this on your pc? do you have it?I wonder if it is necessary to install this software?
thanks for your help my friend
Nadia
On Fri, May 17, 2013 at 1:47 AM, Savitsky Andre wrote:
Dear Friend!
You wrote that data located on ap.xls file
Will be better if You wrote full path
You say rng =a1:e5
But how it located here?
What the spreadsheet?
use this rng = spreadsheet_name!a1 rdim=1 cdim =1
locate data
i1 i2 i3 i4
j1 1 2 3 4
j2 54 5 6 7
16.05.2013, 19:52, “nadia” :
Dear Friend,
I import data from excel to gams using below code.
$CALL GDXXRW.EXE ap.xls par=cf rng=A1:E5
Parameter cf(,);
$GDXIN ap.gdx
$LOAD cf
$GDXIN
display cf;
the model run truelyy, It produce gdx files and show the solutions. “cf” parametesr mentioned above are the coefficients of the objective functions. the gams ignores these coefficients.my objective function is :
one … z =e= sum((i,k,l,j), cf(i,k,l,j)*x(i,k,l,j));
but in the output gams does not show the coefficients of X.
let’s help me.
thank you.
On Tuesday, 14 February 2012 14:09:48 UTC+3:30, Annoisa wrote:
Dear Elcin rather dearest at the moment,
Your email looks a bit easy i shall work accordingly and may i ask if somewhere i got stuck?because i just started GAMS…new born in GAMS…
kind wishes,
Sara
On Tue, Feb 14, 2012 at 2:25 AM, ELCIN KABELOGLU wrote:
Hi,
Here there is a mini example to show how to import data from excel to gams.
Firstly you have to have new excel file in gamsdir folder. In the example it is deneme.xlsx
Your data should be inside this file. Then you can copy the the codes below. Hope it works
Elcin,
set
positions/1*73/
make/model1,model2,model3/
color/color1,color2,color3,color4,color5/;
// sets are defined
parameter firstposassignn(positions,make,color);
*data
$call “gdxxrw dene.xlsx par=firstposassignn rng=sayfa1!a2:c5 rdim=3”
$gdxin dene.gdx
$load firstposassignn
display firstposassignn
2012/2/13 sara sohail
Hi All!
i did not understand it can anyone explain how to do this. because i did not get a hint about GDX file? Or how to get Excel file in GAMS. Please help me. The things on line are not so easy to understand.
Wishes,
Sara
On Mon, Feb 13, 2012 at 1:33 AM, sara sohail wrote:
Thax Deepti,
I shall try it then come to you again.
Wishes,
Sara
On Mon, Feb 13, 2012 at 1:01 AM, deepti singhal wrote:
Dear Sara
Pl follow this link
http://www.gams.com/docs/excel/
I hope it helps…
U need to convert an excel file in GDX format and then u can read it in gams
All the best
Cheers
Deepti
On Mon, Feb 13, 2012 at 2:05 PM, sara sohail wrote:
Dear GAMS Friends,
I am a beginner in GAMS so i need help how to import excel file in to GAMS software.
Can any of you help me so that i can understand it and complete my work?
Kind Regards,
Sara
\
Deepti Chadha (Singhal)
\
With kindly regards!
Andre Savitsky
E-mail:a...@yandex.ru
andre...@yahoo.com
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at http://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/groups/opt_out.
\