BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RodFeak
Calcite | Level 5

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.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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

4 REPLIES 4
FreelanceReinh
Jade | Level 19

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.);
Astounding
PROC Star

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.

Ksharp
Super User
Can you try other format Like: put v1v2_char= $EBCDIC8.;
RodFeak
Calcite | Level 5

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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