## Query on Rounding off

Solved
Occasional Contributor
Posts: 15

# Query on Rounding off

Hi Guys,

I am Shriram from Bangalore, India. I am new to this community/forum. I have just about started learning SAS and have lots of doubts.

I would be glad and grateful if anyone can give me pointers on how to approach the subject, how to ask mny questions and any other suggestions.

Heres is to a start of our association:

Scenario 1: No problems and it works fine

data _null_;

x=123.45567;

y=round(x,0.1); /* nth decimal place*/

z=round(x,100); /* nearest 100 place*/

m=round(x,10);  /*nearest 10 place*/

put y;

put z;

put m;

run;

Scenario 2: how to round of columns of an incoming data set

data roundtest;

set <libref>.filename;

then how to write the code for rounding of columns y w r l and k. (Attached screen shot of columns)

the data comprises of columns y w r l and K and year. Now all these columns(except year) are in 8-9th decimal place. I want to round of to the first decimal  place?...

Accepted Solutions
Solution
‎01-14-2016 11:47 AM
Super User
Posts: 9,599

## Re: Query on Rounding off

data roundtest;

set <libref>.filename;

y=round(y,0.1);

w=round(w,0.1);

...;

run;

All Replies
Solution
‎01-14-2016 11:47 AM
Super User
Posts: 9,599

## Re: Query on Rounding off

data roundtest;

set <libref>.filename;

y=round(y,0.1);

w=round(w,0.1);

...;

run;

Occasional Contributor
Posts: 15

## Re: Query on Rounding off

Thank you @RW9

Appreciate your help. Expected something more complicated.

LOL. Starting troubles :-)

Super User
Posts: 6,781

## Re: Query on Rounding off

To me, the most important piece of advice is not to round unless you absolutely have to.  You can control how many decimal places to use when printing by applying a format.  For example:

data test;

x=123.4567;

y=x;

z=x;

run;

proc print data=test;

format y 8.1;

format z 8.2;

run;

You can let variables keep their actual value without rounding, but still print rounded versions.

Good luck.

Occasional Contributor
Posts: 15

## Re: Query on Rounding off

@Astounding - Thank you. Yes I agree with you.

You have used 8.1 and 8.2 - this is new to me, as in I was not aware there were number formats like 8.1...

How do i learn short cuts like that - as in can you share a link or something or am i approaching this the right way?

Hope my question makes sense!

:-)

Occasional Contributor
Posts: 15

## Re: Query on Rounding off

@Astounding or if i were to rephrase my earlier question

Can you also explain (lead to to the explanation link) how this works?

As in if i use format 1.1 or 2.1 or ......8.1.... etc

What is the difference or what is the logis used by sas to apply these formats onto the dataset?

Super User
Posts: 13,563

## Re: Query on Rounding off

[ Edited ]

The "logic" for applying formats depends on what you are doing. Many analysis procedures will have a default to display a given number of decimals but you can direct the output to a data set and print the results with a different number of decimals, or you may be able to override the default. Here is an example:

``````proc tabulate data=sashelp.class;
class sex;
var height weight;
table sex, (height weight)*(n mean std)
/box='Default formats for statistics';
table sex, (height weight)*(n mean*f=f8.4 std*f=best12.)
/box='Specified formats for statistics';
run;``````

You can also use formats to group data

``````Proc freq data=sashelp.class;
tables weight;
run;

proc format library=work;
value WeightGroup
0 - 85   = ' 0 <= 85'
85< - 110= '85 <=110'
110< - high='110>';
run;

Proc freq data=sashelp.class;
tables weight;
format weight WeightGroup.;
run;``````

Occasional Contributor
Posts: 15

## Re: Query on Rounding off

Thank you @ballardw.
the second code I Understood. First one I have to disect and try out and then ill reach out to you.
Super User
Posts: 6,781

## Re: Query on Rounding off

There are dozens of numeric formats available within the software.

It isn't a question of explaining a few tricks.  Formatting is one of the basics.  Just start somewhere, and add to what you know over time.

To explain 8.1 and 8.2, here are a few general rules.  The first number ("8" in this case) refers to the total number of positions being used to print a numerical value.  That includes all digits (whether before or after the decimal point), the decimal point, and (if needed) a negative sign.  If you are using other numeric formats, such as those that print a dollar sign or a comma, the first number still includes every character being printed.  The second number ("1" or "2" in this case) is the number of digits to print after the decimal point.  SAS will automatically round as needed.

If you apply a format when reporting, it does not change the data.  It only changes the form in which that report prints the data.

Hope this helps!

Occasional Contributor
Posts: 15

## Re: Query on Rounding off

ok that makes sense. Drawing on the same - explanation iv read/understood from u and elsewhere is that numbers are stored in 8 bytes of data hence the 8 and then 1.2.... after the decimal for round off accordingly.
Super User
Posts: 6,781

## Re: Query on Rounding off

Truthfully, it's just coincidence that numbers are stored in 8 bytes.  SAS stores the value (not the digits themselves) in a totally different form (binary floating point).  SAS can accurately store an integer with 15 to 16 significant digits (depends on the operating system).  Very similar explanations would apply to other numeric formats (whether longer or shorter):  comma14.2, dollar5.0, for example.

🔒 This topic is solved and locked.