BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sam369
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Sorry, I should have tested.

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

PG

PG

View solution in original post

6 REPLIES 6
PGStats
Opal | Level 21

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

PG

PG
sam369
Obsidian | Level 7

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;

PGStats
Opal | Level 21

Sorry, I should have tested.

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

PG

PG
sam369
Obsidian | Level 7

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

PGStats
Opal | Level 21

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

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

- PG

PG
sam369
Obsidian | Level 7

Thank You PG. Now i got it

Sam

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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