Hi, I have a data set that contains binary numbers and I want to convert them to hexadecimal values.
The format of binary variable is binary8.
Here is what I have:
binary | hexa |
10001101 | |
10100001 | |
10100000 |
Here is what I want:
binary | hexa |
10001101 | 8D |
10100001 | A1 |
10100000 | A0 |
I already tried with informat/format with hex./$hex. but it does not work.
Can anyone help me with that?
Did you read the bit string using BINARY in-format?
34 data _null_;
35 infile cards firstobs=2;
36 input b:binary. x:$hex2.;
37 put b=binary. b=hex2. b= x=$hex2.;
38 cards;
b=10001101 b=8D b=141 x=8D
b=10100001 b=A1 b=161 x=A1
b=10100000 b=A0 b=160 x=A0
Hi @dera,
As you know, Base SAS has only two variable types: numeric and character. Obviously, the former is the natural choice for your values. I take it that you have assigned the BINARY8. format permanently to variable BINARY in your existing dataset. This means that the values 141, 161, 160 stored in this variable are displayed as 10001101, etc. So, you could create a copy of variable BINARY, call it HEXA and assign the HEX2. format to it:
data have;
input binary binary.;
format binary binary8.;
cards;
10001101
10100001
10100000
;
data want;
set have;
hexa=binary;
format hexa hex2.;
run;
proc print data=want noobs;
run;
Result:
binary hexa 10001101 8D 10100001 A1 10100000 A0
However, the question is: What is the purpose of a redundant copy of a variable? For reporting purposes it is not needed:
proc report data=have;
column binary=b binary=h; /* creates aliases B and H for BINARY */
define b / display;
define h / format=hex2. 'hexa' width=4;
run;
Result:
binary hexa 10001101 8D 10100001 A1 10100000 A0
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.