BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lillymaginta
Lapis Lazuli | Level 10

I want to return any observation that contains the word "med" in any place in a variable

 

id   desc

1   one medication at a time

2   two medication at a time

3   none 

 

 

output 

 

2

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

If I may be so bold as to suggest an improvement to the code

 

if find(desc,'med','i')>0;

This finds the letters med regardless of whether they are uppercase or lowercase.

--
Paige Miller

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20
data have;
input ID   desc & $30. ;
cards;
1   one medication at a time
2   two medication at a time
3   none    
;

data want;
set have;
if find(desc,'med')>0;
run;
PaigeMiller
Diamond | Level 26

If I may be so bold as to suggest an improvement to the code

 

if find(desc,'med','i')>0;

This finds the letters med regardless of whether they are uppercase or lowercase.

--
Paige Miller
novinosrin
Tourmaline | Level 20

@PaigeMiller wrote:

If I may be so bold as to suggest an improvement to the code

 


Of course stat champ sir. i always am in awe of your quantitative acumen on stats domain. Fall qtr starts tomorrow as PCA, logistic will go full throttle.Will reach out for guidance and help on that thread https://communities.sas.com/t5/SAS-Statistical-Procedures/Requesting-guidance-for-Principal-componen.... 🙂 have a great evening

lillymaginta
Lapis Lazuli | Level 10

Thank you, the upper/lower case addition is much needed! 

ChrisNZ
Tourmaline | Level 20

> I want to return any observation that contains the word "med" in any place in a variable

"med" is not a word in your example, just a substring. "medication" is a word.

Looking for words is different than looking for strings. Be careful and exact when phrasing your questions.

s_lassen
Meteorite | Level 14

Another possibility is to use a WHERE clause. May be faster:

data want;
  set have;
  where desc like '%med%';
run;

Or, if you want both upper and lowercase:

data want;
  set have;
  where upcase(desc) like '%MED%';
run;

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1618 views
  • 4 likes
  • 5 in conversation