BookmarkSubscribeRSS Feed
bollibompa
Quartz | Level 8

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

13 REPLIES 13
grimol
Calcite | Level 5

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

else match=0;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Jagadishkatam
Amethyst | Level 16

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
bollibompa
Quartz | Level 8

The find function is working. Thanks

bollibompa
Quartz | Level 8

Thanks,

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

/T

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Had it the wrong way round:

data want;

     set have;

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

     else match=0;

run;

bollibompa
Quartz | Level 8

Thanks

Jagadishkatam
Amethyst | Level 16

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
grimol
Calcite | Level 5

tried with "strip()"?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Jagadishkatam
Amethyst | Level 16

Thank you, that solves the problem

Thanks,
Jag
grimol
Calcite | Level 5

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

Reeza
Super User

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

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 13 replies
  • 1628 views
  • 1 like
  • 5 in conversation