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

good day,

 

i am using function index for searching the keyword from my data but some error happen and here is the example

 

NAME

Liverpool

Liverpoollowolo

club Liverpool

 

data want;

data have;

if index(NAME,"Liverpool") then output;

run;

 

The final data will contain these three names but the second (Liverpoollowolo) is not the result i want. how can i fix this?

 

can i add space to the keyword like below?

if index(NAME," Liverpool ") then output;

but i am afraid that the first one will fail cause there is no space before the name Liverpool

 

what i am looking for is a separate "Liverpool" in my new data set

  

Regards,

Harry

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Use the INDEXW function if searching for words:

data want;
input name $50.;
if indexw(name,"Liverpool") then output;
datalines;
Liverpool
Liverpoollowolo
club Liverpool
;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Use the INDEXW function if searching for words:

data want;
input name $50.;
if indexw(name,"Liverpool") then output;
datalines;
Liverpool
Liverpoollowolo
club Liverpool
;
kelxxx
Quartz | Level 8

hi

you can use indexw(), findw()

it would be better to use upcase or lowcase for case-insensitive

if indexw(upcase(NAME),"LIVERPOOL") then output; 
if findw(lowcase(NAME),"liverpool") then output;
if findw(NAME,"liverpool"," ",'i') then output;

have a good day

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 547 views
  • 2 likes
  • 3 in conversation