OPEN Function

Opens a SAS data set.

Category: SAS File I/O

Syntax

Optional Arguments

data-set-name

is a character constant, variable, or expression that specifies the name of the SAS data set or SAS SQL view to be opened. The value of this character string should be of the form

<libref.> member-name<(data-set-options)>

Default The default value for data-set-name is _LAST_.
Restriction If you specify the FIRSTOBS= and OBS= data set options, they are ignored. All other data set options are valid.

mode

is a character constant, variable, or expression that specifies the type of access to the data set:

I opens the data set in INPUT mode (default). Values can be read but not modified. 'I' uses the strongest access mode available in the engine. That is, if the engine supports random access, OPEN defaults to random access. Otherwise, the file is opened in 'IN' mode automatically. Files are opened with sequential access and a system level warning is set.
IN opens the data set in INPUT mode. Observations are read sequentially, and you are allowed to revisit an observation.
IS opens the data set in INPUT mode. Observations are read sequentially, but you are not allowed to revisit an observation.
Default I

generation-number

specifies a consistently increasing number that identifies one of the historical versions in a generation group.

Tip The generation-number argument is ignored if type = F.

type

is a character constant and can be one of the following values:

D

specifies that the first argument, data-set-name, is a one-level or two-level data set name. The following example shows how the D type value can be used:

 rc = open('lib.mydata', , , 'D');
Tip D is the default if there is no fourth argument.

F

specifies that the first argument, data-set-name, is a filename, a physical path to a file.The following examples show how the F type value can be used:

rc = open('c:\data\mydata.sas7bdat', , , 'F');   
rc = open('c:\data\mydata', , , 'F'); 
Tip If you use the F value, then the third argument, generation-number, is ignored.
Note If an argument is invalid, OPEN returns 0. You can obtain the text of the corresponding error message from the SYSMSG function. Invalid arguments do not produce a message in the SAS log and do not set the _ERROR_ automatic variable.

Details

The OPEN function opens a SAS data set, DATA step, or a SAS SQL view and returns a unique numeric data set identifier, which is used in most other data set access functions. OPEN returns 0 if the data set could not be opened.
If you call the OPEN function from a macro, then the result of the call is valid only when the result is passed to functions in a macro. If you call the OPEN function from the DATA step, then the result is valid only when the result is passed to functions in the same DATA step.
By default, a SAS data set is opened with a control level of RECORD. For more information, see CNTLLEV= Data Set Option in SAS Data Set Options: Reference.An open SAS data set should be closed when it is no longer needed. If you open a data set within a DATA step, it will be closed automatically when the DATA step ends.
OPEN defaults to the strongest access mode available in the engine. That is, if the engine supports random access, OPEN defaults to random access. Otherwise, data sets are opened with sequential access, and a system-level warning is set.

Example

  • This example opens the data set PRICES in the library MASTER using INPUT mode. Note that in a macro statement you do not enclose character strings in quotation marks.
    %let dsid=%sysfunc(open(master.prices,i)); 
    %if (&dsid = 0) %then    
         %put %sysfunc(sysmsg()); 
    %else    
         %put PRICES data set has been opened;
  • This example passes values from macro or DATA step variables to be used on data set options. It opens the data set SASUSER.HOUSES, and uses the WHERE= data set option to apply a permanent WHERE clause. Note that in a macro statement you do not enclose character strings in quotation marks.
    %let choice = style="RANCH";
    %let dsid=%sysfunc(open(sasuser.houses
                           (where=(&choice)),i));
  • This example shows how to check the returned value for errors and to write an error message from the SYSMSG function.
    data _null_; 
       d=open('bad','?');
       if not d then do;
          m=sysmsg();
          put m;
          abort;
       end;
       ... more SAS statements ...;
    run;

See Also