Chapter Contents




Opens a file by directory id and member name, and returns the file identifier or a 0

Category: External Files

See Also




specifies the identifier that was assigned when the directory was opened, generally by the DOPEN function.

specifies the member name in the directory.

specifies the type of access to the file:
A APPEND mode allows writing new records after the current end of the file.
I INPUT mode allows reading only. (Default)
O OUTPUT mode defaults to the OPEN mode specified in the host option in the FILENAME statement or function. If no host option is specified, it allows writing new records at the beginning of the file.
S Sequential input mode is used for pipes and other sequential devices such as hardware ports.
U UPDATE mode allows both reading and writing.
W Sequential update mode is used for pipes and other sequential devices such as ports.
Default: I

specifies a new logical record length for the file. To use the existing record length for the file, specify a length of 0 or do not provide a value here.

specifies a new record format for the file. To use the existing record format, do not specify a value here. Valid values are:
B specifies that data is to be interpreted as binary data.
D specifies the default record format.
E specifies the editable record format.
F specifies that the file contains fixed-length records.
P specifies that the file contains printer carriage control in host-dependent record format.
V specifies that the file contains variable-length records.


MOPEN returns the identifier for the file, or 0 if the file could not be opened. You can use a file-id that is returned by the MOPEN function as you would use a file-id returned by the FOPEN function.

Use OUTPUT mode with care. Opening an existing file for output may overwrite the current contents of the file without warning.  [cautionend]

The member is identified by directory-id and member-name instead of by a fileref. You can also open a directory member by using FILENAME to assign a fileref to the member, followed by a call to FOPEN. However, when you use MOPEN, you do not have to use a separate fileref for each member.

If the file already exists, the output and update modes default to the host option (append or replace) specified with the FILENAME statement or function. For example,

%let rc=%sysfunc(filename(file,physical-name,,mod));
%let did=%sysfunc(dopen(&file));
%let fid=%sysfunc(mopen(&did,member-name,o,0,d));
%let rc=%sysfunc(fput(&fid,This is a test.));
%let rc=%sysfunc(fwrite(&fid));
%let rc=%sysfunc(fclose(&fid));

If 'file' already exists, FWRITE appends the new record instead of writing it at the beginning of the file. However, if there is no a host option specified with the FILENAME function, the output mode implies that the record be replace.

If the open fails, use SYSMSG to retrieve the message text.

Operating Environment Information:   The term directory in this description refers to an aggregate grouping of files that are managed by the operating environment. Different host operating environments identify such groupings with different names, such as directory, subdirectory, MACLIB, or partitioned data set. For details, see the SAS documentation for your operating environment.

Opening a directory member for output or append is not possible in some operating environments.  [cautionend]


This example assigns the fileref MYDIR to a directory. Then it opens the directory, determines the number of members, retrieves the name of the first member, and opens that member. The last three arguments to MOPEN are the defaults. Note that in a macro statement you do not enclose character strings in quotation marks.

%let filrf=mydir;
%let rc=%sysfunc(filename(filrf,physical-name));
%let did=%sysfunc(dopen(&filrf));
%let frstname=' ';
%let memcount=%sysfunc(dnum(&did));
%if (&memcount > 0) %then
      %let frstname = 
      %let fid = 
      macro statements to process the member

      %let rc=%sysfunc(fclose(&fid));
   %put %sysfunc(sysmsg());
%let rc=%sysfunc(dclose(&did));

See Also



Chapter Contents



Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.