DATA Step, Macro, Functions and more

Extract specie word from string

Accepted Solution Solved
Reply
Contributor
Posts: 65
Accepted Solution

Extract specie word from string

Hi I have a table with a comment box I need to extract key words from comment box Theft. Or Storm
The data looks like this

Id. Comment
1A. GL THE BOAT STORM
2A. SUNSHINE
3A. STORM IN THE CITY
4A. THEFT. IN THE JUNGLE
5A. RAINY DAY
6A. THEFT

Output


Id. Comment. Output
1A. GL THE BOAT STORM. Storm
2A. SUNSHINE
3A. STORM IN THE CITY. Storm
4A. THEFT. IN THE JUNGLE. Theft
5A. RAINY DAY
6A. THEFT. Theft

Accepted Solutions
Solution
‎03-31-2017 02:02 PM
Contributor
Posts: 55

Re: Extract specie word from string

You can use the find function to do this.

 

data have;
  set want;
  if find(comment,"storm","i")>0 then output = "Storm";
  else if find(comment,"theft","i")>0 then output = "Theft";
run;

This assumes you will only ever find one or the other. If it is possible to have both, you would have to check for both simultaneously and concatenate your results.

View solution in original post


All Replies
Solution
‎03-31-2017 02:02 PM
Contributor
Posts: 55

Re: Extract specie word from string

You can use the find function to do this.

 

data have;
  set want;
  if find(comment,"storm","i")>0 then output = "Storm";
  else if find(comment,"theft","i")>0 then output = "Theft";
run;

This assumes you will only ever find one or the other. If it is possible to have both, you would have to check for both simultaneously and concatenate your results.

Contributor
Posts: 65

Re: Extract specie word from string

I will test it's one or the other ... thanks for response
Valued Guide
Posts: 632

Re: Extract specie word from string

[ Edited ]

The IFC function can also be used to consolidate.  In this case i choose to look for either or both.

data have;
input Id $ @5 Comment $30.;
datalines;
1A. GL THE BOAT STORM
2A. SUNSHINE
3A. STORM IN THE CITY
4A. THEFT. IN THE JUNGLE
5A. RAINY DAY
6A. THEFT
7A. THEFT IN A STORM
run;
data want;
   set have;
   output=catx(',',ifc(index(comment,'STORM'),'storm',' '),ifc(index(comment,'THEFT'),'theft',' '));
run;
Super User
Posts: 5,081

Re: Extract specie word from string

An issue to consider:  do you want the exact word only, or variations such as THEFTS, RAINSTORM, STORMY ...

 

Depending on your answer, you might switch from FIND to FINDW.

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 155 views
  • 2 likes
  • 4 in conversation