Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- Query on Rounding off

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-14-2016 11:21 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-14-2016 11:42 AM

You seem to have already answered your own question there:

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-14-2016 11:42 AM

You seem to have already answered your own question there:

data roundtest;

set <libref>.filename;

y=round(y,0.1);

w=round(w,0.1);

...;

run;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-14-2016 11:48 AM

Thank you @RW9.

Appreciate your help. Expected something more complicated.

LOL. Starting troubles :-)

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-14-2016 11:48 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-14-2016 12:04 PM

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

Please guide me on this:

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!

:-)

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-14-2016 12:11 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-14-2016 12:32 PM - edited 01-14-2016 01:44 PM

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;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-14-2016 01:15 PM

Thank you @ballardw.

the second code I Understood. First one I have to disect and try out and then ill reach out to you.

the second code I Understood. First one I have to disect and try out and then ill reach out to you.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-14-2016 12:55 PM

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!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-14-2016 01:12 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-14-2016 01:17 PM

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.