Published on :
Administration INTERNAL_CREATION

Fileref and File Manipulation

This code is also available in: Deutsch Français
Awaiting validation
Attention : This code requires administrator privileges.
The script proceeds in several steps. It begins by checking for the existence of a fileref ('curdirfl') that is not yet assigned. Then, it assigns a file path ('c:\tmp333') to this fileref. It re-checks the fileref's existence, then the existence of the physical file the fileref points to. The script decides to open the file in write mode ('o') if it does not exist, or in read mode ('i') if it already exists. After opening, it retrieves and displays various information about the file using the `foptnum`, `foptname`, and `finfo` functions. Finally, it closes the file and deletes the fileref.
Data Analysis

Type : INTERNAL_CREATION


The script does not depend on pre-existing external data. It manipulates a file named 'c:\tmp333' that it attempts to create or open. All processed data (fileref/file existence, file information) is generated or retrieved internally by SAS functions.

1 Code Block
DATA STEP Data
Explanation :
This `DATA` block uses several SAS file manipulation functions:
- `fexist('fileref')` : Checks if a fileref is assigned. Returns 0 if not, 1 if yes.
- `filename('fileref', 'path')` : Assigns a physical path to a fileref. Returns 0 on success.
- `fileref('fileref')` : Checks the status of a fileref and the file it points to. A negative value indicates that the fileref exists but not the file; zero indicates that both exist.
- `fileexist('path')` : Checks the existence of a physical file. Returns 1 if yes, 0 if not.
- `fopen('fileref', 'mode')` : Opens the file associated with the fileref in a specified mode ('i' for input, 'o' for output). Returns a non-zero file identifier (FID) on success.
- `foptnum(fid)` : Returns the number of information options available for an open file.
- `foptname(fid, index)` : Returns the name of a specific information option by its index.
- `finfo(fid, 'option_name')` : Returns the value of the specified information for the open file.
- `fclose(fid)` : Closes the file identified by the FID. Returns 0 on success.
- `fdelete('fileref')` : Deletes the fileref assignment. Returns 0 on success.
The script prints the results of each step (`put rc=;`, `put fid=;` etc.) to illustrate the behavior of the functions. The path 'c:\tmp333' is a Windows file path, but the functions remain compatible with SAS Viya 4 on Linux operating systems, by adjusting the path accordingly (e.g., `/tmp/tmp333`).
Copied!
1DATA a;
2 
3/* Does fileref "curdirfl" exist? No = 0 */
4 
5rc=fexist ("curdirfl");
6put;
7put "Fileref curdirfl exist? rc should be 0 (no); " rc=;
8 
9/* assign fileref */
10 
11rc=filename("curdirfl", "c:\tmp333");
12 
13/* RC=0 indicates success in assigning fileref */
14
15put "Fileref assigned - rc should be 0; " rc=;
16rc=fexist ("curdirfl");
17 
18/* Does file which "curdirfl" points to exist? No = 0 */
19/* Assigning a fileref doesn't create the file. */
20
21put "File still doesn't exist - rc should be 0; " rc=;
22rc=fileref ("curdirfl");
23 
24/* Does fileref "curdirfl" exist? */
25/* Negative means fileref exists, but file does not */
26/* Positive means fileref does not exist */
27/* Zero means both fileref and file exist */
28 
29put "Fileref now exists - rc should be negative; " rc=;
30put;
31 
32/* Does the file that the fileref points to exist? Should be no. */
33
34if ( fileexist ("./tmp333") ) then
35 /* if it does, open it for input */
36 do;
37 put "Open file for INPUT";
38 fid=fopen ("curdirfl", "i") ;
39 end;
40 else /* most likely scenario */
41 do;
42 put "Open file for OUTPUT";
43 fid=fopen ("curdirfl", "o");
44 end;
45 
46/* fid should be non-zero. 0 indicates failure. */
47put "File id is: " fid=;
48numopts = foptnum(fid);
49put "Number of information items should be 6; " numopts=;
50do i = 1 to numopts;
51optname = foptname (fid,i);
52put i= optname=;
53optval = finfo (fid, optname);
54put optval= ;
55end;
56rc=fclose (fid);
57rc=fdelete ("curdirfl");
58put "Closing the file, rc should be 0; "
59rc=; RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Banner
Expert Advice
Expert
Michael
Responsable de l'infrastructure Viya.
« Programmatic file manipulation through Data Step functions offers a much higher level of control than standard INFILE or FILE statements. This script perfectly illustrates the crucial distinction between a fileref (the logical pointer) and the physical file (the actual entity on the disk). »