Send As SMS

Saturday, March 17, 2007

Case-insensitive Sorting

If you want to sort a field in a case-insensitive way (for use by humans, not computers), there are several ways you can do it.

options nocenter nodate nonumber;
data test;
infile cards;
input sometext $8.;
cards;
abcd
ABCD
aBcD
efgh
EFGH
111
a
Z
A
z
;;;; run;

proc sort data=test out=test_default;
   by sometext;
run;

title 'Default Sort Order';
proc print data=test_default;
run;

proc sort data=test out=test_lat1_lcs
          sortseq=lat1_lcs; 
   by sometext;
run;

title 'lat1_lcs Sort Order';
proc print data=test_lat1_lcs;
run;

proc sql;
   create table test_sql as
      select   *
      from     test
      order    by upcase(sometext);
quit;

title 'SQL Sort Order';
proc print data=test_sql;
run;

The result:

Default Sort Order

Obs    sometext

  1      111
  2      A
  3      ABCD
  4      EFGH
  5      Z
  6      a
  7      aBcD
  8      abcd
  9      efgh
 10      z

lat1_lcs Sort Order Obs sometext 1 111 2 a 3 A 4 abcd 5 ABCD 6 aBcD 7 efgh 8 EFGH 9 Z 10 z
SQL Sort Order Obs sometext 1 111 2 A 3 a 4 aBcD 5 abcd 6 ABCD 7 EFGH 8 efgh 9 z 10 Z

In this case, the last two sorts produce the same result. On an EBCDIC system, however, they would be different because of differences between ASCII encoding and EBCDIC encoding. Also, the first sort would produce different results on the two systems.

0 Comments:

Post a Comment

<< Home