Page 1 of 2

SAS files into GAMS

Posted: Wed Sep 26, 2018 12:11 pm
by Jarenka
Dear,

I would like to know whether there is a possibility to read files from SAS into GAMS.

Instead of, for example, Excel or csv files, I would like to use SAS data inputs.
The reason of that is that on the machine, which we use to create our projects, we store a huge amount of data in SAS data base. And to avoid transferring aggregated data from SAS to something else and then to GAMS, I would like to know whether there is a direct way from SAS to GAMS.

All the best
irena

By the way, I have found some help in reading in other files: https://www.gams.com/latest/datalib_ml/ ... ddata.html.
Could be a possibility to re-program the examples in a way that it reads other files?

Re: SAS files into GAMS

Posted: Wed Sep 26, 2018 2:13 pm
by Renger
Hi Irena
I would recommend to use R to read SAS-files. You can then export the data to a gdx file using gdxxrw.
Cheers
Renger

Re: SAS files into GAMS

Posted: Wed Sep 26, 2018 2:26 pm
by Jarenka
Dear Renger,
Thank you.
Yes, but the problem is that I do not want to use any indirect program between SAS and GAMS.
In this case you suggest I have to use R to direct a data file to GAMS from SAS.

Is there any way to do it without any indirect program?

Best
irena

Re: SAS files into GAMS

Posted: Tue Oct 02, 2018 9:12 am
by Renger
Hi Irena
As far as I know this is not possible. However, you can run an R-script from within Gams using:

Code: Select all

execute 'rscript myscript.R'
Cheers
Renger

Re: SAS files into GAMS

Posted: Mon Oct 08, 2018 11:14 am
by Jarenka
I am also trying to include R program, which you wrote, into GAMS.

Do I need to specify something more? Like for example:
- path to my R-script,
- global options at the beginning of the GAMS program to "tell" GAMS that I am going to read in data from R.

Best
Irena

Re: SAS files into GAMS

Posted: Mon Oct 08, 2018 11:25 am
by Renger
Hi Irena
If you have the path of R in your system variable "PATH", there is no need, otherwise, you either specify the whole path to the gams code
e.g. $call c:\R\bin\rscript myscript.R'
(note that I write '$call' as I assume that the data must be read in compile phase.)
or add it to your system variable "PATH"
No need to tell Gams anything more.

Cheers
Renger

Re: SAS files into GAMS

Posted: Mon Oct 08, 2018 12:45 pm
by Jarenka
Yes, it works! Thank you!

I am attaching the "screen-cut" from my program.

On the left hand side there is my code with specified variables. On the right hand side there is a screen-display from R-program (the first data-file with dimensions and variables). You can see which parameters I want to include in *.gdx file:
- from QEGDET to YOSSFAEDET;

How to load them all, with their dimensions, into *.gdx file? They come from a program, and not like usual from external file.

Best
Irena

Re: SAS files into GAMS

Posted: Mon Oct 08, 2018 2:50 pm
by Renger
Hi Irena
You could use wgdx to write the dataframe to gdx and then load it. After that you assign the variables

Code: Select all

lst <- wgdx.reshape(dataframeinR symDIm = 4, symName = "alldata")
wgdx.lst("datafromR.gdx", lst)
If you have more data, you just define lst2, lst3, etc. for the data and read them with wgdx.lst("datafromR.gdx",lst, lst2, ...)

In Gams you can now easily load this from the gdx file and assign for example (you could even read the sets from alldata using gdx):

Code: Select all

QUADET(erd1det, erd2, akm, ard) = alldata(erd1det, erd2, akm, ard, "QUADET");
If this is taken a long time, you should split your model file from your data reading file, so you only have to run it once in a while.

CHeers
Renger

Re: SAS files into GAMS

Posted: Tue Oct 09, 2018 1:11 pm
by Jarenka
So far is good.

Firstly, I had to install gdxrrw package by using *.zip file, which fortunately was under GAMS folder :-) - and that was very good, because we work on the research machines (Windows 32-bit) that do not have access to the Internet due to confidentiality.

In the attachment, at the bottom, there is my R-code that reads dat-file consisting of 4 dimensions and 9 variables.
On the photo (foto1) you can see symDim = 15.
In fact I need only 4, because I have 4 dimensions and 11 variables. But the code (with symDim = 4) gave me an error - foto4.

In the second screen shot (foto2) I have the results from GAMS - this is a data set. This data set should include first 4 dimensions and the rest (in red circle) should be variables.
But it seems that all inputs are sets.

In the third screen shot (foto3) there is my program in GAMS. I couldn't make your code working:

Code: Select all

QUADET(erd1det, erd2, akm, ard) = alldata(erd1det, erd2, akm, ard, "QUADET");
I tried with "alldata" and with "DataFromR" option.

So far, the code is working in R-program, but it seems that GAMS doesn't read sets and parameters properly.

Best
Irena

Re: SAS files into GAMS

Posted: Tue Oct 09, 2018 1:12 pm
by Jarenka
And foto4 attached below: