BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
eagles_dare13
Obsidian | Level 7

Hello,

I have a numeric variable x and i tried to convert to character using: put (x, best32.).

Now, x can have values like 3, 3.0, 3.02 etc...upto 2 decimal places.

How to keep the format as is..i.e 3 -->3, 3.0 --> 3.0 and so on?

Currently, with my code, it is outputting everything as 1 decimal places...and dropping the second decimal.

Thanks very much

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

It may be that

with my code, it is outputting everything as 1 decimal

is due to the method. Some procedures round for simplicity. Show how you are creating that output.

If you are printing the output then the format is likely an issue. Try assigning a format that forces 2 decimals such as

Format variable f8.2 ;

If the default format is something like best8. and you have values of 12345678.99 then only 8 digits are likely to display and the .99 wouldn't be seen.

View solution in original post

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Just specify the number of decimal places:

data have;

  num=3; output;

  num=3.01; output;

  num=3.1; output;

run;

data want;

  set have;

  length text $10.;

  text=strip(put(num,best8.2));

run;

ballardw
Super User

It may be that

with my code, it is outputting everything as 1 decimal

is due to the method. Some procedures round for simplicity. Show how you are creating that output.

If you are printing the output then the format is likely an issue. Try assigning a format that forces 2 decimals such as

Format variable f8.2 ;

If the default format is something like best8. and you have values of 12345678.99 then only 8 digits are likely to display and the .99 wouldn't be seen.

eagles_dare13
Obsidian | Level 7

Sorry, but 3.10 is coming as 3.1, the last 0 is getting dropped.

data have;

  num=3; output;

  num=3.01; output;

  num=3.10; output;

run;

data want;

  set have;

  length text $10.;

  text=strip(put(num,best7.2));

run;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yes, you can't fix set number of numbers and not use it for all the values like that.  So, to fix it to two decimal output you would use z8.2, however the upshot of this is that 3 would become 3.00.  That is I am afraid consistency.  If you don't want that effect then you will have to pad it yourself in the text field, ie.

temp=strip(substr(index(text,".")+1));  /* This gets you the decimal part */

if length(strip(temp)) < 2 then text=cats(strip(text),repeat("0",length(temp)-1);

Tom
Super User Tom
Super User

The number 3.1 and the number 3.10 are the exact same number.  If you want to distinguish between them you will need to store them differently.

You could store them in a character variable.

You could store additional variable(s)

     Perhaps store the display format you want :  (3.1, 'F3.1') and (3.10,'F4.2')

     Or just the number of decimal places (3.1, 1) and (3.10, 2).

You could store them in different variables which could have their own formats attached.

  DECIMAL1 = 3.1;

  DECIMAL2 = 3.1;

  format decimal1 F3.1 decimal2 F4.2 ;

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!

How to choose a machine learning algorithm

Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 32177 views
  • 0 likes
  • 4 in conversation