turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Re: One string variable contains another string va...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Saturday

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!

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

Sunday

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Shortselling123

Saturday

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Shortselling123

Saturday

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;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to novinosrin

Sunday

Right. Thanks! Not sure why Index did not work last time...

Highlighted
## Re: One string variable contains another string variable?

Options

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Shortselling123

Saturday

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

```
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;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ChrisBrooks

Sunday

Thanks! This is really helpful.

Solution

Sunday

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Shortselling123

Saturday

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Astounding

Sunday

Thanks! This is helpful!