BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Shortselling123
Fluorite | Level 6
Hi SAS experts!
I am wondering if there are some SAS functions that can tell me if one string variable contains another string variable? The "index" function can tell us whether one string variable contains a fixed string, but what if the string I am searching for is varying among different observations?

For example: there are two variables: v1 and v2. V1 is a short string and v2 is a long string. V1 can be "sgd" and "kjh" And v2 can be "hdsgdk" and "ukjhso". Obviously v2 contains v1 for these two cases. Any SAS functions can help me with this? Thanks!
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

As others have shown, the second parameter in INDEX can refer to a variable.  It doesn't have to be a fixed string.  However, when you use a variable, there is one issue that you might need to attend to:  the possibility of trailing blanks.  If you want the variable V1 to be the second parameter (the string to be searched for), it would be safer to use strip(v1) as the second parameter.

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

I did a small test though i am not sure what you are after:

Using index, indexw, find and findw

 

data have;
V1="sgd";
v2="hdsgdk";
output;
V1="kjh";
v2="ukjhso";
output;
run;

data want;
set have;
index=index(v2,v1);
indexw=indexw(v2,v1);
find=find(v2,v1);
findw=findw(v2,v1);
run;
Shortselling123
Fluorite | Level 6
Right. Thanks! Not sure why Index did not work last time...
ChrisBrooks
Ammonite | Level 13

Unless I'm not understanding the question properly you've answered it yourself Smiley Happy

 

data _null_;

	v1="sgd";
	v2="hdsgdk";

	if index(v2,v1) then put "found";
	else put "not found";

	v1="kjh";
	v2="ukjhso";

	if index(v2,v1) then put "found";
	else put "not found";

	/* Just for contrast */

	v1="sgd";
	v2="ukjhso";

	if index(v2,v1) then put "found";
	else put "not found";

run;
Shortselling123
Fluorite | Level 6
Thanks! This is really helpful.
Astounding
PROC Star

As others have shown, the second parameter in INDEX can refer to a variable.  It doesn't have to be a fixed string.  However, when you use a variable, there is one issue that you might need to attend to:  the possibility of trailing blanks.  If you want the variable V1 to be the second parameter (the string to be searched for), it would be safer to use strip(v1) as the second parameter.

Shortselling123
Fluorite | Level 6
Thanks! This is helpful!

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
  • 6 replies
  • 10310 views
  • 4 likes
  • 4 in conversation