Macros for SAS Application Developers
https://github.com/sasjs/core
mf_wordsinstr1butnotstr2.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Returns words that are in string 1 but not in string 2
4  @details Compares two space separated strings and returns the words that are
5  in the first but not in the second.
6 
7  Note - case sensitive!
8 
9  Usage:
10 
11  %let x= %mf_wordsInStr1ButNotStr2(
12  Str1=blah sss blaaah brah bram boo
13  ,Str2= blah blaaah brah ssss
14  );
15 
16  returns:
17  > sss bram boo
18 
19  @param [in] str1= () String containing words to extract
20  @param [in] str2= () Used to compare with the extract string
21 
22  @version 9.2
23  @author Allan Bowe
24 
25 **/
26 
27 %macro mf_wordsInStr1ButNotStr2(
28  Str1= /* string containing words to extract */
29  ,Str2= /* used to compare with the extract string */
30 )/*/STORE SOURCE*/;
31 
32 %local count_base count_extr i i2 extr_word base_word match outvar;
33 %if %length(&str1)=0 or %length(&str2)=0 %then %do;
34  %put base string (str1)= &str1;
35  %put compare string (str2) = &str2;
36  %return;
37 %end;
38 %let count_base=%sysfunc(countw(&Str2));
39 %let count_extr=%sysfunc(countw(&Str1));
40 
41 %do i=1 %to &count_extr;
42  %let extr_word=%scan(&Str1,&i,%str( ));
43  %let match=0;
44  %do i2=1 %to &count_base;
45  %let base_word=%scan(&Str2,&i2,%str( ));
46  %if &extr_word=&base_word %then %let match=1;
47  %end;
48  %if &match=0 %then %let outvar=&outvar &extr_word;
49 %end;
50 
51  &outvar
52 
53 %mend mf_wordsInStr1ButNotStr2;
54