mirror of
https://github.com/sasjs/core.git
synced 2025-12-11 06:24:35 +00:00
feat: mf_dedup and associated test
This commit is contained in:
54
base/mf_dedup.sas
Normal file
54
base/mf_dedup.sas
Normal file
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
@file
|
||||
@brief de-duplicates a macro string
|
||||
@details Removes all duplicates from a string of words. A delimeter can be
|
||||
chosen. Is inspired heavily by this excellent [macro](
|
||||
https://github.com/scottbass/SAS/blob/master/Macro/dedup_mstring.sas) from
|
||||
[Scott Base](https://www.linkedin.com/in/scottbass). Case sensitive.
|
||||
|
||||
Usage:
|
||||
|
||||
%let str=One two one two and through and through;
|
||||
%put %mf_dedup(&str);
|
||||
%put %mf_dedup(&str,outdlm=%str(,));
|
||||
|
||||
Which returns:
|
||||
|
||||
> One two one and through
|
||||
> One,two,one,and,through
|
||||
|
||||
@param [in] str String to be deduplicated
|
||||
@param [in] indlm= ( ) Delimeter of the input string
|
||||
@param [out] outdlm= ( ) Delimiter of the output string
|
||||
|
||||
<h4> Related Macros </h4>
|
||||
@li mf_trimstr.sas
|
||||
@li mf_wordsinstr1butnotstr2.sas
|
||||
|
||||
@version 9.2
|
||||
@author Allan Bowe
|
||||
**/
|
||||
|
||||
%macro mf_dedup(str
|
||||
,indlm=%str( )
|
||||
,outdlm=%str( )
|
||||
)/*/STORE SOURCE*/;
|
||||
|
||||
%local num word i pos out;
|
||||
|
||||
%* loop over each token, searching the target for that token ;
|
||||
%let num=%sysfunc(countc(%superq(str),%str(&indlm)));
|
||||
%do i=1 %to %eval(&num+1);
|
||||
%let word=%scan(%superq(str),&i,%str(&indlm));
|
||||
%let pos=%sysfunc(indexw(&out,&word,%str(&outdlm)));
|
||||
%if (&pos eq 0) %then %do;
|
||||
%if (&i gt 1) %then %let out=&out%str(&outdlm);
|
||||
%let out=&out&word;
|
||||
%end;
|
||||
%end;
|
||||
|
||||
%unquote(&out)
|
||||
|
||||
%mend mf_dedup;
|
||||
|
||||
|
||||
23
tests/crossplatform/mf_dedup.test.sas
Normal file
23
tests/crossplatform/mf_dedup.test.sas
Normal file
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
@file
|
||||
@brief Testing mf_dedup macro
|
||||
|
||||
<h4> SAS Macros </h4>
|
||||
@li mf_dedup.sas
|
||||
@li mp_assert.sas
|
||||
|
||||
**/
|
||||
|
||||
%let str=One two one two and through and through;
|
||||
|
||||
%mp_assert(
|
||||
iftrue=("%mf_dedup(&str)"="One two one and through"),
|
||||
desc=Basic test,
|
||||
outds=work.test_results
|
||||
)
|
||||
|
||||
%mp_assert(
|
||||
iftrue=("%mf_dedup(&str,outdlm=%str(,))"="One,two,one,and,through"),
|
||||
desc=Outdlm test,
|
||||
outds=work.test_results
|
||||
)
|
||||
Reference in New Issue
Block a user