SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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