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
Pyrite | Level 9

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
Pyrite | Level 9

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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