SAS Component Language Dictionary |
Category: | Directory |
Syntax | |
Details | |
Example | |
See Also |
Syntax |
file-id=MOPEN(directory-id,member-name<open-mode <,record-length<,record-format>>>); |
contains 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 just as you would use a file-id returned by the FOPEN function.
is the identifier that was returned by DOPEN when the directory was opened. If directory-id is invalid, the program halts.
is the name of a file in the directory that is identified by directory-id.
is the type of access to the file:
is the logical record length of the file. To use the existing record length for the file, specify a length of 0 or do not provide a value here.
is the record format of the file:
'B' | |
'D' | |
'E' | |
'F' | |
'P' |
The file contains printer carriage-control characters in a host-dependent record format. |
'V' |
To use the existing record format, do not specify a value here.
Details |
Opening an existing file for output may overwrite the current contents of the file without warning.
The member file 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, using MOPEN saves you from having to use a separate fileref for each member.
If the file already exists and is opened with an open-mode of O, then the output mode defaults to either APPEND or REPLACE, based on host-options that were specified in the FILENAME function or statement. For example:
rc=filename('file',filename,' ','mod'); fid=fopen('file','o'); rc=fput(fid,'This is a test.'); rc=fwrite(fid); rc=fclose(fid);
If FILE already exists, then FWRITE appends a new record. However, if no host option was specified with the FILENAME function, then FWRITE writes the value at the beginning of the file, which could replace an existing value.
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 managed by the host operating system. Different host operating systems identify such groupings with different names, such as directory, subdirectory, MACLIB, or partitioned data set. See the SAS documentation for your operating environment for details.
Opening a directory member for output is not possible on some operating systems.
Example |
Assign the fileref MYDIR to a directory. Then open the directory, determine the number of members, retrieve the name of the first member, and open that member. The last three arguments to MOPEN are the defaults.
rc=filename('mydir','filename'); did=dopen('mydir'); frstname=' '; memcount=dnum(did); if (memcount>0) then do; frstname=dread(did,1); fid=mopen(did,frstname,'i',0,'d'); ...SCL statements to process the member... rc=fclose(fid); end; else _msg_=sysmsg(); rc=dclose(did);
See Also |
Copyright © 2009 by SAS Institute Inc., Cary, NC, USA. All rights reserved.