BookmarkSubscribeRSS Feed
a_khedekar
Calcite | Level 5

data custnm;
infile datalines;
input name $50.;
datalines;
ABC AMERICAN REALTY
ABC MARKETING
ABC YOGA
;
run;

data kwds;
infile datalines;
input keyword $15.;
datalines;
ABC
AMERICAN
;
run;

I want to match flag to return 2 since two keyword matched for first record and return 1 for one keyword match.

Let me know if anyone has any solutions around it. 

3 REPLIES 3
ballardw
Super User

How many total keywords do you have in your list? There are some approaches that may work for short lists that don't work for longer ones.

 

Should embedded strings "match"? Example: Does keyword "ABC" match value "ABCDE"?

Are your matches case sensitive? You show everything in upper case but what if you have a value of "Abc". Should that match "ABC" or not.

What if the same string appears more than once in the value? Would a value of "ABC MARKETING ABC PRODUCTS" have ABC counted as 1 or 2?

CurtisMackWSIPP
Lapis Lazuli | Level 10

Here is a hash solution.  Harder to code, but should scale much better.

 

data matches(keep=name keywords);
  if _n_ = 1 then do;
	 declare hash kwds(dataset:"kwds");
	 kwds.defineKey("keyword");
	 kwds.defineDone();
  end;
  set custnm;
  keywords = 0;
  delims = ' ';     
  numWords = countw(name, delims);  /* for each line of text, how many words? */
  do i = 1 to numWords;            /* split text into words */
     keyword = scan(name, i, delims);
     if kwds.check() eq 0 then keywords = keywords + 1;
  end;
run; 
CurtisMackWSIPP
Lapis Lazuli | Level 10

There is a hash approach that would be best, but this is a simple to understand example.

 

proc sql;
  create table matches as 
  select name,count(*) as keywords
  from custnm, kwds
  where findw(name,strip(keyword)) > 0
  group by name;
quit;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 3 replies
  • 420 views
  • 0 likes
  • 3 in conversation