SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Any 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?

Reply
Frequent Contributor
Posts: 89

Any 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?

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?

Super User
Super User
Posts: 7,942

Re: Any 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?

Posted in reply to venkatnaveen

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. 

Frequent Contributor
Posts: 89

Re: Any 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?

Yes I am sure

Super User
Posts: 11,336

Re: Any 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?

Posted in reply to venkatnaveen

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.

Respected Advisor
Posts: 4,173

Re: Any 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?

Posted in reply to venkatnaveen

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?

Frequent Contributor
Posts: 89

Re: Any 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?

Thanks

Frequent Contributor
Posts: 89

Re: Any 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?

this is query posted in facebook.

See the link.

https://www.facebook.com/groups/SASanalysts/

Super User
Super User
Posts: 7,035

Re: Any 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?

Posted in reply to venkatnaveen

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;

Super User
Posts: 5,495

Re: Any 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?

Posted in reply to venkatnaveen

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.

Frequent Contributor
Posts: 89

Re: Any 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?

Posted in reply to Astounding

this is query posted in facebook.

See the link.

https://www.facebook.com/groups/SASanalysts/

Super User
Super User
Posts: 7,942

Re: Any 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?

Posted in reply to venkatnaveen

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.

Ask a Question
Discussion stats
  • 10 replies
  • 710 views
  • 0 likes
  • 6 in conversation