Functions and CALL Routines |
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>>>) |
is a numeric variable that specifies the identifier that was assigned when the directory was opened, generally by the DOPEN function.
is a character constant, variable, or expression that specifies the member name in the directory.
is a character constant, variable, or expression that specifies the type of access to the file:
Default: | I |
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.
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 | |
D | |
E | |
F | |
P |
specifies that the file contains printer carriage control in operating environment-dependent record format. |
V |
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.
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.
Opening an existing file for output might overwrite the current contents of the file without warning.
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.
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 |
|
Copyright © 2011 by SAS Institute Inc., Cary, NC, USA. All rights reserved.