From 42a16ef4963de41491b0195fdbc5be1dfe6472a1 Mon Sep 17 00:00:00 2001 From: Allan Bowe Date: Wed, 26 May 2021 23:36:43 +0300 Subject: [PATCH] feat: adding mfv_existfile.sas and tests --- tests/viya/mfv_existfile.test.sas | 34 ++++++++++++++++++++++ viya/mfv_existfile.sas | 48 +++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 tests/viya/mfv_existfile.test.sas create mode 100644 viya/mfv_existfile.sas diff --git a/tests/viya/mfv_existfile.test.sas b/tests/viya/mfv_existfile.test.sas new file mode 100644 index 0000000..6207426 --- /dev/null +++ b/tests/viya/mfv_existfile.test.sas @@ -0,0 +1,34 @@ +/** + @file + @brief Testing mfv_existfile macro function + +

SAS Macros

+ @li mf_uid.sas + @li mfv_existfile.sas + @li mp_assert.sas + @li mv_createfile.sas + +**/ + +options mprint sgen; + +%let file=%mf_uid(); + +/* create a folder */ +filename somefile temp; +data _null_; + file somefile; + put 'hello testings'; +run; +%mv_createfile(path=&mcTestAppLoc/temp, name=&file..txt,inref=somefile) + + +%mp_assert( + iftrue=(%mfv_existfile(&mcTestAppLoc/temp/&file..txt)=1), + desc=Check if created file exists +) + +%mp_assert( + iftrue=(%mfv_existfile(&mcTestAppLoc/temp/%mf_uid().txt)=0), + desc=Check if non created file does not exist +) \ No newline at end of file diff --git a/viya/mfv_existfile.sas b/viya/mfv_existfile.sas new file mode 100644 index 0000000..51359ba --- /dev/null +++ b/viya/mfv_existfile.sas @@ -0,0 +1,48 @@ +/** + @file + @brief Checks whether a file exists in SAS Drive + @details Returns 1 if the file exists, and 0 if it doesn't. Works by + attempting to assign a fileref with the filesrvc engine. If not found, the + syscc is automatically set to a non zero value - so in this case it is reset. + To avoid hiding issues, there is therefore a test at the start to ensure the + syscc is zero. + + Usage: + + %put %mfv_existfile(/does/exist.txt); + %put %mfv_existfile(/does/not/exist.txt); + + @param filepath The full path to the file on SAS drive (eg /Public/myfile.txt) + +

SAS Macros

+ @li mf_abort.sas + @li mf_getuniquefileref.sas + + @version 3.5 + @author [Allan Bowe](https://www.linkedin.com/in/allanbowe/) +**/ + +%macro mfv_existfile(filepath +)/*/STORE SOURCE*/; + + %mf_abort( + iftrue=(&syscc ne 0), + msg=Cannot enter mfv_existfile.sas with syscc=&syscc + ) + + %local fref rc path name; + %let fref=%mf_getuniquefileref(); + %let name=%scan(&filepath,-1,/); + %let path=%substr(&filepath,1,%length(&filepath)-%length(&name)-1); + + %if %sysfunc(filename(fref,,filesrvc,folderPath="&path" filename="&name"))=0 + %then %do; + %sysfunc(fexist(&fref)) + %let rc=%sysfunc(filename(fref)); + %end; + %else %do; + 0 + %let syscc=0; + %end; + +%mend mfv_existfile; \ No newline at end of file