BookmarkSubscribeRSS Feed
AKNagarajan
Calcite | Level 5

What is the logic behind the PUT function to convert a numeric variable to character variiable?

consider two examples;

data temp;

value = 0.5500;

value1 = put(value, 16.1);

run;

the above code correctly prints the value of value1 as 0.6 in the output dataset.

But in some scenarios, the value1 in the output dataset is being printed as 0.5(gets truncated instead of rounding). I couldnt identify where the issue is. could someone please help?

Thanks.

5 REPLIES 5
art297
Opal | Level 21

I will take a guess: numeric precision.

see: SAS(R) 9.2 Companion for Windows, Second Edition

AKNagarajan
Calcite | Level 5

thank you Arthur. How we can resolve this?

data_null__
Jade | Level 19

The W.D format does not always round correctly.  The "classic" example to me is -0.0 below.  To solve it, round the value to the desired precision before using PUT function.

data _null_;
   a=
.55;
  
put a=hex16.;
  
do x = .33+.2199999999999995,-1e-16;
      y = put(x,
16.1);
      z = put(round(x,.1),16.1);
      put x=hex16. x= y= z=;
      end;
  
run;

a=3FE199999999999A
x=3FE1999999999995 x=
0.55 y=0.5 z=0.6
x=BC9CD2B297D889BC x=-
1E-16 y=-0.0 z=0.0
RamKumar
Fluorite | Level 6

How does x resolve in your code? You've used comma between the values so I'm not sure how it calculates.

x = .33+.2199999999999995,-1e-16;

data_null__
Jade | Level 19

It is iterative DO specifications connected by COMMA.

specification

denotes an expression or a series of expressions in this form

start <TO stop> <BY increment> <WHILE(expression) | UNTIL(expression)>

in this case with only the START values specified similar to DO I=1,10,20; each comma specifies a separate specification.


START STOP and BY can all be expressions as in this case add two numeric constants .33+.219999 comma then another expression (a numeric constant) -1E-16


The DO iterates 2 times.



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
  • 5 replies
  • 4981 views
  • 1 like
  • 4 in conversation