Hi,
suppsoe that i ave the following data:
description |
A car |
1 car 23j @ |
Car |
cars |
2 trucks |
One truck |
One truck and one car |
What I would like to have is to create a new data set where in the description either "car" or "truck" is present, so the new data table is:
description |
A car |
1 car 23j @ |
Car |
One truck |
One truck and one car |
Thnak you!
Yes there is way to put the list into the function directly rather than passing each word, for this you need to create a macro variable which carries these words. Also it depends on the function which you are going to use.
For example, as i mentioned in the my previous reply about the prxmatch which could be used for checking the words, this function requires the words to be checked separated by pipe '|' so when you create the macro variables you need to have the words separated by pipe.
please try something like below
proc sql;
select distinct words into : word separated by '|' from have;
quit;
data have;
set want;
if prxmatch('m/\b(&word)\b/i',description)>0;
run;
Use IF FINDW(description, "car", " ", "i") > 0 OR FINDW(description, "truck", " ", "i") > 0;
Alternatively you could use the perl regular expression like
prxmatch('m/(car|truck)\b/i',description)>0
@Jagadishkatam you would need a word boundary before the words too 'm/\b(car|truck)\b/i', otherwise you will match anything like "I was struck by lightning"
Hi,
thank you for replying!!!
Just another small extension of the question, if I have a list of words, say 10, is it possible to put the list instead of each word individually?
Thnka you!
/*
Yes, You can in this code (word,"car","truck","ANYTHINHYOUWANT); you specify which word you need to find
find
dataiwant created a new dataset with the result;
*/
data have dataiwant;
input word$ 30.;
matchingword = find(word,"car","truck");
if matchingword > 0 then output dataiwant;
drop matchingword;
datalines;
A car
1 car 23j @
Car
cars
2 trucks
One truck
One truck and one car
;
run;
Proc Print data = dataiwant;
run;
That is not the right syntax for the FIND() function.
Yes there is way to put the list into the function directly rather than passing each word, for this you need to create a macro variable which carries these words. Also it depends on the function which you are going to use.
For example, as i mentioned in the my previous reply about the prxmatch which could be used for checking the words, this function requires the words to be checked separated by pipe '|' so when you create the macro variables you need to have the words separated by pipe.
please try something like below
proc sql;
select distinct words into : word separated by '|' from have;
quit;
data have;
set want;
if prxmatch('m/\b(&word)\b/i',description)>0;
run;
Don't you need to use double quotes to have the macro variable reference resolve?
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.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.