0

I want to compare string value of A and B by using the index function. I want to check if A contains B in its column. The only way I know how to do it is Index but the problem is index doesn't allow column name in its parameters. You have to enter a string value.

Tried this: index(Address, HouseNumber)>0 but it doesn't work.

Example:

Address        HouseNumber    
123 Road       Road

So I want to see if Address column contains House number value in its field. It wont be a direct match but just want to check if A contains the string. I think using a macro variable or array is the solution but I do not know how to do it.

Joe
  • 62,789
  • 6
  • 49
  • 67
Paula
  • 3
  • 3
  • 1
    I think you have your answer below in Tom's, but your question would get a much better answer if you put in the code to replicate your problem as a full data step. – Joe Aug 19 '16 at 15:16

2 Answers2

3

You need to account for the padding that SAS does since all variables are fixed length.

data have ;
  length Address HouseNumber $50;
  infile cards dsd dlm='|';
  input address housenumber ;
cards;
123 Road|Road
;;;;

data want ;
  set have ;
  if index(address,strip(HouseNumber));
run;
Tom
  • 47,574
  • 2
  • 16
  • 29
  • `FIND()` has the `'t` modifier that will do the trimming for you, subject to the caveat that it will trim both fields. – Joe Aug 19 '16 at 15:15
  • This comment saved my life. I was wondering why the index didnt work. I added the strip() in, and it works! – Paula Aug 21 '16 at 18:09
0

This works - is it what you're trying to do??

data _null_;
  a = '52 Festive Rd';
  b = 'Festive';
  if index(a,b) then put 'yes';
  else put 'no';
run;
Angus
  • 137
  • 8
  • To see the problem add some spaces to the end of the value of B. Either by including them in the string literal or setting the length before the assignment something longer than 7 characters. – Tom Aug 19 '16 at 17:14
  • Yup but had to add the strip() in . I tried this earlier but not giving me results even if both strings are the same. Thanks! – Paula Aug 21 '16 at 18:09