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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 13309 views
  • 10 likes
  • 3 in conversation