BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ganeshmule
Fluorite | Level 6

 

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 ;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

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...

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

5 REPLIES 5
yabwon
Onyx | Level 15

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...

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

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.

yabwon
Onyx | Level 15

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

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Onyx | Level 15

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

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 915 views
  • 6 likes
  • 3 in conversation