BookmarkSubscribeRSS Feed
yashpande
Obsidian | Level 7

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

10 REPLIES 10
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

stat_sas
Ammonite | Level 13

data want;

set new;

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

run;

data_null__
Jade | Level 19

FINDW to find the WORD phone ignoring case.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

yashpande
Obsidian | Level 7

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

Patrick
Opal | Level 21

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;

Jagadishkatam
Amethyst | Level 16

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

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;

HHZ
Calcite | Level 5 HHZ
Calcite | Level 5

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 = " " ?

Cruise
Ammonite | Level 13
worked like a charm. i used your suggestion with macro variable with no problem

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
  • 10 replies
  • 11260 views
  • 1 like
  • 8 in conversation