<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Convert displayed HEX value to character in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/580963#M165070</link>
    <description>&lt;P&gt;Whether you need to use PUT() or INPUT() depends on what type of data the original poster actually has. They words are ambiguous and could be interpreted either way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 14 Aug 2019 00:13:17 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-08-14T00:13:17Z</dc:date>
    <item>
      <title>Convert displayed HEX value to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/580925#M165057</link>
      <description>&lt;P&gt;I have a dataset of values store as $hex32 character variables. Is there any way to copy the displayed HEX value to a string?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, the displayed HEX value is "FF938C281FEB8DC357B37032976326A3". I would like that exact string (not the underlying unprintable data) in a character variable that I can then manipulate using substr(). I have searched around and can't figure out a way to do this. Any help would be appreciated. Thanks.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Aug 2019 19:05:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/580925#M165057</guid>
      <dc:creator>wedens</dc:creator>
      <dc:date>2019-08-13T19:05:51Z</dc:date>
    </item>
    <item>
      <title>Re: Convert displayed HEX value to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/580939#M165062</link>
      <description>&lt;P&gt;I think you're asking how to create a new variable with the formatted value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;newVar = PUT(oldVar, $hex32.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PUT() will apply the format and convert it to a character value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285087"&gt;@wedens&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a dataset of values store as $hex32 character variables. Is there any way to copy the displayed HEX value to a string?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, the displayed HEX value is "FF938C281FEB8DC357B37032976326A3". I would like that exact string (not the underlying unprintable data) in a character variable that I can then manipulate using substr(). I have searched around and can't figure out a way to do this. Any help would be appreciated. Thanks.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Aug 2019 19:36:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/580939#M165062</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-08-13T19:36:25Z</dc:date>
    </item>
    <item>
      <title>Re: Convert displayed HEX value to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/580960#M165068</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285087"&gt;@wedens&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;You need the $HEXw. &lt;EM&gt;&lt;STRONG&gt;IN&lt;/STRONG&gt;&lt;/EM&gt;FORMAT&amp;nbsp;coupled with the &lt;EM&gt;&lt;STRONG&gt;IN&lt;/STRONG&gt;&lt;/EM&gt;PUT function to do that. If your variable (let's call it HEX) has 32 hex digits (i.e. 0-9, A-F), you should use W=32. If the character string, to which the hex value is being converted (let's call it CHAR), hasn't been preassigned a length, reading HEX using the $HEX32. informat will result in CHAR with length $16, as 2 hex digits correspond to 1 byte.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  char = input (hex, $hex32.) ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that using the $HEX32. &lt;EM&gt;format&lt;/EM&gt; with the &lt;EM&gt;PUT&lt;/EM&gt; function &lt;EM&gt;will not work&lt;/EM&gt; because the format interprets its argument merely as a collection of&amp;nbsp; characters to be converted to their hex representation, regardless of whether these characters also happen to be hex digits or not.&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Aug 2019 23:15:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/580960#M165068</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-08-13T23:15:20Z</dc:date>
    </item>
    <item>
      <title>Re: Convert displayed HEX value to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/580961#M165069</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;Nope, it &lt;EM&gt;will not&lt;/EM&gt; work.&lt;/P&gt;
&lt;P&gt;It would if the OP needed to convert (print, display) some characters to (in) their hex representation. But what is needed here is to &lt;EM&gt;read&lt;/EM&gt; a string in the radix16 representation (whose digits are the characters from 0 to 9 and A to F) and convert it to the radix 256 representation (whose digits are all the 256 characters in the collating sequence). Since reading in SAS is done using &lt;EM&gt;in&lt;/EM&gt;formats, that's what the doctor ordered here; and the conversion function used with &lt;EM&gt;in&lt;/EM&gt;formats is &lt;EM&gt;IN&lt;/EM&gt;PUT, not PUT. See what you get if you try to use the format instead of the informat:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_ ;                
  c = "AB" ;                 
  x = put (c, $hex4.) ;      
  right = input (x, $hex4.) ;
  wrong =   put (x, $hex4.) ;
  put right= wrong= ;        
run ;                        
-------------------
right=AB wrong=3431
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What we are getting here with the format instead of the correct original value "AB", is the first two characters of X (themselves hex digits, but the format doesn't care) in their 16-radix representation.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Moreover, the $HEX&lt;EM&gt;w&lt;/EM&gt;. &lt;EM&gt;format&lt;/EM&gt; does nothing to evaluate the input to see if it is in the correct hex digit form. But the $HEX&lt;EM&gt;w&lt;/EM&gt;. &lt;EM&gt;informat&lt;/EM&gt; does: If its argument should contain anything but hex digits, it will set _ERROR_=1, write a note "Invalid argument to function INPUT" in the log (provided, of course, that the ?? modifier is not used with it), and set the response to a missing value (i.e. space in this case).&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Aug 2019 23:43:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/580961#M165069</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-08-13T23:43:21Z</dc:date>
    </item>
    <item>
      <title>Re: Convert displayed HEX value to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/580963#M165070</link>
      <description>&lt;P&gt;Whether you need to use PUT() or INPUT() depends on what type of data the original poster actually has. They words are ambiguous and could be interpreted either way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Aug 2019 00:13:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/580963#M165070</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-08-14T00:13:17Z</dc:date>
    </item>
    <item>
      <title>Re: Convert displayed HEX value to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/580964#M165071</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;For example, the displayed HEX value is "FF938C281FEB8DC357B37032976326A3". I would like that exact string (not the underlying unprintable data) in a character variable that I can then manipulate using substr()&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;I'm assuming this part is true and what the OP has is actually a&amp;nbsp;variable with the HEX32 format. I could be wrong of course &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Aug 2019 00:30:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/580964#M165071</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-08-14T00:30:04Z</dc:date>
    </item>
    <item>
      <title>Re: Convert displayed HEX value to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/580966#M165072</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;Agreed; it's ambiguous, indeed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now that I've reread it a few times, it looks as though I might have been duped by "stored as", and it is most likely that what the OP &lt;EM&gt;sees&lt;/EM&gt; is the variable with the $HEX32. format attached to it, so it appears displayed as hex in OP's UI. Which of course would mean that it's not "stored as" hex at all, but the OP needs a different variable where the content of the variable in question would indeed be stored in its hex representation. In which case&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;would be the one who's interpreted the specs correctly, and I would be the one who's goofed.&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Aug 2019 01:07:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/580966#M165072</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-08-14T01:07:07Z</dc:date>
    </item>
    <item>
      <title>Re: Convert displayed HEX value to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/580967#M165073</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;I think you're actually right. Of course, that would mean that the "stored as" part is wrong. Go figure &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Aug 2019 01:09:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/580967#M165073</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-08-14T01:09:28Z</dc:date>
    </item>
    <item>
      <title>Re: Convert displayed HEX value to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/581220#M165166</link>
      <description>&lt;P&gt;Thanks to both of you for the comments and apologies for the initial ambiguities. I don't typically interact with this type of variable structure so my language may have been incorrect.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The HEX variable is generated using the MD5 function and the output is assigned the $hex32 format to make it readable. So I believe I was incorrect in my initial post. The underlying data is whatever the default output of the MD5 function is and I have applied the $hex32 format myself. My issue is that when I try to then truncate or combine the HEX variable with normal char or num variables the result is gibberish. My goal is to combine a 2-digit year, a 2-digit state code, and the first 6 digits of the HEX variable into a new combined ID. See below for what currently happens when I run the following code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test; length hex $32.; format hex $hex32.; set book1;
	hex = md5(cats(year,state,id));
	newid = cats(state,mod(year,100),substr(hex,6));
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;hex&lt;/TD&gt;&lt;TD&gt;Year&lt;/TD&gt;&lt;TD&gt;State&lt;/TD&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;newid&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0FC31C920C2957877533B187C24FDB1D&lt;/TD&gt;&lt;TD&gt;2019&lt;/TD&gt;&lt;TD&gt;GA&lt;/TD&gt;&lt;TD&gt;123456&lt;/TD&gt;&lt;TD&gt;GA19)W‡u3±‡ÂOÛ&amp;#29;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My actual goal is for the newid variable in this case to be GA190FC31C with a standard $char10 format. Does that make more sense? I'm having a tough time putting it into words. Thanks for the help.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Aug 2019 17:19:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/581220#M165166</guid>
      <dc:creator>wedens</dc:creator>
      <dc:date>2019-08-14T17:19:27Z</dc:date>
    </item>
    <item>
      <title>Re: Convert displayed HEX value to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/581226#M165169</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285087"&gt;@wedens&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;Now it's much clearer. You need to replace the SUBSTR function with the PUT function coupled with the $HEX6. format (it will grab 3 first bytes, each byte is 2 hex digits, so you will have 3*2=6). HEX as a variable name for the MD5 digest is confusing, so below it's changed to just MD5. You also need to give NEWID the needed length 10, otherwise CATS will default it to 200:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test ;                                                
  set book1 ;                                              
  format md5 $hex32. ;                                     
  md5 = md5 (cats (year, state, id)) ;  &lt;BR /&gt;  length newid $ 10 ;                   
  newid = cats (state, mod (year, 100), &lt;STRONG&gt;put (md5, $hex6.)&lt;/STRONG&gt;) ;
run ;                                                      
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Aug 2019 18:47:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/581226#M165169</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-08-14T18:47:36Z</dc:date>
    </item>
    <item>
      <title>Re: Convert displayed HEX value to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/581228#M165170</link>
      <description>&lt;P&gt;Please consider:&lt;/P&gt;
&lt;PRE&gt;data work.test; 
   length hex $32.; 
   format hex $hex32.; 
/*   set book1;*/
   Year=2019;
   State='GA';
   id = '123456';
	hex = md5(cats(year,state,id));
	newid = cats(state,mod(year,100),substr(put(hex,$hex32.),1,6));
run;&lt;/PRE&gt;
&lt;P&gt;You didn't provide the BOOK1 set so I hard coded in the values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that your Substr was likely way wrong as it would start at position 6 and return everything through the end of the string.&lt;/P&gt;
&lt;P&gt;You likely want to set a length for NEWID as well. As is it will default to 200 characters.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Aug 2019 17:52:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/581228#M165170</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-08-14T17:52:53Z</dc:date>
    </item>
    <item>
      <title>Re: Convert displayed HEX value to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/581231#M165172</link>
      <description>The number in the $HEXn. format specification is the WIDTH of the output, not the LENGTH of the input.&lt;BR /&gt;So $HEX6. takes the first 3 characters of the input string and generates 6 hex characters that are the hexadecimal representation of those three bytes.&lt;BR /&gt;</description>
      <pubDate>Wed, 14 Aug 2019 17:58:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/581231#M165172</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-08-14T17:58:16Z</dc:date>
    </item>
    <item>
      <title>Re: Convert displayed HEX value to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/581237#M165174</link>
      <description>&lt;P&gt;Thanks to all. This worked perfectly! Looks like the suggested code from both&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;/&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp; and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;did exactly what I needed. Sorry again for the initial confusion.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Aug 2019 18:14:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/581237#M165174</guid>
      <dc:creator>wedens</dc:creator>
      <dc:date>2019-08-14T18:14:07Z</dc:date>
    </item>
    <item>
      <title>Re: Convert displayed HEX value to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/581245#M165179</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;Correct. Shame on my old goofy head. Thanks for catching it - need to edit my response.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Aug 2019 18:46:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-displayed-HEX-value-to-character/m-p/581245#M165179</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-08-14T18:46:11Z</dc:date>
    </item>
  </channel>
</rss>

