diff --git a/all.sas b/all.sas index baef181..349bdb3 100644 --- a/all.sas +++ b/all.sas @@ -1805,6 +1805,60 @@ Usage: %exit_success: %mend mf_verifymacvars; +/** + @file + @brief Returns words that are in both string 1 and string 2 + @details Compares two space separated strings and returns the words that are + in both. + Usage: + + %put %mf_wordsInStr1andStr2( + Str1=blah sss blaaah brah bram boo + ,Str2= blah blaaah brah ssss + ); + + returns: + > blah blaaah brah + + @param str1= string containing words to extract + @param str2= used to compare with the extract string + + @warning CASE SENSITIVE! + + @version 9.2 + @author Allan Bowe + +**/ + +%macro mf_wordsInStr1andStr2( + Str1= /* string containing words to extract */ + ,Str2= /* used to compare with the extract string */ +)/*/STORE SOURCE*/; + +%local count_base count_extr i i2 extr_word base_word match outvar; +%if %length(&str1)=0 or %length(&str2)=0 %then %do; + %put %str(WARN)ING: empty string provided!; + %put base string (str1)= &str1; + %put compare string (str2) = &str2; + %return; +%end; +%let count_base=%sysfunc(countw(&Str2)); +%let count_extr=%sysfunc(countw(&Str1)); + +%do i=1 %to &count_extr; + %let extr_word=%scan(&Str1,&i,%str( )); + %let match=0; + %do i2=1 %to &count_base; + %let base_word=%scan(&Str2,&i2,%str( )); + %if &extr_word=&base_word %then %let match=1; + %end; + %if &match=1 %then %let outvar=&outvar &extr_word; +%end; + + &outvar + +%mend mf_wordsInStr1andStr2; + /** @file @brief Returns words that are in string 1 but not in string 2 diff --git a/base/mf_wordsinstr1andstr2.sas b/base/mf_wordsinstr1andstr2.sas new file mode 100644 index 0000000..1abadbd --- /dev/null +++ b/base/mf_wordsinstr1andstr2.sas @@ -0,0 +1,54 @@ +/** + @file + @brief Returns words that are in both string 1 and string 2 + @details Compares two space separated strings and returns the words that are + in both. + Usage: + + %put %mf_wordsInStr1andStr2( + Str1=blah sss blaaah brah bram boo + ,Str2= blah blaaah brah ssss + ); + + returns: + > blah blaaah brah + + @param str1= string containing words to extract + @param str2= used to compare with the extract string + + @warning CASE SENSITIVE! + + @version 9.2 + @author Allan Bowe + +**/ + +%macro mf_wordsInStr1andStr2( + Str1= /* string containing words to extract */ + ,Str2= /* used to compare with the extract string */ +)/*/STORE SOURCE*/; + +%local count_base count_extr i i2 extr_word base_word match outvar; +%if %length(&str1)=0 or %length(&str2)=0 %then %do; + %put %str(WARN)ING: empty string provided!; + %put base string (str1)= &str1; + %put compare string (str2) = &str2; + %return; +%end; +%let count_base=%sysfunc(countw(&Str2)); +%let count_extr=%sysfunc(countw(&Str1)); + +%do i=1 %to &count_extr; + %let extr_word=%scan(&Str1,&i,%str( )); + %let match=0; + %do i2=1 %to &count_base; + %let base_word=%scan(&Str2,&i2,%str( )); + %if &extr_word=&base_word %then %let match=1; + %end; + %if &match=1 %then %let outvar=&outvar &extr_word; +%end; + + &outvar + +%mend mf_wordsInStr1andStr2; + diff --git a/tests/crossplatform/mf_wordsinstr1andstr2.test.sas b/tests/crossplatform/mf_wordsinstr1andstr2.test.sas new file mode 100644 index 0000000..38a3c55 --- /dev/null +++ b/tests/crossplatform/mf_wordsinstr1andstr2.test.sas @@ -0,0 +1,20 @@ +/** + @file + @brief Testing mf_wordsinstr1andstr2 macro + +

SAS Macros

+ @li mf_wordsinstr1andstr2.sas + @li mp_assert.sas + +**/ + +%let x=%mf_wordsinstr1andstr2(str1=xx DOLLAR x $CHAR xxx W MONNAME + ,str2=DOLLAR $CHAR W MONNAME xxxxxx +); +%mp_assert( + iftrue=( + "&x"="DOLLAR $CHAR W MONNAME" + ), + desc=Checking basic string, + outds=work.test_results +) diff --git a/tests/crossplatform/mf_wordsinstr1butnotstr2.test.sas b/tests/crossplatform/mf_wordsinstr1butnotstr2.test.sas new file mode 100644 index 0000000..48fd6fc --- /dev/null +++ b/tests/crossplatform/mf_wordsinstr1butnotstr2.test.sas @@ -0,0 +1,20 @@ +/** + @file + @brief Testing mf_wordsinstr1butnotstr2 macro + +

SAS Macros

+ @li mf_wordsinstr1butnotstr2.sas + @li mp_assert.sas + +**/ + +%let x=%mf_wordsinstr1butnotstr2(str1=xx DOLLAR x $CHAR xxx W MONNAME + ,str2=ff xx x xxx xxxxxx +); +%mp_assert( + iftrue=( + "&x"="DOLLAR $CHAR W MONNAME" + ), + desc=Checking basic string, + outds=work.test_results +)