DATA Step, Macro, Functions and more

Find the exact match in string

Reply
Contributor
Posts: 43

Find the exact match in string

Hi All,

I am searching for keyword phone .. it could be case insensitive

I have data like

data new;

input number 8. str $20;

cards;

1 phone

2 Phone_id

3 Phone_num

4 Phone_new

5 Phone

;

run;

I want output as

1 phone

5 Phone

I used Find function and Index function but its producing me all observations. I want exact and case insensitive match. How can it be done.

Many Thanks

Super User
Super User
Posts: 7,942

Re: Find the exact match in string

Posted in reply to yashpande

Hi,

Can you clarify why you want 5 specifically, is it the last value only you want, if so:

data want;

  set new;

  new_str=scan(str,1,"_");

run;

data want;

  set want;

  by new_str notsorted;

  if last.new_str then output;

run;

Trusted Advisor
Posts: 1,228

Re: Find the exact match in string

Posted in reply to yashpande

data want;

set new;

if upcase(strip(str))='PHONE';

run;

Respected Advisor
Posts: 3,799

Re: Find the exact match in string

Posted in reply to yashpande

FINDW to find the WORD phone ignoring case.

Super User
Super User
Posts: 7,942

Re: Find the exact match in string

Posted in reply to data_null__

Sorry, I had thought case was important from the OP, hence I took the delimiter approach rather than string search.

Contributor
Posts: 43

Re: Find the exact match in string

Posted in reply to yashpande

can we use the Findw ? But somehow i am not getting correct answer

Lets say if my data is something like this

data new;

input number 8. str $200;

cards;

1 phone is not present

2 Phone_id is not present

3 Phone_num is present

4 Phone_new is absent

5 Phone is present

6 PHONE is also there

7 pHone is there

8 phone_num  and phone is not there

9 phone_num is present

;

run;

Output should be

1 phone is not present

5 Phone is present

6 PHONE is also there

7 pHone is there

8 phone_num  and phone is not there

I am really confused .. what to use and what not. Can we use PRXMATCH for same task

Respected Advisor
Posts: 4,173

Re: Find the exact match in string

Posted in reply to yashpande

Once you read in the data correctly using findw() works just fine.

data new;

input number :8. str :$200.;

if findw(str,'phone',1,' ','i') then output;

cards;

1 phone is not present

2 Phone_id is not present

3 Phone_num is present

4 Phone_new is absent

5 Phone is present

6 PHONE is also there

7 pHone is there

8 phone_num  and phone is not there

9 phone_num is present

;

run;

Trusted Advisor
Posts: 1,137

Re: Find the exact match in string

Posted in reply to yashpande

Yes PRXmatch is a good choice \b matches the exact word and i ignores the case

Please try

data new;

infile cards truncover;

input number  str $200.;

if prxmatch('/\bphone\b/i',str)>0;

cards;

1 phone is not present

2 Phone_id is not present

3 Phone_num is present

4 Phone_new is absent

5 Phone is present

6 PHONE is also there

7 pHone is there

8 phone_num and phone is not there

9 phone_num is present

;

run;

Thanks,

Jag

Thanks,
Jag
Super User
Super User
Posts: 7,942

Re: Find the exact match in string

Posted in reply to Jagadishkatam

I think your response, and Patrick above it, both do the same as posted before, just finding any instance of the word Phone with any capitilisation.  The OP clearly shows that he only wants one record per instance of the word per the casing present.

OP, did you try the code I presented.  It assumes that you want the latest in sequence of where a particular capitilisation exists, however you will need to update it to cover spaces as well (as that wasn't in the original test data):

data want;

  set new;

  new_str=scan(str,1," _");  /* This here takes the first set of characters up until the underscore or blank, i.e. the first word in any capitilisation */

run;

/* This step then works on that first word, outputting the last instance of that string (including upcase or lowcase varieties) */

/* You may actually want to sort the dataset by this word, and the id variable */

data want;

  set want;

  by new_str notsorted;

  if last.new_str then output;

run;

New Contributor HHZ
New Contributor
Posts: 2

Re: Find the exact match in string

Just some suggestions, not tested by coding:

Can you first create two additional variables, VAR1 and VAR2 to hold the first word and the rest words of "str" respectively; then standardize the VAR1 by using function "upcase". After doing this, chose based on two conditions, (1) VAR1 = "PHONE" and (2) VAR2 = " " ?

Frequent Contributor
Posts: 137

Re: Find the exact match in string

Posted in reply to Jagadishkatam
worked like a charm. i used your suggestion with macro variable with no problem
Ask a Question
Discussion stats
  • 10 replies
  • 2600 views
  • 1 like
  • 8 in conversation