|
|||
|
Hi John,
You got a TRANWRD solution before me ... damn! So I've got to think of another way ... How about the non-Perl RE (I've only got V8 at work), but it not too bad for something like this. data test2; set one; retain rx; if _n_ eq 1 then rx = rxparse("(cheese|pizza|pear)"); if rxmatch(rx,string) then put 'Found ' string; run; Matches regardless of case (I think). ++ Guido >From: John McQuown <John.McQuown@OTNNET.COM> >Reply-To: John McQuown <John.McQuown@OTNNET.COM> >To: SAS-L@LISTSERV.UGA.EDU >Subject: Re: check if a string contains a list of substrings >Date: Thu, 14 Jul 2005 17:22:00 -0700 > >Just for fun, here's a recursive solution: > data one(drop=test); > length string test $50; > input string $50.; > >test=tranwrd(tranwrd(tranwrd(string,'cheese','@') ,'pizza','@'),'pear','@ >'); > put / @1 string= / @3 test=; > if index(test,'@'); > datalines; > I like pizza, cheese cake and apple > I hate pears, beer and ice cream > I really like steak, potatoes and wine > I wine if anyone tries to serve me fish > I like to drink wine when playing parcheese > ; > run; > > proc print data=one; run; > >Which produces this output: > Obs string > 1 I like pizza, cheese cake and apple > 2 I hate pears, beer and ice cream > 3 I like to drink wine when playing parcheese > >It's almost Friday. Cheers, john > > >-----Original Message----- >From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of >Sigurd Hermansen >Sent: Thursday, July 14, 2005 4:28 PM >To: SAS-L@LISTSERV.UGA.EDU >Subject: Re: check if a string contains a list of substrings > >Heeha: >Try: >data test; > string='I like pizza, cheese cake and apple'; > strList="'coffee','cheese cake','tea'"; > do str=strList; > if index(string,str) then match+1; > end; > if match=1 then put 'match '; > else put 'no match '; >run; > >One can SET string and strList from a SAS dataset or INPUT values from a >file. Note that the INDEX() function does not match strings with >internal blanks to subsets of strings containing internal blanks. >Sig > >-----Original Message----- >From: owner-sas-l@listserv.uga.edu [mailto wner-sas-l@listserv.uga.edu]>On Behalf Of heeha@canada.com >Sent: Thursday, July 14, 2005 6:39 PM >To: SAS-L@LISTSERV.UGA.EDU >Subject: check if a string contains a list of substrings > > >Dear SAS users, > >How can I do it in SAS if I want to check if a string >contains a list of words of interest? > >For example, string='I like pizza, cheese cake and >apple'. I want to tell if the string contains at least >one of the 'cheese', 'pizza' or 'pear'. In this >example, the answer would be yes. > >Thanks, > >KC |
|
|
||||
|
||||
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Re: check if a string contains a list of substrings | toby dunn | Newsgroup comp.soft-sys.sas | 0 | 07-15-2005 02:21 PM |
| Re: check if a string contains a list of substrings | Sigurd Hermansen | Newsgroup comp.soft-sys.sas | 1 | 07-15-2005 12:42 PM |
| Re: check if a string contains a list of substrings | John McQuown | Newsgroup comp.soft-sys.sas | 0 | 07-15-2005 12:22 AM |
| Re: check if a string contains a list of substrings | Ashok | Newsgroup comp.soft-sys.sas | 0 | 07-14-2005 11:59 PM |
| Re: check if a string contains a list of substrings | Arthur Tabachneck | Newsgroup comp.soft-sys.sas | 0 | 07-14-2005 11:04 PM |