BookmarkSubscribeRSS Feed
venkatnaveen
Obsidian | Level 7

Does anyone know of any alternatives to using the Best format? I am using the Best format to convert a numeric variable to character and would like to keep the same number of decimals originally present in the numeric variable. However I'm getting more decimal places on certain occasions. For instance, I have a variable - height and it has a value of 88.8. After doing charheight=put(height,best.). I am getting a value of 88.81 as opposed to 88.8. The Best format does this sometimes. I've seen this happen in 9.1 and 9.3 versions. Does anyone know of any trick of alternative to using the Best format in order to preserve the same number of decimal places present in the numeric variable while converting it to character?

10 REPLIES 10
RW9
Diamond | Level 26 RW9
Diamond | Level 26

It shouldn't do.  Are you sure the variable where you are getting 88.81 from doesn't have a format applied?  I.e the actual value is 88.81, however the variable has a 4.1 format applied hence you don't see the 1?  If not please post a specific example where this is happening. 

ballardw
Super User

This code doesn't replicate the problem:

data _null_;                                                                                                                           

x=88.8;                                                                                                                               

c=put( x, best.);                                                                                                                     

put c;                                                                                                                                

run;

Time to show what is generating your 88.8 and what you did afterwards.

Patrick
Opal | Level 21

I assume that if the best. format gives you 8.81 then the actual numeric value had been 8.81. You might have seen 8.1 only because there was already a format on the variable. So if you want in the character variable what you've seen before then use exactly the same format as already on the variable.

If you believe above isn't the reason for what you encounter then could you please post a code sample with data which demonstrates the issue?

Tom
Super User Tom
Super User

Did you try using VVALUEX() function?

Try this example.

data _null_;

  x=8.80567;

  format x 4.1 ;

  c1=put(x,4.2);

  c2=put(x,best12.);

  c3=vvalue(x);

  c4=vvaluex('x');

  put (_all_) (=/);

run;

Astounding
PROC Star

Your original question is based on a false premise.  There is no such thing as a number of decimals present in a numeric variable.  There is only a value that is stored.  You may have observed a number of decimals present in a report, but that is a feature of the reporting procedure.  It is not a feature of the variable itself.

If you want a certain number of decimal positions, use a different format.  All of these are legal statements:

newvar1 = put(temp, best8.);

newvar2 = put(temp, 4.1);

newvar3 = put(temp, 5.2);

Pick the one you want.  Just don't think of the number of decimal positions as being a characteristic of the data.  It isn't.  It's only a characteristic of a report.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Unfortunately I don't use Facebook and hence those links will be unavailable.  The posts have all suggested the same things above so if you would like to see a result, please post the text, test data, and example code of where this is happening.  If it really does do what you say it does then its a serious issue and needs to be reported to SAS via a helpdesk ticket.

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 connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 10 replies
  • 1688 views
  • 0 likes
  • 6 in conversation