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
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;
data want;
set new;
if upcase(strip(str))='PHONE';
run;
FINDW to find the WORD phone ignoring case.
Sorry, I had thought case was important from the OP, hence I took the delimiter approach rather than string search.
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
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;
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
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;
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 = " " ?
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.