RATS 11
RATS 11

OPEN( options )   RATS I/O unit    filename

Opens a new or existing file as a RATS I/O unit (any of the standard RATS I/O units, or a name you define). By defining your own unit names, you can have several data or output files open, and switch between them (with UNIT options or CHANGE).


OPEN is commonly used as the first step in reading data into RATS, writing data out to a file, saving graphs to a file, and directing output to a file (rather than to the screen).

Parameters

filename

file, URL, or window name depending upon what you are doing. A URL must use an http:// or https:// prefix or a file:// prefix if it's a local executable. If a file name has embedded spaces, it must be enclosed in either "..." or '...'.

 

If the filename is in a STRING variable, you can use &name_of_string for filename to open the file whose name is in the variable name_of_string

Options

WINDOW/[NOWINDOW]

If you use the WINDOW option, a text window will be opened with the title that you provide in the filename field. This window can be used for text output.

 

APPEND/[NOAPPEND]

Use APPEND to append new output to the end of an existing text file. By default, OPEN destroys the contents of an existing file.

 

IMMEDIATE/[NOIMMEDIATE]

FORMAT=[FREE]/BINARY/CSV/DBF/DIF/HTML/MATLAB/PORTABLE/PRN/RATS/RTF/TDF/TEX/WF1/XLS/XLSX/'(FORTRAN format)'

WRITE/[NOWRITE]

STATUS=INTEGER variable returning status of open attempt [unused]

Normally, OPEN just associates a filename with a unit name. The file isn’t actually opened until you use an instruction like DATA or COPY. Use the IMMEDIATE option if you want to actually open the file immediately. The FORMAT option specifies the format of the file (see DATA or COPY for details on these). WRITE opens the file for writing (output), otherwise it will be opened for reading (input). If you use the STATUS option, the variable you supply will be set to 1 if the file was opened successfully, and 0 otherwise.

Examples

open data rates.rat

data(format=rats)

 

opens RATES.RAT as a data file and reads from it (DATA is the default I/O unit for the DATA instruction).


 

open copy teststat.fil

do i=1,draws

      ...

     write(unit=copy) tstats

end do i

 

opens the file TESTSTAT.FIL as a “copy” file, and writes output to it from inside a loop. Note that you put the OPEN instruction outside the loop. If you put it inside, each OPEN will erase the old results.


 

 

compute fname=thiscountry+".rat"

open data &fname

data(format=rats)

 

opens as the DATA unit a file whose name is created from appending ".rat" to the current contents of the string THISCOUNTRY and reads its contents. (DATA is the default I/O unit for the DATA instruction).



 

open(window) tests "Test Results"

display(unit=tests) "Test Statistic=" %cdstat "P-value" %signif

 

opens the unit TEST as a window titled “Test Results” and puts information from the DISPLAY instruction into it.

Example with URL

open data "https://api.statistiken.bundesbank.de/rest/download/BBDE1/M.DE.Y.AEA1.A2P300000.F.C.I15.L?format=csv&lang=en"

data(format=cdf,nolabels,org=col,skiplines=10,right=2) 1991:1 * neword

 

fetches information from the Deutsche Bundesbank time series database. Note that you have to be very careful about the syntax of the downloaded file to handle the extra information that may be present. In this case, it skips 10 lines of headers (SKIPLINES=10 option) and extra columns beyond the dates and data (RIGHT=2).

Notes

You can use the instruction CHANGE to redefine an I/O unit, which can be particularly useful for redirecting output to different files.

 

The instruction CLOSE closes its associated file. A text file is usually left “open” after an instruction writes to it. In general, an open file can’t be read from another program. If you’re done with a unit, issuing a CLOSE instruction for it will make it possible to process the information with another application.

 


Copyright © 2025 Thomas A. Doan