Previous Page | Next Page

Functions and CALL Routines

MOPEN Function



Opens a file by directory ID and member name, and returns either the file identifier or a 0.
Category: External Files
See: MOPEN Function under UNIX OpenVMS z/OS

Syntax
Arguments
Details
Examples
See Also

Syntax

MOPEN(directory-id,member-name<,open-mode<,record-length<,record-format>>>)


Arguments

directory-id

is a numeric variable that specifies the identifier that was assigned when the directory was opened, generally by the DOPEN function.

member-name

is a character constant, variable, or expression that specifies the member name in the directory.

open-mode

is a character constant, variable, or expression that 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 operating environment option in the FILENAME statement or function. If no operating environment 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
record-length

is a numeric variable, constant, or expression that 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.

record-format

is a character constant, variable, or expression that specifies a new record format for the file. To use the existing record format, do not specify a value here. The following values are valid:

B

specifies that data is to be interpreted as binary data.

D

specifies the default record format.

E

specifies the record format that you can edit.

F

specifies that the file contains fixed-length records.

P

specifies that the file contains printer carriage control in operating environment-dependent record format.

V

specifies that the file contains variable-length records.

Note:   If an argument is invalid, then MOPEN 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.   [cautionend]


Details

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.

CAUTION:
Use OUTPUT mode with care.

Opening an existing file for output might 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 operating environment 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 no operating environment option is specified with the FILENAME function, the output mode implies that the record be replaced.

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, folder, 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]


Examples

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
   %do;
      %let frstname = 
         %sysfunc(dread(&did,1));
      %let fid = 
         %sysfunc(mopen(&did,&frstname,i,0,d));
      macro statements to process the member

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


See Also

Functions:

DCLOSE Function

DNUM Function

DOPEN Function

DREAD Function

FCLOSE Function

FILENAME Function

FOPEN Function

FPUT Function

FWRITE Function

SYSMSG Function

Previous Page | Next Page | Top of Page