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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 14920 views
  • 10 likes
  • 3 in conversation