From 73fd85d254ab5da7ed7d4378897c8c0fce04becd Mon Sep 17 00:00:00 2001 From: 4gl <@> Date: Tue, 28 Apr 2026 12:37:56 +0100 Subject: [PATCH] feat: new mfv_getcaslib macro Fetches a caslib from a regular SAS libref --- tests/viyaonly/mfv_getcaslib.test.sas | 69 +++++++++++++++++++++++++++ viya/mfv_getcaslib.sas | 37 ++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 tests/viyaonly/mfv_getcaslib.test.sas create mode 100644 viya/mfv_getcaslib.sas diff --git a/tests/viyaonly/mfv_getcaslib.test.sas b/tests/viyaonly/mfv_getcaslib.test.sas new file mode 100644 index 0000000..da70f0e --- /dev/null +++ b/tests/viyaonly/mfv_getcaslib.test.sas @@ -0,0 +1,69 @@ +/** + @file + @brief Testing mfv_getcaslib macro function + +

SAS Macros

+ @li mfv_getcaslib.sas + @li mp_assert.sas + @li mp_assertscope.sas + +**/ + +options mprint; + +/* ------------------------------------------------------------------------ */ +/* Setup: start a CAS session and assign caslibs */ +/* ------------------------------------------------------------------------ */ +cas mysess; +caslib _all_ assign; + +%let testcaslib=Public; + +libname castest cas caslib=&testcaslib; + +/* ------------------------------------------------------------------------ */ +%put TEST 1 - returns the caslib name for a valid CAS libref; +/* ------------------------------------------------------------------------ */ +%mp_assert( + iftrue=(%mfv_getcaslib(castest)=%upcase(&testcaslib)), + desc=Check correct caslib name returned for a valid CAS libref +) + + +/* ------------------------------------------------------------------------ */ +%put TEST 2 - returns empty for a non-CAS libref (WORK); +/* ------------------------------------------------------------------------ */ +%mp_assert( + iftrue=(%mfv_getcaslib(WORK)=), + desc=Check empty string returned for a non-CAS libref +) + + +/* ------------------------------------------------------------------------ */ +%put TEST 3 - returns empty for a libref that does not exist; +/* ------------------------------------------------------------------------ */ +%mp_assert( + iftrue=(%mfv_getcaslib(DOESNOTEXIST)=), + desc=Check empty string returned for a non-existent libref +) + + +/* ------------------------------------------------------------------------ */ +%put TEST 5 - no scope leakage into global macro variables; +/* ------------------------------------------------------------------------ */ +%mp_assertscope(SNAPSHOT) + +%let _rc=%mfv_getcaslib(castest); + +%mp_assertscope(COMPARE, + desc=Check mfv_getcaslib does not leak macro variables into GLOBAL scope, + ignorelist=_RC +) + + +/* ------------------------------------------------------------------------ */ +/* Teardown */ +/* ------------------------------------------------------------------------ */ +cas mysess terminate; + + diff --git a/viya/mfv_getcaslib.sas b/viya/mfv_getcaslib.sas new file mode 100644 index 0000000..e95693f --- /dev/null +++ b/viya/mfv_getcaslib.sas @@ -0,0 +1,37 @@ +/** + @file mfv_getcaslib.sas + @brief Returns the CAS caslib name for a given SAS libref + @details Pure macro function. Reads sashelp.vlibnam and returns + the sysvalue where sysname='Caslib' for the given libref. This + is useful when the caslib name and libref name may differ. + + Usage: + + %put %mfv_getcaslib(lib=PUBLIC); + + @param [in] lib SAS libref for which to return the CAS caslib name + + @return Returns the CAS caslib name, or empty string if not found + +**/ + +%macro mfv_getcaslib(lib); + +%local dsid rc result; + +%let dsid=%sysfunc(open(sashelp.vlibnam( + where=(libname="%upcase(&lib)" and sysname="Caslib") +))); + +%if &dsid %then %do; + %let rc=%sysfunc(fetch(&dsid)); + %if &rc=0 %then + %let result=%sysfunc( + getvarc(&dsid,%sysfunc(varnum(&dsid,SYSVALUE))) + ); + %let rc=%sysfunc(close(&dsid)); +%end; + +&result + +%mend mfv_getcaslib;