BookmarkSubscribeRSS Feed
G_I_Jeff
Obsidian | Level 7

I need to check the value of the first byte from a HEX4 format field.

The value of the first observation is: 6EA0.

I've tried assiging the field to a defined character field in a data step and checking the substr(*,1,1) and various other ways with no success.

6 REPLIES 6
data_null__
Jade | Level 19

It is not clear to me where this data exists.

This example may be helpful.

17         data _null_;
18            x='6EA0'x;
19            x1 = first(x);
20            put x1= x1=hex2.;
21            run;

x1=n x1=6E
G_I_Jeff
Obsidian | Level 7

data_null_;

Thanks for the example. I can reference the first byte of the HEX field, but I need to check the first character. I know how to accomplish this in COBOL or Assembler, just don't know how to do it in SAS. Sorry, should have made myself a little clearer.

I need to check the first character to see if it's a numeric 6 to assign another field a value:

IF X1 =: '06'X THEN X2 = 'A';

I'm not 100% clear on how SAS is storing this value. I see the format in the storing pdb is HEX4, but it seems SAS is treating the value as character (which is baffling me).

Astounding
PROC Star

Jeff,

SAS lets you store hex strings as either character or numeric.  The way you refer to them depends on how they are stored.  Either of these is permissible:

hex_num = 6EA0x;

hex_char = '6EA0'x;

HEX_NUM would be stored in SAS's usual numeric format, so you could check it with:

if put(hex_num, hex4.) =: '06' then X2='A';

HEX_CHAR would be stored as the two-byte character equivalent of 6E A0, so you could check it with:

if put(hex_char, $hex4.) =: '06' then X2='A';

Good luck.

G_I_Jeff
Obsidian | Level 7

Astounding,

I totally agree with you and I believe that your numeric example should be working, but it doesn't.

This is my exact code (latest I've tried). DEVNR is type=num len=5 format=HEX4:

IF PUT(DEVNR,HEX4.) = 06X THEN DEVTYPE='V';

ELSE DEVTYPE='P';

My education/training tells me that the actual DEVNR field should look like '06EA0F', a 3 byte length field. When I list the variable, it shows a 4 numeric ( I assume) length field in the listing. Is SAS maybe padding this field with zeros? Is it NOT storing it as a true HEX compressed field? Or am I totally way off base here?

Astounding
PROC Star

Jeff,

A couple of factors are at work here.

First, there is no special storage format in SAS for numeric hex values.  All numerics get stored in the same floating point format, and all can be expressed in hex format.  For example, you could try:

value = 255;

put value hex2.;

put value hex3.;

put value hex4.;

Second, the PUT function always returns a character string.  It doesn't matter whether the first argument is character or numeric, the result is always an expression of a value in a format which means a character string.  You could try (and then run PROC PRINT / PROC CONTENTS):

year=204;

result = put(year, z4.);

So if DEVNR is a numeric, how it was created doesn't really matter.  It gets stored in the same format internally no matter what.  If it was generated by reading in a character string from hex, you could test it this way:

*devnr = 06AAx;

if put(devnr, hex4.) =: '06' then devtype='V';

I think I've answered your questions, but feel free to ask again if this needs more detail.

Tom
Super User Tom
Super User

Sounds like you are talking about a number and not a character string. Otherwise SAS would have complained if you tried to use the numeric format HEX. instead of the character format $HEX.

You want to test the digit in a specific place then you could apply some arithmetic to the problem. For example to test the digit in the 100ths place :

if mod(int(x/100),10) = 6 then ...

If instead you want to test the most significant digit irrespective of magnitude it might be easiest to convert it to a character string using put functions.

if left(put(x,10)) =: '6' then ...

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 1224 views
  • 0 likes
  • 4 in conversation