diff --git a/base/mf_existfunction.sas b/base/mf_existfunction.sas new file mode 100644 index 0000000..937ced2 --- /dev/null +++ b/base/mf_existfunction.sas @@ -0,0 +1,33 @@ +/** + @file + @brief Checks if a function exists + @details Returns 1 if the function exists, else 0 + Usage: + + %put %mf_existfunction(CAT); + %put %mf_existfunction(DOG); + + Full credit to [Bart](https://sasensei.com/user/305) for the vfunc pointer + and the tidy approach for pure macro data set filtering. + Check out his [SAS Packages](https://github.com/yabwon/SAS_PACKAGES) + framework! + + @param [in] name (positional) - function name + + @author Allan Bowe +**/ +/** @cond */ + +%macro mf_existfunction(name +)/*/STORE SOURCE*/; + + %local dsid rc exist; + %let dsid=%sysfunc(open(sashelp.vfunc(where=(fncname="%upcase(&name)")))); + %let exist = %sysfunc(fetch(&dsid, NOSET)); + %let rc = %sysfunc(close(&dsid)); + + %sysevalf(0 = &exist) + +%mend mf_existfunction; + +/** @endcond */ \ No newline at end of file diff --git a/tests/crossplatform/mf_existfunction.test.sas b/tests/crossplatform/mf_existfunction.test.sas new file mode 100644 index 0000000..cd00373 --- /dev/null +++ b/tests/crossplatform/mf_existfunction.test.sas @@ -0,0 +1,22 @@ +/** + @file + @brief Testing mf_existfunction macro + +

SAS Macros

+ @li mf_existfunction.sas + @li mp_assert.sas + +**/ + +%mp_assert( + iftrue=(%mf_existfunction(CAT)=1), + desc=Checking if CAT function exists, + outds=work.test_results +) + +%mp_assert( + iftrue=(%mf_existfunction(DOG)=0), + desc=Checking DOG function does not exist, + outds=work.test_results +) +