Convert a numeric value to an alphanumeric value based on 36

Easily convert a number to a radix 16 based alphanumeric value in SAS using the $ HEX format. Now I'm looking for an easy way to do this with radix 36 (10 digits and 26 letters).

Examples:

  • 100 → '2s'
  • 2000 → '1jk'
  • 30000 → 'n5c'
  • 400000 → '8kn4'

In Java, you can do this with Integer.toString(mynumber, 36). Any ideas how to do this in the SAS database?

+5
source share
3 answers

Unfortunately, there is an easy way to do this in usings formats, but the next data step should solve the problem. It only works for positive integers.

data _null_;
 infile cards;
 input innumber;
 number = innumber;
 format base $32.;
 alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
 if number = 0 then base = '0';
 else
  do while (number ne 0);
   mod = mod(number, length(alphabet));
   div = floor(number / (length(alphabet)));
   base = cats(substr(alphabet,mod+1,1),base);
   number = div;
  end;
 put innumber= base=;
cards;
0
100
2000
30000
400000
;
run;
+3
source

. 36 , 36, . , 48decimal 30hex, ascii- 0-9 101decimal 65hex, ascii- AZ.

+1

PROC FCMP , . , :

proc fcmp outlib=sasuser.funcs.Radix36;
   function Radix36(innumber) $ 32; /* returns character string, length 32 */
     number = innumber;
     format base $32.;
     alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
     if number = 0 then base = '0';
     else do while (number ne 0);
        mod = mod(number, length(alphabet));
        div = floor(number / (length(alphabet)));
        base = cats(substr(alphabet,mod+1,1),base);
        number = div;
     end;
     return (base);
   endsub;
run;

/*****************************************************/
options cmplib=sasuser.funcs; /* add to search path */
data _null_;
input innumber;
 base = Radix36(innumber); /* call function */
 put innumber= base=;
datalines;
0
100
2000
30000
400000
;
run;
+1

All Articles