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
Sorry, I should have tested.
flag = (index(char1,trim(char2)) + index(char2,trim(char1))) > 0;
PG
flag = (index(char1,char2) + index(char2,char1) > 0;
PG
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;
Sorry, I should have tested.
flag = (index(char1,trim(char2)) + index(char2,trim(char1))) > 0;
PG
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
Without TRIM the substring that is searched for by INDEX includes the trailling blanks. For example :
INDEX("T312F/M456U ", "M456U ") = 0
- PG
Thank You PG. Now i got it
Sam
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.
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.