List all obs in a data set where more than 2 words is in a variable?

Accepted Solution Solved
Reply
Contributor
Posts: 68
Accepted Solution

List all obs in a data set where more than 2 words is in a variable?

Hi guys,

By using findw function, only one word can be found.

FINDW(string, word, chars, modifiers <, startpos>)

what function can help me find more than 2 words?

Thank you in advance.


Accepted Solutions
Solution
‎09-17-2014 10:16 AM
Super User
Super User
Posts: 6,845

Re: List all obs in a data set where more than 2 words is in a variable?

Just use normal boolean logic.

data want ;

set have ;

if indexw(upcase(comment),'PATIENT')

and indexw(upcase(comment,'ANTIBIOTICS')

;

run;

View solution in original post


All Replies
Respected Advisor
Posts: 4,137

Re: List all obs in a data set where more than 2 words is in a variable?

countw() if it's just about finding strings with more than 2 words.

find() if you're looking for a sub-string made up of 2 specific words.

A regular expression using prxmatch() for more complex text patterns.

Contributor
Posts: 68

Re: List all obs in a data set where more than 2 words is in a variable?

Thank you for letting me know prxmatch(). It do search certain Patterns, but it seems can not search two substrings according to my learning just acquired from SAS(R) 9.3 Functions and CALL Routines: Reference

Super User
Super User
Posts: 6,845

Re: List all obs in a data set where more than 2 words is in a variable?

Depends what you mean.  If you want to search for a "word" that contains a space then it works just fine.

3587  data have ;

3588    str='ONE TWO THREE FOUR';

3589    x=findw(str,'TWO THREE');

3590    put x=;

3591  run;

x=5

Super User
Super User
Posts: 7,720

Re: List all obs in a data set where more than 2 words is in a variable?

If its just two words separated by a space then my suggestion would be index, tiny fraction faster and less code:

data have ;

     str='ONE TWO THREE FOUR';

     x=index(str,' ');    

     put x=;

run;

Contributor
Posts: 68

Re: List all obs in a data set where more than 2 words is in a variable?

Sorry, I didn't make my question clear.

I'm not looking for a substring with 2 words, but 2 or more substrings with 1 or more words.

For example, I want to search obs which contain the word of Patient and antibiotics in the variable Comment and obs which contain the word of patient and high stress in the variable Comment. Which function should I use?

Thank you so much!

                                 comment

Patient has had a persistent cough for 3 weeks
Patient placed on beta-blockers on 7/1/2006
Patient has been on antibiotics for 10 days
Patient advised to lose some weight
This patient is always under high stress
Refer this patient to mental health for evaluation
Super User
Super User
Posts: 7,720

Re: List all obs in a data set where more than 2 words is in a variable?

Well, you can try index:

if index(comment,"Patient")>0 and index(comment,"antibiotics")>0 then ...

You will find it cumbersome if you have lots of different matches to make though.  You could also use regular expressions to the same effect.

You could look at putting your matches in a datastep then generating the code:

data matches;

     matcha="Patient"; matchb="antibiotics"; output;

run;

data _null_;

     set matches end=last;

     if _n_=1 then call execute('data want; set have;');

     call execute(' if index(comment,"'||strip(matcha)||'")>0 and index(comment,"'||strip(matchb)||'" then found=1;');

     if last then call execute(';run;');

run;

Contributor
Posts: 68

Re: List all obs in a data set where more than 2 words is in a variable?

Sorry, I didn't make my question clear.

I'm not looking for a substring with 2 words, but 2 or more substrings with 1 or more words.

For example, I want to search obs which contain the word of Patient and antibiotics in the variable Comment and obs which contain the word of patient and high stress in the variable Comment. Which function should I use?

Thank you so much!

                                                                          comment

1001Mayo Clinic10/21/2006120787Patient has had a persistent cough for 3 weeks
2003HMC09/01/2006166588Patient placed on beta-blockers on 7/1/2006
3002Mayo Clinic10/01/2006210689Patient has been on antibiotics for 10 days
4004HMC11/11/2006288889Patient advised to lose some weight
5007Mayo Clinic05/01/2006180547This patient is always under high stress
6050HMC07/06/200619960123Refer this patient to mental health for evaluation
Solution
‎09-17-2014 10:16 AM
Super User
Super User
Posts: 6,845

Re: List all obs in a data set where more than 2 words is in a variable?

Just use normal boolean logic.

data want ;

set have ;

if indexw(upcase(comment),'PATIENT')

and indexw(upcase(comment,'ANTIBIOTICS')

;

run;

🔒 This topic is solved and locked.

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

Discussion stats
  • 8 replies
  • 335 views
  • 3 likes
  • 4 in conversation