Macros for SAS Application Developers
https://github.com/sasjs/core
mf_dedup.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief de-duplicates a macro string
4  @details Removes all duplicates from a string of words. A delimeter can be
5  chosen. Is inspired heavily by this excellent [macro](
6  https://github.com/scottbass/SAS/blob/master/Macro/dedup_mstring.sas) from
7  [Scott Base](https://www.linkedin.com/in/scottbass). Case sensitive.
8 
9  Usage:
10 
11  %let str=One two one two and through and through;
12  %put %mf_dedup(&str);
13  %put %mf_dedup(&str,outdlm=%str(,));
14 
15  Which returns:
16 
17  > One two one and through
18  > One,two,one,and,through
19 
20  @param [in] str String to be deduplicated
21  @param [in] indlm= ( ) Delimeter of the input string
22  @param [out] outdlm= ( ) Delimiter of the output string
23 
24  <h4> Related Macros </h4>
25  @li mf_trimstr.sas
26 
27  @version 9.2
28  @author Allan Bowe
29 **/
30 
31 %macro mf_dedup(str
32  ,indlm=%str( )
33  ,outdlm=%str( )
34 )/*/STORE SOURCE*/;
35 
36 %local num word i pos out;
37 
38 %* loop over each token, searching the target for that token ;
39 %let num=%sysfunc(countc(%superq(str),%str(&indlm)));
40 %do i=1 %to %eval(&num+1);
41  %let word=%scan(%superq(str),&i,%str(&indlm));
42  %let pos=%sysfunc(indexw(&out,&word,%str(&outdlm)));
43  %if (&pos eq 0) %then %do;
44  %if (&i gt 1) %then %let out=&out%str(&outdlm);
45  %let out=&out&word;
46  %end;
47 %end;
48 
49 %unquote(&out)
50 
51 %mend mf_dedup;
52 
53