From 186ea1cfbaeee1df3e85dcf53b4083fd8d95e71a Mon Sep 17 00:00:00 2001 From: allan Date: Thu, 2 Apr 2026 23:45:09 +0100 Subject: [PATCH] feat: adding ISLIB validation to mp_validatecol macro --- base/mp_validatecol.sas | 16 +++++++++++++++- tests/base/mp_validatecol.test.sas | 26 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/base/mp_validatecol.sas b/base/mp_validatecol.sas index 4b85f30..aae4eaa 100644 --- a/base/mp_validatecol.sas +++ b/base/mp_validatecol.sas @@ -1,6 +1,6 @@ /** @file - @brief Used to validate variables in a dataset + @brief Used to validate values in a data step @details Useful when sanitising inputs, to ensure that they arrive with a certain pattern. Usage: @@ -27,6 +27,7 @@ @param [in] incol The column to be validated @param [in] rule The rule to apply. Current rules: @li ISINT - checks if the variable is an integer + @li ISLIB - checks if the value is a valid libref (NOT whether it exists) @li ISNUM - checks if the variable is numeric @li LIBDS - matches LIBREF.DATASET format @li FORMAT - checks if the provided format is syntactically valid @@ -65,6 +66,19 @@ else &outcol=1; drop &tempcol; %end; +%else %if &rule=ISLIB %then %do; + if _n_=1 then do; + retain &tempcol; + &tempcol=prxparse('/^[_a-z]\w{0,7}$/i'); + if missing(&tempcol) then do; + putlog 'ERR' +(-1) "OR: Invalid expression for ISLIB"; + stop; + end; + drop &tempcol; + end; + if prxmatch(&tempcol, trim(&incol)) then &outcol=1; + else &outcol=0; +%end; %else %if &rule=LIBDS %then %do; /* match libref.dataset */ if _n_=1 then do; diff --git a/tests/base/mp_validatecol.test.sas b/tests/base/mp_validatecol.test.sas index 766752e..1f381e3 100644 --- a/tests/base/mp_validatecol.test.sas +++ b/tests/base/mp_validatecol.test.sas @@ -129,4 +129,30 @@ run; desc=Test4 - ISFORMAT, test=EQUALS 6, outds=work.test_results +) + +/** + * Test 5 - ISLIB + */ +data test5; + infile datalines4 dsd; + input; + inf=_infile_; + %mp_validatecol(inf,ISLIB,islib) + if islib=1; +datalines4; +some +!lib +%abort +definite +2fail +nineletrs +.failalso +_valid +;;;; +run; +%mp_assertdsobs(work.test5, + desc=Testing ISLIB, + test=EQUALS 3, + outds=work.test_results ) \ No newline at end of file