BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DangIT
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

4 REPLIES 4
Ksharp
Super User

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

DangIT
Fluorite | Level 6

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

Ksharp
Super User

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 13381 views
  • 10 likes
  • 3 in conversation