Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert numeric into an alphanumeric value based on radix 36

Tags:

sas

It is easy to transform a number into a alphanumeric value based on radix 16 in SAS by using the $HEX format. Now i'm looking for an easy way to do this with radix 36 (10 numerals & 26 letters).

Examples:

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

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

like image 900
TechnoCore Avatar asked Mar 10 '11 12:03

TechnoCore


2 Answers

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

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;
like image 167
Laurent de Walick Avatar answered Sep 30 '22 10:09

Laurent de Walick


There is no built-in functionality for this. You can iteratively modulo 36 your number, then divide by 36 until what remains is zero. To convert the sequence of modulos you get you have to add 48decimal or 30hex to get the ascii-character in case of the digits 0-9 and 101decimal or 65hex to get the ascii-character in the case of the digits A-Z.

like image 30
Bernd Elkemann Avatar answered Sep 30 '22 10:09

Bernd Elkemann