Usage Note 52903: Converting values from base 10 to a given base and vice versa
It is often necessary to convert a value from base 10 to the equivalent of that value in another base, or vice versa. See the code on the Full Code tab for two examples. The first example converts from a given base to base 10. The second example converts from base 10 to a given base.
Operating System and Release Information
SAS System | Base SAS | z/OS | 9 TS M0 | |
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9 TS M0 | |
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9 TS M0 | |
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9 TS M0 | |
Microsoft Windows 2000 Advanced Server | 9 TS M0 | |
Microsoft Windows 2000 Server | 9 TS M0 | |
Microsoft Windows 2000 Datacenter Server | 9 TS M0 | |
Microsoft Windows 2000 Professional | 9 TS M0 | |
Microsoft Windows NT Workstation | 9 TS M0 | |
Microsoft Windows Server 2003 Datacenter Edition | 9 TS M0 | |
Microsoft Windows Server 2003 Enterprise Edition | 9 TS M0 | |
Microsoft Windows Server 2003 Standard Edition | 9 TS M0 | |
Microsoft Windows XP Professional | 9 TS M0 | |
64-bit Enabled AIX | 9 TS M0 | |
64-bit Enabled HP-UX | 9 TS M0 | |
64-bit Enabled Solaris | 9 TS M0 | |
HP-UX IPF | 9 TS M0 | |
Linux | 9 TS M0 | |
OpenVMS Alpha | 9 TS M0 | |
Tru64 UNIX | 9 TS M0 | |
*
For software releases that are not yet generally available, the Fixed
Release is the software release in which the problem is planned to be
fixed.
Example 1 converts a value from a given base to base 10. The string to be converted is read into the variable DIGITS and the base of that value is read into BASE. This example uses several bases to show that the code works for all. When you use the code, substitute your base value into each use of the variable BASE in the code, and then remove the base values from the input data.
Example 2 converts a value from base 10 to a given base. The base 10 string to convert is read into the variable BASE10 and the new base value is read into NEWBASE. This example uses several bases to show that the code works for all. When you use the code, substitute your base value into each use of the variable NEWBASE in the code, and then remove the base values from the input data.
/* Converting from a given base to base 10 */
data _null_;
retain possdigs '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
length basedig $1;
input digits $ base;
digits=upcase(digits);
l=length(digits);
j=0;
sum=0;
do i=l to 1 by -1;
basedig=substr(digits,i,1);
k=index(possdigs,basedig)-1;
sum+(k * base**j);
j+1;
end;
put digits= base= sum=;
datalines;
1234 10
ff 16
1111 2
ABCDEZZZZ 36
;
run;
/* Converting from base 10 to a given base */
data _null_;
retain possdigs '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
input base10 newbase;
do power=0 to 100 while(newbase**power<=base10); end;
left=base10;
length digits $10;
digits=' ';
i=0;
do power=power-1 to 0 by -1;
i+1;
r=int(left/(newbase**power));
substr(digits,i,1)=substr(possdigs,r+1,1);
left=left-(newbase**power)*r;
end;
put base10= newbase= digits=;
datalines;
1234 10
255 2
255 16
36 36
37 36
;
run;
/* Log output from Example 1 code */
DIGITS=1234 BASE=10 SUM=1234
DIGITS=FF BASE=16 SUM=255
DIGITS=1111 BASE=2 SUM=15
DIGITS=ABCDEZZZ BASE=36 SUM=808334375615
/* Log output from Example 2 code */
BASE10=1234 NEWBASE=10 DIGITS=1234
BASE10=255 NEWBASE=2 DIGITS=11111111
BASE10=255 NEWBASE=16 DIGITS=FF
BASE10=36 NEWBASE=36 DIGITS=10
BASE10=37 NEWBASE=36 DIGITS=11
Type: | Usage Note |
Priority: | |
Topic: | Data Management ==> Manipulation and Transformation Data Management ==> data management SAS Reference ==> DATA Step
|
Date Modified: | 2014-05-06 15:59:44 |
Date Created: | 2014-05-05 12:09:27 |