BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
chouchou
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

8 REPLIES 8
Patrick
Opal | Level 21

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.

chouchou
Calcite | Level 5

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

Tom
Super User Tom
Super User

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

chouchou
Calcite | Level 5

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
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

chouchou
Calcite | Level 5

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
Tom
Super User Tom
Super User

Just use normal boolean logic.

data want ;

set have ;

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

and indexw(upcase(comment,'ANTIBIOTICS')

;

run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 8 replies
  • 752 views
  • 3 likes
  • 4 in conversation