Help using Base SAS procedures

Searching a string variable for various words

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

Searching a string variable for various words

I have a lookup table of product names (around 200)

 

For example,

  • BARKAT
  • CALOGEN
  • CALSHAKE
  • etc.

and I would like to search each of these products through all of my observations. 

I am not sure how to go about this as I am still new to SAS...

I need to do some sort of a scan as the product names do not exactly match up.

 

For example, when I search for these names in the productname variable, I would like to flag things like...

 

  • Barkat brown rice
  • Calogen extra
  • Calogen extra shots
  • Calshake
  • etc.

 

I made a cntlin list of the product names, and tried to use indexw/findw, but these seem to require individual words. My list is up to 200, so a manual search is not an option. I wonder if there is a way to do this?


Accepted Solutions
Solution
‎02-28-2017 09:45 AM
Super User
Super User
Posts: 7,430

Re: Searching a string variable for various words

This isn't the best topic to be starting to learn with if your new to SAS.  

As for how to do it, there are a few ways, the way I would go with (and you have not provided any test data in the form of datasteps so this is only an example) - assumes listofwords contains the words, and have contains one string variable called sentance:

data _null_;
  set listofwords end=last;
  if _n_=1 then call execute('data want; set have; length wordsfound $2000;');
  call execute('if findw(sentance,"'||strip(word)||'") >0 then wordsfound=catx(",",wordsfound,"'||strip(word)||'");');
  if last then call execute('run;');
run;

What this does is create the datastep code after this is executed, which has an if statement for each word in listofwords, with a catx() function call to add any words found to wordsfound variable.  

 

As mentioned this is advanced, not a good area for learning as the above probably doesn't make any sense to you.

View solution in original post


All Replies
Solution
‎02-28-2017 09:45 AM
Super User
Super User
Posts: 7,430

Re: Searching a string variable for various words

This isn't the best topic to be starting to learn with if your new to SAS.  

As for how to do it, there are a few ways, the way I would go with (and you have not provided any test data in the form of datasteps so this is only an example) - assumes listofwords contains the words, and have contains one string variable called sentance:

data _null_;
  set listofwords end=last;
  if _n_=1 then call execute('data want; set have; length wordsfound $2000;');
  call execute('if findw(sentance,"'||strip(word)||'") >0 then wordsfound=catx(",",wordsfound,"'||strip(word)||'");');
  if last then call execute('run;');
run;

What this does is create the datastep code after this is executed, which has an if statement for each word in listofwords, with a catx() function call to add any words found to wordsfound variable.  

 

As mentioned this is advanced, not a good area for learning as the above probably doesn't make any sense to you.

Occasional Contributor
Posts: 18

Re: Searching a string variable for various words

Thank you this absolutely worked, but as you say I will need to look into the code to understand it fully. Thank you for your time.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 129 views
  • 1 like
  • 2 in conversation