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.
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
What is your logic ? How do you define it as "Import" or as "domestic" ?
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.
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;
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
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.