Production Ready 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 Usage:
7
8 %let x= %mf_wordsInStr1ButNotStr2(
9 Str1=blah sss blaaah brah bram boo
10 ,Str2= blah blaaah brah ssss
11 );
12
13 returns:
14 > sss bram boo
15
16 @param str1= string containing words to extract
17 @param str2= used to compare with the extract string
18
19 @warning CASE SENSITIVE!
20
21 @version 9.2
22 @author Allan Bowe
23
24**/
25
26%macro mf_wordsInStr1ButNotStr2(
27 Str1= /* string containing words to extract */
28 ,Str2= /* used to compare with the extract string */
29)/*/STORE SOURCE*/;
30
31%local count_base count_extr i i2 extr_word base_word match outvar;
32%if %length(&str1)=0 or %length(&str2)=0 %then %do;
33 %put %str(WARN)ING: empty string provided!;
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