data have;
input string_1 : $12. string_2 $12.;
datalines;
elbow below
brog grob
yet tey
abc efg
;
run;
OUTPUT to print check string_1 with string_2 content all letters are match then this will be anagram string :
example: elbow & below both are having same words then this should be print as anagram string ;
If you don't have multi byte characters in your string do this:
[EDIT:]
unfortunately for multi byte characters it can fails (example in P.S.)...
data have;
input string_1 : $12. string_2 $12.;
datalines;
elbow below
brog grob
yet tey
abc efg
xxxxx xxxx
żółć ćółż
空手道 道手空
;
run;
proc print;
run;
filename f TEMP;
data _null_; file f; put; run;
%let n=10; /* max number of letters */
data want;
set have;
infile f truncover;
input @1 @;
_infile_ = string_1;
input @1 (str1_1-str1_&n.) ($1.) @@;
_infile_ = string_2;
input @1 (str2_1-str2_&n.) ($1.) @@;
call sortc(of str1_:);
call sortc(of str2_:);
anagram = (cats(of str1_:) = cats(of str2_:));
drop str1_: str2_:;
run;
filename f CLEAR;
proc print;
run;
Bart
{EDIT:}
P.S.
data x;
x="空";
y=char(x,1)!!char(x,3)!!char(x,2);
run;
proc print;
run;
X and Y are two different characters but their bytes are "the same after sorting"...
P.S.2 I'll find you a different solution.
[EDIT:] Here it is: https://communities.sas.com/t5/SAS-Programming/How-to-check-whether-two-strings-are-anagram-or-not/m...
If you don't have multi byte characters in your string do this:
[EDIT:]
unfortunately for multi byte characters it can fails (example in P.S.)...
data have;
input string_1 : $12. string_2 $12.;
datalines;
elbow below
brog grob
yet tey
abc efg
xxxxx xxxx
żółć ćółż
空手道 道手空
;
run;
proc print;
run;
filename f TEMP;
data _null_; file f; put; run;
%let n=10; /* max number of letters */
data want;
set have;
infile f truncover;
input @1 @;
_infile_ = string_1;
input @1 (str1_1-str1_&n.) ($1.) @@;
_infile_ = string_2;
input @1 (str2_1-str2_&n.) ($1.) @@;
call sortc(of str1_:);
call sortc(of str2_:);
anagram = (cats(of str1_:) = cats(of str2_:));
drop str1_: str2_:;
run;
filename f CLEAR;
proc print;
run;
Bart
{EDIT:}
P.S.
data x;
x="空";
y=char(x,1)!!char(x,3)!!char(x,2);
run;
proc print;
run;
X and Y are two different characters but their bytes are "the same after sorting"...
P.S.2 I'll find you a different solution.
[EDIT:] Here it is: https://communities.sas.com/t5/SAS-Programming/How-to-check-whether-two-strings-are-anagram-or-not/m...
You probably do need to worry about multi-byte characters. Unless you can show that it is impossible to construct two different sets of unicode characters from the same set of bytes.
For example what if you had one string that had normal character X and also multi-byte character ZY. And the other string had normal character Y and multi-byte character ZX. They would each have the same 3 byte strings XYZ, but would not be anagrams when considering just the 2 character original strings.
Yes, I'm looking for 3 byte UTF characters at the moment, which are:
1110xxxx 10xxxxxx 10xxxxxx
so the second and third could be "swapped".
Bart
Here is an example which fails my code:
data x;
x="空";
y=char(x,1)!!char(x,3)!!char(x,2);
run;
proc print;
run;
Bart
Ok, this one fixes multi byte problem.
data have;
input string_1 : $12. string_2 $12.;
datalines;
elbow below
brog grob
yet tey
abc efg
xxxxx xxxx
żółć ćółż
空手道 道手空
;
run;
data x;
length string_1 string_2 $ 12;
x="空";
string_1=x;
string_2=char(x,1)!!char(x,3)!!char(x,2);
drop x;
run;
data have2;
set have x;
run;
proc print;
run;
%let n=10; /* max number of letters */
data want;
set have2;
array str1_[&N.] $ 4. _temporary_;
array str2_[&N.] $ 4. _temporary_;
l1 = klength(string_1);
l2 = klength(string_2);
anagram=0;
if l1 NE l2 then
do;
output;
if 0;
end;
do i = 1 to l1;
str1_[i] = ksubstr(string_1,i,1);
str2_[i] = ksubstr(string_2,i,1);
end;
call sortc(of str1_[*]);
call sortc(of str2_[*]);
anagram = (cats(of str1_[*]) = cats(of str2_[*]));
output;
run;
proc print;
run;
Bart
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 16. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: