DATA Step, Macro, Functions and more

One string variable contains another string variable?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

One string variable contains another string variable?

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!

Accepted Solutions
Solution
‎01-14-2018 12:38 PM
Super User
Posts: 6,522

Re: One string variable contains another string variable?

Posted in reply to Shortselling123

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


All Replies
PROC Star
Posts: 1,282

Re: One string variable contains another string variable?

Posted in reply to Shortselling123

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;
Occasional Contributor
Posts: 7

Re: One string variable contains another string variable?

Posted in reply to novinosrin
Right. Thanks! Not sure why Index did not work last time...
Valued Guide
Posts: 554

Re: One string variable contains another string variable?

Posted in reply to Shortselling123

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;
Occasional Contributor
Posts: 7

Re: One string variable contains another string variable?

Posted in reply to ChrisBrooks
Thanks! This is really helpful.
Solution
‎01-14-2018 12:38 PM
Super User
Posts: 6,522

Re: One string variable contains another string variable?

Posted in reply to Shortselling123

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.

Occasional Contributor
Posts: 7

Re: One string variable contains another string variable?

Posted in reply to Astounding
Thanks! This is helpful!
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 181 views
  • 3 likes
  • 4 in conversation