BookmarkSubscribeRSS Feed
windlove
Fluorite | Level 6

I would like to search a string variable only contains certain words but not anything else. 

 

The data would look like:

 

Osteoarthritis (OA)
OA (Osteoarthritis)
OA Knee
Osteoarthritis Knee
OA + hands
Osteoarthritis, hands
OA , hip
OA
Knee OA

So I would like to keep the rows containing OA/Osteoarthritis, or OA/Osteo with knee, but get rid of everything else. 

OA/Osteo with knee is easy to keep, but I am in difficulty to keep those only having word OA/Osteoarthritis. 

The variable is quite long and very messy, so impossible to list those I want to delete. 

Thanks

 

2 REPLIES 2
art297
Opal | Level 21

Given the example you posted, which records do you want to keep?

 

Art, CEO, AnalystFinder.com

 

ballardw
Super User

When describing character searches you must be very explicit as character variables may contain much junk.

For instance you say you want to keep "rows containing OA/Osteoarthritis"

NONE of your example data shows OA/Osteoarthritis (the / character ). Do you mean the string values OA and Osteoarthritis appearing as words (meaning OA by itself not as part of Osteoarthritis or in the word oak)?

 

For OA/Osteo  similar with the added bit of Osteo not appearing in compound? But when you say with knee, since none of your example shows Osteo do you mean "OA" or "Osteo" and "Knee"?

Since your example data shows no instance of "Osteo" were you being lazy and not spelling "Osteoarthritis" completely? Spelling counts!

 

And will any of the words of interest be case sensitive: Find "Knee" but exclude "knee" (or vice versa)?

 

You know what you intend but computers are stupid and must be given very explicit instructions. Without those explicit rules we could make some guesses but might very well miss some conditions that seem obvious to you but we are not mind readers.

 

Here is a suggestion based on guesses.

assuming you want to match Knee or knee or kNee and similar

data want;
   set have;
   if (findw(str,'OA',' (),:;','i')>0 and findw(str,'OSTEOARTHRITIS',' (),:;','i' ))
      or 
      ( (findw(str,'OA',' (),:;','i') or findw(str,'OSTEOARTHRITIS',' (),:;','i') ) 
         AND findw(str,'KNEE',' (),:;','i') )
      
   ;
run;

where STR is the name of your variable with the text to search.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 367 views
  • 0 likes
  • 3 in conversation