synaptik
synaptik

Reputation: 9499

SAS macro convert integer to hexadecimal string

Suppose I have the following macro variable assignment:

%let x=42165;

How can I get the corresponding hex string representation? That is, how can I transform &x and store it into &y such that

%put y=&y;

writes

y=A4B5

?

Upvotes: 1

Views: 583

Answers (3)

user667489
user667489

Reputation: 9569

Jeff's answer is fine, but if you're generating &x as the output of a data step function via %sysfunc, you can save a few characters by applying the format using %sysfunc itself, e.g.

%let y = %sysfunc(sum(42000, 165), hex4.);

Upvotes: 1

Salva
Salva

Reputation: 322

If you want to store the HEX string representation, you may want to do it inside a Datastep Program.

Here is how:

%let x=42165;
%put &x;

%let y=;

data _null_;
    call symput('y',put(&x,hex4.));
run;

%put &y;

This works. If you need to store the value into a Dataset, just modify the program.

Upvotes: 2

Jeff
Jeff

Reputation: 1807

Like this:

%let y=%sysfunc(putn(&x.,hex4.));

[I think there's no %put() macro function to avoid confustion with the %put macro statement.]

Upvotes: 3

Related Questions