DATA Step, Macro, Functions and more

How to convert hexadecimal to character?

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

How to convert hexadecimal to character?

I have two variables that are internally stored as two bytes of hex data each. I need to create another variable from these two, such that the new variable is a character concatenation of the two, with the equivalent hexadecimal characters, in this case 8 characters.

 

I've tried

 

V1V2_CHAR = CAT(V1_HEX, V2_HEX);

FORMAT V1V2_CHAR $CHAR4. ;

 

but the variable still prints in hex (non-printable on the screen) but a hex display shows it.

 

I admittedly only use SAS on rare occasions, so pardon my newbi-ness..

 

Thanks!

Rod

 

PS. This is SAS on z/OS.

 

 


Accepted Solutions
Solution
‎03-28-2016 07:52 AM
Trusted Advisor
Posts: 1,115

Re: How to convert hexadecimal to character?

Hello Rod,

 

Here's an example (tested on a Windows machine):

data have;
v1_hex='030B'x;
v2_hex='4041'x;
run;

data want;
set have;
v1v2_char=put(v1_hex||v2_hex, $hex8.);
run; /* Result: 030B4041 */

If V1_HEX and V2_HEX are character variables of length >2 (i.e. contain trailing blanks), you'll want to replace the assignment statement with

v1v2_char=put(cats(v1_hex, v2_hex), $hex8.);

unless you see a risk that removing leading and trailing blanks (by the CATS function) could truncate any of the two variables (because they happen to contain a blank as the first or second character). In the latter case, you can use:

v1v2_char=put(put(v1_hex,$2.)||put(v2_hex,$2.), $hex8.);

View solution in original post


All Replies
Solution
‎03-28-2016 07:52 AM
Trusted Advisor
Posts: 1,115

Re: How to convert hexadecimal to character?

Hello Rod,

 

Here's an example (tested on a Windows machine):

data have;
v1_hex='030B'x;
v2_hex='4041'x;
run;

data want;
set have;
v1v2_char=put(v1_hex||v2_hex, $hex8.);
run; /* Result: 030B4041 */

If V1_HEX and V2_HEX are character variables of length >2 (i.e. contain trailing blanks), you'll want to replace the assignment statement with

v1v2_char=put(cats(v1_hex, v2_hex), $hex8.);

unless you see a risk that removing leading and trailing blanks (by the CATS function) could truncate any of the two variables (because they happen to contain a blank as the first or second character). In the latter case, you can use:

v1v2_char=put(put(v1_hex,$2.)||put(v2_hex,$2.), $hex8.);
Super User
Posts: 5,081

Re: How to convert hexadecimal to character?

Given your newness to SAS, I would suggest checking your assumptions about what these variables actually contain.  It's simple enough to do that:

 

data _null_;

set have;

put v1_hex @4  $hex4.  @10 v2_hex @13 v2_hex $hex4.;

stop;

run;

 

The solution changes depending on what text is actually in the original variables.

Super User
Posts: 9,676

Re: How to convert hexadecimal to character?

Can you try other format Like: put v1v2_char= $EBCDIC8.;
New Contributor
Posts: 2

Re: How to convert hexadecimal to character?

Thanks to all who responded! The PUT was what I was missing and the recommendations solved the problem.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 519 views
  • 0 likes
  • 4 in conversation