Pyrite | Level 9

How to increase decimal, or add decimal to an integer?

Hello!

I have a column like the one below:

yyzz

4556

0.56

0.375

1223456.5

9673000000

1.24

I just want to maintain 5 decimal places with each number. So my result should be:

yyzz

4556.00000

0.56000

0.37500

1223456.50000

9673000000.00000

1.24000

What should be the code to get this desired outcome? Struggled a lot myself, but no help!

Regards.

3 REPLIES 3
Super User

Re: How to increase decimal, or add decimal to an integer?

``````data have;
input yyzz;
format yyzz f32.5;
cards;
4556
0.56
0.375
1223456.5
9673000000
1.24
;``````
Super User

Re: How to increase decimal, or add decimal to an integer?

``````data have;
input yyzz;
format yyzz f32.5;
cards;
4556
0.56
0.375
1223456.5
9673000000
1.24
;``````
Jade | Level 19

Re: How to increase decimal, or add decimal to an integer?

Hello @d6k5d3,

Assuming that yyzz is a numeric variable in an existing SAS dataset (let's call it HAVE) you don't need to modify the values of yyzz in order to change their appearance. As you know, 4556=4556.00000 mathematically and SAS would store both values in exactly the same way internally. The same is true for the other values (0.56=0.56000, etc.*).

SAS formats are the right tool for changing the way how a value is displayed, printed or written to a file. For example, you can display a numeric value with the format named 17.5 in order to get 5 decimal places and up to 17 characters (including all digits, the decimal point and a minus sign, if any) in total. So, this would be sufficient for your example values (which require only up to 16 characters). Similarly, the format F32.5 (which is an alias for 32.5) suggested by Ksharp would reserve a width of 32 (rather than 17) characters for the output field. (Try the PROC PRINT step below with both formats, one at a time, to see the difference.)

You can apply such formats temporarily in the code you are using to display the values, e.g., in a PROC PRINT step:

``````proc print data=have;
format yyzz 17.5;
run;``````

Alternatively, you can associate the format permanently with variable yyzz so that the above PROC PRINT step would produce the same result without the FORMAT statement. (This is what Ksharp did when he created dataset HAVE.) To do this, you can use the same FORMAT statement in a DATA step:

``````data have;
set have;
format yyzz 17.5;
run;``````

or, more efficiently, in a PROC DATASETS step:

``````proc datasets lib=work nolist;
modify have;
format yyzz 17.5;
quit;``````

The latter has the advantage that no data are read or written, but only one variable attribute is added (a change only in "metadata").

* (Sadly this is not quite true in general: There are exceptional cases where SAS stores numeric values which are mathematically equal in slightly different ways. But this is a more advanced topic.)

Discussion stats
• 3 replies
• 33916 views
• 7 likes
• 3 in conversation