From 8b5238230b04e47ed6b468cbcd92d39b98ea7a86 Mon Sep 17 00:00:00 2001 From: munja Date: Mon, 13 Dec 2021 18:36:22 +0000 Subject: [PATCH] feat: new macro to determine if a macro variable value is an integer - mf_isint (and associated test) --- all.sas | 32 ++++++++++++++++++++++++++ base/mf_isint.sas | 33 +++++++++++++++++++++++++++ tests/crossplatform/mf_isint.test.sas | 33 +++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 base/mf_isint.sas create mode 100644 tests/crossplatform/mf_isint.test.sas diff --git a/all.sas b/all.sas index 1fa3ba6..65a1ebf 100644 --- a/all.sas +++ b/all.sas @@ -1340,6 +1340,38 @@ Usage: &is_directory %mend mf_isdir;/** + @file + @brief Returns 1 if the variable contains only digits 0-9, else 0 + @details Note that numerics containing any punctuation (including decimals + or exponents) will be flagged zero. + + If you'd like support for this, then do raise an issue (or even better, a + pull request!) + + Usage: + + %put %mf_isint(1) returns 1; + %put %mf_isint(1.1) returns 0; + %put %mf_isint(%str(1,1)) returns 0; + + @param [in] arg input value to check + + @version 9.2 +**/ + +%macro mf_isint(arg +)/*/STORE SOURCE*/; + /* remove minus sign if exists */ + + %local val; + %if "%substr(%str(&arg),1,1)"="-" %then %let val=%substr(%str(&arg),2); + %else %let val=&arg; + + /* check remaining chars */ + %if %sysfunc(findc(%str(&val),,kd)) %then %do;0%end; + %else %do;1%end; + +%mend mf_isint;/** @file @brief Returns physical location of various SAS items @details Returns location of the PlatformObjectFramework tools diff --git a/base/mf_isint.sas b/base/mf_isint.sas new file mode 100644 index 0000000..0cb7f87 --- /dev/null +++ b/base/mf_isint.sas @@ -0,0 +1,33 @@ +/** + @file + @brief Returns 1 if the variable contains only digits 0-9, else 0 + @details Note that numerics containing any punctuation (including decimals + or exponents) will be flagged zero. + + If you'd like support for this, then do raise an issue (or even better, a + pull request!) + + Usage: + + %put %mf_isint(1) returns 1; + %put %mf_isint(1.1) returns 0; + %put %mf_isint(%str(1,1)) returns 0; + + @param [in] arg input value to check + + @version 9.2 +**/ + +%macro mf_isint(arg +)/*/STORE SOURCE*/; + /* remove minus sign if exists */ + + %local val; + %if "%substr(%str(&arg),1,1)"="-" %then %let val=%substr(%str(&arg),2); + %else %let val=&arg; + + /* check remaining chars */ + %if %sysfunc(findc(%str(&val),,kd)) %then %do;0%end; + %else %do;1%end; + +%mend mf_isint; \ No newline at end of file diff --git a/tests/crossplatform/mf_isint.test.sas b/tests/crossplatform/mf_isint.test.sas new file mode 100644 index 0000000..7d2bfae --- /dev/null +++ b/tests/crossplatform/mf_isint.test.sas @@ -0,0 +1,33 @@ +/** + @file + @brief Testing mf_isint macro + +

SAS Macros

+ @li mf_isint.sas + @li mp_assert.sas + +**/ + +%mp_assert( + iftrue=( + "%mf_isint(1)"="1" + ), + desc=Checking basic mf_isint(1), + outds=work.test_results +) + +%mp_assert( + iftrue=( + "%mf_isint(1.1)"="0" + ), + desc=Checking basic mf_isint(1.1), + outds=work.test_results +) + +%mp_assert( + iftrue=( + "%mf_isint(-1)"="1" + ), + desc=Checking mf_isint(-1), + outds=work.test_results +) \ No newline at end of file