Help using Base SAS procedures

Compare the variables

Accepted Solution Solved
Reply
Regular Contributor
Posts: 168
Accepted Solution

Compare the variables

Hi,

In my dataset i have a 2 character variables, i need to compare them.If any  Part of char1 matched with char2 i need to flag it.Matched items are marked as in BOLD in want dataset

have;

id                                        char1                                      char2

100                                   T312F/M456U                       M456U
110                                   J568M                                    M578T

120                                  G786E/T024Y/F123Y            F123Y

130                                  F765G                                    Q903T/N678B/Y347T/F765G/B987Y

140                                  H786E/F024Y/K123Y            F123Y/M768Y/C675N

Want:

id                                        char1                                      char2                                                                  Flag

100                                   T312F/M456U                       M456U                                                                 1
110                                   J568M                                    M578T                                                                  0

120                                  G786E/T024Y/F123Y            F123Y                                                                   1

130                                  F765G                                    Q903T/N678B/Y347T/F765G/B987Y                1     

140                                  H786E/F024Y/K123Y            F123Y/M768Y/C675N                                        0

Thanks

Sam


Accepted Solutions
Solution
‎04-11-2013 06:21 PM
Respected Advisor
Posts: 4,920

Re: Compare the variables

Sorry, I should have tested.

flag = (index(char1,trim(char2)) + index(char2,trim(char1))) > 0;

PG

PG

View solution in original post


All Replies
Respected Advisor
Posts: 4,920

Re: Compare the variables

flag = (index(char1,char2) + index(char2,char1) > 0;

PG

PG
Regular Contributor
Posts: 168

Re: Compare the variables

Hi PGStat,

after using your code i got flag 0

see the code below:

data have;

input  id char1 $20. char2 $35.;

cards;

100  T312F/M456U          M456U

110  J568M                M578T

120  G786E/T024Y/F123Y    F123Y

130  F765G                Q903T/N678B/Y347T/F765G/B987Y

140  H786E/F024Y/K123Y    F123Y/M768Y/C675N

;

run;

data want;

set have;

  flag = index(char1,char2) + index(char2,char1) > 0;

run;

proc print;run;

Solution
‎04-11-2013 06:21 PM
Respected Advisor
Posts: 4,920

Re: Compare the variables

Sorry, I should have tested.

flag = (index(char1,trim(char2)) + index(char2,trim(char1))) > 0;

PG

PG
Regular Contributor
Posts: 168

Re: Compare the variables

Perfect!!!!

Thank you PGstat.

But i didn't understand how exactly this was worked. I was confused at trim part? if you have time could you explain it

THanks

Sam

Respected Advisor
Posts: 4,920

Re: Compare the variables

Without TRIM the substring that is searched for by INDEX includes the trailling blanks. For example :

INDEX("T312F/M456U         ", "M456U                              ") = 0

- PG

PG
Regular Contributor
Posts: 168

Re: Compare the variables

Thank You PG. Now i got it

Sam

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 222 views
  • 0 likes
  • 2 in conversation