Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mfv_existfile.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Checks whether a file exists in SAS Drive
4 @details Returns 1 if the file exists, and 0 if it doesn't. Works by
5 attempting to assign a fileref with the filesrvc engine. When the file is
6 not found the filename function sets syscc to a non zero value, so the macro
7 resets syscc to zero and returns 0.
8
9 A non zero syscc on entry means an earlier step has already failed. The macro
10 logs a %str(ERR)OR: line and returns 0 without touching syscc, so a caller
11 comparing the result against 1 or 0 stays valid, and can tell this case from a
12 genuine miss by checking syscc, which a miss resets to zero.
13
14 Usage:
15
16 %put %mfv_existfile(/does/exist.txt);
17 %put %mfv_existfile(/does/not/exist.txt);
18
19 @param [in] filepath The full path to the file on SAS drive
20 (eg /Public/myfile.txt)
21
22 <h4> SAS Macros </h4>
23 @li mf_getuniquefileref.sas
24
25 <h4> Related Macros </h4>
26 @li mfv_existfolder.sas
27
28 @version 3.5
29 @author [Allan Bowe](https://www.linkedin.com/in/allanbowe/)
30**/
31
32%macro mfv_existfile(filepath
33)/*/STORE SOURCE*/;
34
35 %local fref rc path name;
36
37 %if &syscc ne 0 %then %do;
38 %put %str(ERR)OR: mfv_existfile.sas cannot execute with syscc=&syscc;
39 0
40 %return;
41 %end;
42
43 %let fref=%mf_getuniquefileref();
44 %let name=%scan(&filepath,-1,/);
45 %let path=%substr(&filepath,1,%length(&filepath)-%length(&name)-1);
46
47 %if %sysfunc(filename(fref,,filesrvc,folderPath="&path" filename="&name"))=0
48 %then %do;
49 %sysfunc(fexist(&fref))
50 %let rc=%sysfunc(filename(fref));
51 %end;
52 %else %do;
53 0
54 %let syscc=0;
55 %end;
56
57%mend mfv_existfile;