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
if index(strip(b),strip(a))>0 then match=1;
else match=0;
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.
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
The find function is working. Thanks
Thanks,
However, I cound not make thiscode work. Every match is 0
/T
Had it the wrong way round:
data want;
set have;
if index(b,a)>0 then match=1;
else match=0;
run;
Thanks
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;
tried with "strip()"?
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.
Thank you, that solves the problem
A Forum where you can see the postings not until 6 hours after posting is..... useless.....
Only because you're a new user, it's a spam control issue at the moment I believe.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.