Contributor
Posts: 73

# Contains?

Hi,

Hope anyone can help with this problem:

I hava two colums (character), a and b like this:

 a b 0|1|2|3|4 0|1|2|3|4|5|6|7|8 3|4 0|1|2|3|4|5|6|7|8 0|1|2|3|4 1|2|3|4|5|6|7|8 0|1|2|3|4 0|1|2|3

I want to add a third variable (match) if column b contains the information in column a. Like this

 a b match 0|1|2|3|4 0|1|2|3|4|5|6|7|8 1 3|4 0|1|2|3|4|5|6|7|8 1 0|1|2|3|4 1|2|3|4|5|6|7|8 0 2|3|4 0|1|2|3 0

Can this be done with contains-function?

Thanks

Thomas

Occasional Contributor
Posts: 5

## Re: Contains?

if index(strip(b),strip(a))>0 then match=1;

else match=0;

Super User
Posts: 9,599

## Re: Contains?

Hi,

Several ways really:

data want;

set have;

if index(a,b)>0 then match=1;

else match=0;

run;

You could also use Perl Regular Expressions.  Or the find() function.  Etc.

Posts: 1,147

## Re: Contains?

Hi ,

I tried the same but i am not sure why using index function, it is not working

However with find function with trim modifier it is working

data have;

input a :\$10. b :\$20.;

datalines;

0|1|2|3|4 0|1|2|3|4|5|6|7|8

3|4 0|1|2|3|4|5|6|7|8

0|1|2|3|4 1|2|3|4|5|6|7|8

run;

data want;

set have;

if find(b,a,"t")>0 then match=1;

else match=0;

run;

Thanks,

Jag

Thanks,
Jag
Contributor
Posts: 73

## Re: Contains?

The find function is working. Thanks

Contributor
Posts: 73

## Re: Contains?

Thanks,

However, I cound not make thiscode work. Every match is 0

/T

Super User
Posts: 9,599

## Re: Contains?

Had it the wrong way round:

data want;

set have;

if index(b,a)>0 then match=1;

else match=0;

run;

Contributor
Posts: 73

Thanks

Posts: 1,147

## Re: Contains?

Hi,

Even the following code does not work, in the beginning i tried the same but i wonder why the index is not working in this case.Any thoughts

data want;

set have;

if index(b,a)>0 then match=1;

else match=0;

run;

Thanks,
Jag
Occasional Contributor
Posts: 5

## Re: Contains?

tried with "strip()"?

Super User
Posts: 9,599

## Re: Contains?

Blanks, that's the problem.  Using a variable means all the characters including suffix blanks are used:

data have;

a="0|1|2|3|4"; b="0|1|2|3|4|5|6|7|8"; output;

a="3|4"; b="0|1|2|3|4|5|6|7|8"; output;

run;

data want;

set have;

tmp=index(b,strip(a));

run;

In the previous one, a refers to 3|4_____ with the underscores as blanks, so it isn't found.

Posts: 1,147

## Re: Contains?

Thank you, that solves the problem

Thanks,
Jag
Occasional Contributor
Posts: 5

## Re: Contains?

A Forum where you can see the postings not until 6 hours after posting is..... useless.....

Super User
Posts: 23,776

## Re: Contains?

Only because you're a new user, it's a spam control issue at the moment I believe.

Discussion stats
• 13 replies
• 524 views
• 1 like
• 5 in conversation