Macros for SAS Application Developers
https://github.com/sasjs/core
mf_wordsinstr1andstr2.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Returns words that are in both string 1 and string 2
4  @details Compares two space separated strings and returns the words that are
5  in both.
6  Usage:
7 
8  %put %mf_wordsInStr1andStr2(
9  Str1=blah sss blaaah brah bram boo
10  ,Str2= blah blaaah brah ssss
11  );
12 
13  returns:
14  > blah blaaah brah
15 
16  @param [in] str1= () string containing words to extract
17  @param [in] 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_wordsInStr1andStr2(
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 base string (str1)= &str1;
34  %put compare string (str2) = &str2;
35  %return;
36 %end;
37 %let count_base=%sysfunc(countw(&Str2));
38 %let count_extr=%sysfunc(countw(&Str1));
39 
40 %do i=1 %to &count_extr;
41  %let extr_word=%scan(&Str1,&i,%str( ));
42  %let match=0;
43  %do i2=1 %to &count_base;
44  %let base_word=%scan(&Str2,&i2,%str( ));
45  %if &extr_word=&base_word %then %let match=1;
46  %end;
47  %if &match=1 %then %let outvar=&outvar &extr_word;
48 %end;
49 
50  &outvar
51 
52 %mend mf_wordsInStr1andStr2;
53