BookmarkSubscribeRSS Feed
TT_T
Calcite | Level 5

I need to compare two strings from two macro variables, best in proc sql.

 

StringA = Cars Dog Cat

StringB = Cars House Model Fish

 

If like in the example Dog and Cat didnt hit in StringB it has to be removed, so that

StringA = Cars or %let NewString = Cars

If nothing hits than %put something.

 

Is there a way or a function?

7 REPLIES 7
rudfaden
Lapis Lazuli | Level 10

This might work

 

%let StringA = Cars Dog Cat;
%let StringB = Cars House Model Fish;

%macro findWords;
	%let remove=&stringA;
	%do i=1 %to %sysfunc(countw(&stringA));
		%let word=%scan(&stringB,&i);
		%let remove=%sysfunc(tranwrd(&remove, &word,));
	%end;
	%let StringA=%sysfunc(tranwrd(&StringA, &remove,));
	%put remove=&remove;
	%put stringA=&StringA;
	%put stringB=&stringB;
%mend;
%findWords;
TT_T
Calcite | Level 5

Thxs for the replie.

 

It can be that StringA = House Dog Cat Cars;

For that it doesnt work, but thxs for your idea. I will try on with your solution.

rudfaden
Lapis Lazuli | Level 10

Why does it not work with 

House Dog Cat Cars

This:

 

%let StringA = House Dog Cat Cars;
%let StringB = Cars House Model Fish;

%macro findWords;
	%let remove=&stringA;
	%do i=1 %to %sysfunc(countw(&stringA));
		%let word=%scan(&stringB,&i);
		%let remove=%sysfunc(tranwrd(&remove, &word,));
	%end;
	%let StringA=%sysfunc(tranwrd(&StringA, &remove,));
	%put remove=&remove;
	%put stringA=&StringA;
	%put stringB=&stringB;
%mend;
%findWords;

returns 

 

remove=Dog Cat
stringA=House   Cars
stringB=Cars House Model Fish
Tom
Super User Tom
Super User

That is a good first step, but for this type of problem you normally have to worry about tokens that are wholly contained in other tokens. 

What if one or more of the two listst include both Car and Cartoon?  How would you adjust your solution to handle that situation?

 

Tom
Super User Tom
Super User

Loop over the words in one of the list. Check if the word is in the other list. If so add it to the resulting string.

%let StringA = Cars Dog Cat;
%let StringB = Cars House Model Fish;
%let Expect = Cars ;

%macro both(a,b);
  %local i return word;
  %do i=1 %to %sysfunc(countw(&a,%str( )));
    %let word=%scan(&a,&i,%str( ));
    %if %sysfunc(findw(&b,&word,,s)) %then %let return=&return &word ;
  %end;
&return.
%mend both;

%put %both(&stringa,&stringb);
Astounding
PROC Star

Because of the issue that @Tom mentioned, this can become complex and involve %SYSFUNC and FINDW.  Instead, I prefer to pad each string with blanks and use %INDEX:

 

%let StringA = Cars Dog Cat;
%let StringB = Cars House Model Fish;
%let NewString = ;

%macro findwords;
   %local k nextword;
   %do k=1 %to %sysfunc(countw(&StringA));
      %let nextword = %scan(&StringA, &k);
      %if  %index(%str( &StringB ), %str( &nextword ))
      %then %let NewString = &NewString &nextword;
   %end;
%mend findwords;
%findwords
%put Matches found:  &NewString;

It's untested code so might need a minor tweak.

TT_T
Calcite | Level 5

Thxs for the replies. It´s a bit tricky because there are ongoing action´s, i will try to implement some of your answer´s. Thxs

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1004 views
  • 0 likes
  • 4 in conversation