Extract common words from two strings

This code is also available in: Deutsch Español Français
Difficulty Level
Beginner
Published on :
The `mf_wordsInStr1andStr2` macro takes two parameters, `Str1` and `Str2`, which are character strings. It compares the words in `Str1` with those in `Str2` and builds a new string containing only the words present in both strings. The comparison is case-sensitive. If one of the strings is empty, the macro displays a message in the log and returns nothing.
Data Analysis

Type : INTERNAL_CREATION


The macro processes character strings provided as parameters. It does not read or write traditional or external SAS data.

1 Code Block
MACRO
Explanation :
Definition of the `mf_wordsInStr1andStr2` macro with its input parameters `Str1` and `Str2`. Declaration of local macro variables used for word counting, loops, storing extracted words, and the final result.
Copied!
1%macro mf_wordsInStr1andStr2(
2 Str1= /* string containing words to extract */
3 ,Str2= /* used to compare with the extract string */
4)/*/STORE SOURCE*/;
5 
6%local count_base count_extr i i2 extr_word base_word match outvar;
2 Code Block
MACRO
Explanation :
This block checks if one of the input strings (`Str1` or `Str2`) is empty. If so, a message is displayed in the SAS log indicating the string values, and the macro terminates (`%return`) without producing any output.
Copied!
1%IF %LENGTH(&str1)=0 or %LENGTH(&str2)=0 %THEN %DO;
2 %put base string (str1)= &str1;
3 %put compare string (str2) = &str2;
4 %return;
5%END;
3 Code Block
MACRO
Explanation :
Use of the `%sysfunc(countw(...))` macro function to count the number of words in each input string (`Str1` and `Str2`) and store these counts in the `count_base` and `count_extr` macro variables.
Copied!
1%let count_base=%sysfunc(countw(&Str2));
2%let count_extr=%sysfunc(countw(&Str1));
3 
4 Code Block
MACRO
Explanation :
Outer loop (`%do i=1 %to &count_extr;`) to iterate through each word in `Str1`. For each word (`extr_word`), an inner loop (`%do i2=1 %to &count_base;`) iterates through all words in `Str2`. If a match is found (`&extr_word=&base_word`), the `match` variable is set to 1. If `match` is 1 after the inner loop, the word (`extr_word`) is added to the `outvar` macro variable, which accumulates the result.
Copied!
1%DO i=1 %to &count_extr;
2 %let extr_word=%scan(&Str1,&i,%str( ));
3 %let match=0;
4 %DO i2=1 %to &count_base;
5 %let base_word=%scan(&Str2,&i2,%str( ));
6 %IF &extr_word=&base_word %THEN %let match=1;
7 %END;
8 %IF &match=1 %THEN %let outvar=&outvar &extr_word;
9%END;
5 Code Block
MACRO
Explanation :
The macro returns the value of the `outvar` macro variable, which contains all common words found in `Str1` and `Str2`, separated by spaces. `%mend` marks the end of the macro definition.
Copied!
1 &outvar
2 
3%mend mf_wordsInStr1andStr2;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Copyright (c) 2001-2006 Rodney Sparapani (from _version.sas); Copyright © 2022, SAS Institute Inc. (from print_macro_parameters.sas); Copyright 2010-2023 HMS Analytical Software GmbH (from macro_without_brief_tag.sas, _delfile.json, _issuewarningmessage.json); Allan Bowe (mentioned in the HELP START/END block as a general reference).


Related Documentation

Aucune documentation spécifique pour cette catégorie.