include a txt file in GAMS - errors 140 & 36 in the first lines of my txt file - gams-math

I am a new user in GAMS and I am trying to include a txt file in my code, but I get the same errors (error 140 & error 36) in the first lines of my txt file again and again.
Could anyone help?
My code goes like this, and I have also attached the txt file
* define the set of asset classes
set n Number of returns /n1*n120/;
* define Tables, Parameters, Scalars
Scalar T /120/;
$INCLUDE prices.txt
Please note, that later in the code I need to use the data from the text file in an equation like this:
EQ1.. sum(n, p(n)*prices(n)) =e= price0*exp(r*time);
Thanks
prices.txt file

Your prices.txt does not contain valid GAMS syntax. What you need is something like this:
Parameter prices(n) /
n1 455.5
n2 44545.5
/;

Related

How do I loop excel sheet names inside quotes in GAMS 'GDXXRW?

I would like to define a string set in GAMS to loop to read different xlsx sheet names in gdxxrw. However, I didn't find the appropriate code in the help documentation using string formatting and String Substitution as keywords.
'''
SET
sheets /index1, index2, index3/
B /a, b, c/
;
PARAMETER
POP0(B)
POP(B)
;
LOOP(sheets,
* Write Excel data to GDX
put_utility 'exec' / 'gdxxrw.exe Input.xlsx se=0 index='sheets.tl'!a1';
$Load POP0
POP(B)=POP0(B);
execute_unload "Output.gdx"
POP
* Write GDX data to excel
put_utility 'exec' / 'gdxxrw.exe RESULT.gdx o=RESULT.xlsx index='sheets.tl'!a7';
'''
I read loop in GAMS for scenario generation in excel. But GAMS report error as that
'''
116 Label is unknown
141 Symbol declared but no values have been assigned. Check for missing
data definition, assignment, data loading or implicit assignment
via a solve statement.
A wild shot: You may have spurious commas in the explanatory
text of a declaration. Check symbol reference list.
300 Remaining errors not printed for this line
352 Set has not been initialized
353 The domain for this index position is unknown and the element
cannot be checked at this point. Missing data statement.
502 GDXIN file not open - ignore rest of line
509 Dollar control statements are processed during compilation
and NOT during execution. The use of dollar statements
that change data may be misleading when inside a LOOP
or IF statement.
'''
I think the key is the apply of $Load or $loaddc. Does GAMS have a string formatting method control index sheet in quotes?
After a night of debugging, I finally solved the problem.
'''
SET
sheets /index1, index2, index3/
B /a, b, c/
;
PARAMETER
POP0(B)
POP(B)
;
LOOP(sheets,
* Write Excel data to GDX
put_utility 'exec' / 'gdxxrw.exe Input.xlsx se=0 index='sheets.tl:0'!a1';
*":0" is needed
* Load data from GDX (substitution of $load)
execute_load 'data.gdx',
POP0
;
POP(B)=POP0(B);
* Load data from GDX
execute_unload "Output.gdx",
POP
;
* Write GDX data to excel
put_utility 'exec' / 'gdxxrw.exe RESULT.gdx o=RESULT.xlsx index='sheets.tl:0'!a7';
'''
Very thank Lutz!

Directory.EnumerateFiles with Take and Where

I have following Problem:
For Each _file As String In Directory.EnumerateFiles(Quellpfad, "*.rdy").Take(500).Where(Function(item) item.Replace(Quellpfad, "").Length <= 11)
this code should take files from a Directory saved in the String "Quellpfad"
by These 2 criteria:
1.) only 500 files
2.) filename lenth <= 11 eg.: 0330829.rdy
The file 0330829.rdy is in the Directory but I can't find it with the code above.
You should use Take last because you want to apply the filter first, you should also use Path.GetFileName or Path.GetFileNameWithoutExtension instead of String.Replace:
Dim files = From file In Directory.EnumerateFiles(Quellpfad, "*.rdy")
Where Path.GetFileName(file).Length <= 11
Take 500
In VB.NET query syntax supports Take, so i'd prefer that.
You need to reorder your statement to put the Where before the Take:
For Each _file As String In Directory.EnumerateFiles(Quellpfad, "*.rdy").Where(Function(item) item.Replace(Quellpfad, "").Length <= 11).Take(500)
The Where returns all files matching your condition FIRST, and THEN limits those down to 500.

Fortran runtime error: End of file when reading input data

I'm currently running a code and I'm always getting to the same end. I am trying to read an input file and it returns the error:
Fortran runtime error: End of file
In an other post they said to put in the iostat specifier so now my code looks like this:
INTEGER :: m
INTEGER :: st
Open(Unit = 13,action='read',file='Data_Inp.dat',status='old')
read (13,*, iostat = st) m
write (*,*) st
write (*,*) m
ALLOCATE(winkel(m),energie(m))
Do i = 1,m
read(13,*),winkel(i),energie(i)
End Do
And the input file looks like this:
12
-17.83 -0.019386527878
-15.83 -0.020125057233
-12.83 -0.020653853148
-11.83 -0.020840036028
-9.83 -0.020974157405
-8.83 -0.021056401707
-6.83 -0.021065517811
-5.83 -0.020992571816
-4.83 -0.020867828448
-1.83 -0.02069158012
Now the terminal prints a -1 for iostat and a constantly changing number for m.
If the first read command is causing an error, check for extraneous characters before or after "12" in your input file, especially if you created it on one platform (Windows?) and using it on another platform (Linux? Mac?)

How to run same syntax on multiple spss files

I have 24 spss files in .sav format in a single folder. All these files have the same structure. I want to run the same syntax on all these files. Is it possible to write a code in spss for this?
You can use the SPSSINC PROCESS FILES user submitted command to do this or write your own macro. So first lets create some very simple fake data to work with.
*FILE HANDLE save /NAME = "Your Handle Here!".
*Creating some fake data.
DATA LIST FREE / X Y.
BEGIN DATA
1 2
3 4
END DATA.
DATASET NAME Test.
SAVE OUTFILE = "save\X1.sav".
SAVE OUTFILE = "save\X2.sav".
SAVE OUTFILE = "save\X3.sav".
EXECUTE.
*Creating a syntax file to call.
DO IF $casenum = 1.
PRINT OUTFILE = "save\TestProcess_SHOWN.sps" /"FREQ X Y.".
END IF.
EXECUTE.
Now we can use the SPSSINC PROCESS FILES command to specify the sav files in the folder and apply the TestProcess_SHOWN.sps syntax to each of those files.
*Now example calling the syntax.
SPSSINC PROCESS FILES INPUTDATA="save\X*.sav"
SYNTAX="save\TestProcess_SHOWN.sps"
OUTPUTDATADIR="save" CONTINUEONERROR=YES
VIEWERFILE= "save\Results.spv" CLOSEDATA=NO
MACRONAME="!JOB"
/MACRODEFS ITEMS.
Another (less advanced) way is to use the command INSERT. To do so, repeatedly GET each sav-file, run the syntax with INSERT, and sav the file. Probably something like this:
get 'file1.sav'.
insert file='syntax.sps'.
save outf='file1_v2.sav'.
dataset close all.
get 'file2.sav'.
insert file='syntax.sps'.
save outf='file2_v2.sav'.
etc etc.
Good luck!
If the Syntax you need to run is completely independent of the files then you can either use: INSERT FILE = 'Syntax.sps' or put the code in a macro e.g.
Define !Syntax ()
* Put Syntax here
!EndDefine.
You can then run either of these 'manually';
get file = 'file1.sav'.
insert file='syntax.sps'.
save outfile ='file1_v2.sav'.
Or
get file = 'file1.sav'.
!Syntax.
save outfile ='file1_v2.sav'.
Or if the files follow a reasonably strict naming structure you can embed either of the above in a simple bit of python;
Begin Program.
imports spss
for i in range(0, 24 + 1):
syntax = "get file = 'file" + str(i) + ".sav.\n"
syntax += "insert file='syntax.sps'.\n"
syntax += "save outfile ='file1_v2.sav'.\n"
print syntax
spss.Submit(syntax)
End Program.

error when trying to import ps file by grImport in R

I need to create a pdf file with several chart created by ggplot2 arranged in a A4 paper, and repeat it 20-30 times.
I export the ggplot2 chart into ps file, and try to PostScriptTrace it as instructed in grImport, but it just keep giving me error of Unrecoverable error, exit code 1.
I ignore the error and try to import and xml file generated into R object, give me another error:
attributes construct error
Couldn't find end of Start Tag text line 21
Premature end of data in tag picture line 3
Error: 1: attributes construct error
2: Couldn't find end of Start Tag text line 21
3: Premature end of data in tag picture line 3
What's wrong here?
Thanks!
If you have no time to deal with Sweave, you could also write a simple TeX document from R after generating the plots, which you could later compile to pdf.
E.g.:
ggsave(p, file=paste('filename', id, '.pdf'))
cat(paste('\\includegraphics{',
paste('filename', id, '.pdf'), '}', sep=''),
file='report.pdf')
Later, you could easily compile it to pdf with for example pdflatex.