DATA Step, Macro, Functions and more

Searching a string for a list of words

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 81
Accepted Solution

Searching a string for a list of words

Hi,

I'm having troubles figuring out what function I should use to search a string variable for a number of words, any help greatly appreciated!

I have a table of website descriptions

ID SiteDesc

1 acura car dealer

2 acura used car dealer

3 toyota deals

4 deals for acura

5 chevy car sales

For records 1-4 i'd like to create a category variable with value of "Import"

For record 5, the category would be "domestic"

I have a list of 10 manufacturers for each category, i'm not sure what the best way to create this classification by looping through the string for each manufacturer?

Thanks for your help.


Accepted Solutions
Solution
‎05-11-2015 08:45 AM
Super User
Posts: 10,041

Re: Searching a string for a list of words

Good for PRX.

data have;
  length text $20;
  id=1; text="acura car dealer"; output;
  id=2; text="acura used car dealer"; output;
  id=3; text="toyota deals"; output;
  id=4; text="deals for acura"; output;
  id=5; text="chevy car sales"; output;
run;
data want;
 set have;
 length type $20;
 if prxmatch('/acura|toyota|hyundai|kia|honda/i',text) then type='Import';
  else if prxmatch('/ford|chevy|mercury|gm|jeep|pontiac/i',text) then type='Domestic';
run;

Xia Keshan

View solution in original post


All Replies
Super User
Posts: 10,041

Re: Searching a string for a list of words

What is your logic ? How do you define it as "Import" or as "domestic" ?

Frequent Contributor
Posts: 81

Re: Searching a string for a list of words

I have a list of manufacturers for each category.

For example:
For Import, if any of the following: acura, toyota, hyundai, kia, honda, etc.

For Domestic, if any of the following: ford, chevy, mercury, gm, jeep, pontiac etc.

Super User
Super User
Posts: 7,976

Re: Searching a string for a list of words

Well, you could do something like the below.  It makes several assumptions though, car is not in import and domestic for example.  E.g. what happens for:

acura and chevy car dealer

---

data list;

  type="Domestic";

  word="acura"; output;

  word="toyota"; output;

  type="Import";

  word="chevy"; output;

run;

data have;

  length type $20;

  id=1; text="acura car dealer"; output;

  id=2; text="acura used car dealer"; output;

  id=3; text="toyota deals"; output;

  id=4; text="deals for acura"; output;

  id=5; text="chevy car sales"; output;

run;

proc sql;

  update HAVE A

  set TYPE=(select distinct TYPE from LIST where index(lowcase(A.TEXT),lowcase(WORD))>0);  /* Note you could also do by merge */

quit;

Solution
‎05-11-2015 08:45 AM
Super User
Posts: 10,041

Re: Searching a string for a list of words

Good for PRX.

data have;
  length text $20;
  id=1; text="acura car dealer"; output;
  id=2; text="acura used car dealer"; output;
  id=3; text="toyota deals"; output;
  id=4; text="deals for acura"; output;
  id=5; text="chevy car sales"; output;
run;
data want;
 set have;
 length type $20;
 if prxmatch('/acura|toyota|hyundai|kia|honda/i',text) then type='Import';
  else if prxmatch('/ford|chevy|mercury|gm|jeep|pontiac/i',text) then type='Domestic';
run;

Xia Keshan

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 2177 views
  • 6 likes
  • 3 in conversation