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.
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?
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;
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;
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!
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.
Ready to level-up your skills? Choose your own adventure.