Help using Base SAS procedures

Contains?

Reply
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?

Posted in reply to bollibompa

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

else match=0;

Super User
Super User
Posts: 7,942

Re: Contains?

Posted in reply to bollibompa

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.

Trusted Advisor
Posts: 1,137

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?

Posted in reply to Jagadishkatam

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
Super User
Posts: 7,942

Re: Contains?

Posted in reply to bollibompa

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

Re: Contains?

Thanks

Trusted Advisor
Posts: 1,137

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?

Posted in reply to Jagadishkatam

tried with "strip()"?

Super User
Super User
Posts: 7,942

Re: Contains?

Posted in reply to Jagadishkatam

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.

Trusted Advisor
Posts: 1,137

Re: Contains?

Thank you, that solves the problem

Thanks,
Jag
Occasional Contributor
Posts: 5

Re: Contains?

Posted in reply to bollibompa

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

Super User
Posts: 19,770

Re: Contains?

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

Ask a Question
Discussion stats
  • 13 replies
  • 476 views
  • 1 like
  • 5 in conversation