turn on suggestions

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
- /
- Base SAS Programming
- /
- rounding off to lower number

Topic Options

- 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
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-13-2017 04:03 PM

folks,

could you please help to round off numbers with sas function?

eg: 0.0015 or 0.0019 or 0.001595 or anything after 0.001.... should be 0.001 until it hit 0.002

thanks

Accepted Solutions

Solution

11-13-2017
11:11 PM

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

Posted in reply to buddha_d

11-13-2017 10:59 PM

The next-to-last observation should be fine as is. The last observation is asking for a new set of rules, and you haven't presented any such rules. Here is another approach you can experiment with:

var = round(var, 0.00000001);

var = round(var, 0.01); or possibly var = round(var - 1e-10, 0.01);

By rounding twice, the 0.049999999 would become 0.05, then the second rounding wouldn't change the value. You might have to experiment with the number of decimal places to use in the first rounding, and whether you want to subtract that tiny amount for the second rounding.

All Replies

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

Posted in reply to buddha_d

11-13-2017 04:07 PM

So truncation instead?

Always to the third decimal place or....?

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

Posted in reply to Reeza

11-13-2017 04:18 PM

Basically you need to use some math functions. FLOOR can be used, but rounds to integers so first round it to the nearest integer and then create it back as a decimal value.

Or here's another of the math tricks using ROUND() function:

`z = round(x - 0.0005, 0.001);`

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

Posted in reply to buddha_d

11-13-2017 04:10 PM

If you want 3 decimal places

newvalue = floor(value*1000)/1000;

--

Paige Miller

Paige Miller

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

Posted in reply to PaigeMiller

11-13-2017 04:35 PM

PaigeMiller wrote:

If you want 3 decimal places

newvalue = floor(value*1000)/1000;

I should add this doesn't give the desired answer for negative numbers, but it is easily fixed.

--

Paige Miller

Paige Miller

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

Posted in reply to buddha_d

11-13-2017 04:31 PM - edited 11-13-2017 04:36 PM

You could also use the MOD() function.

` y = x - mod(x,0.001);`

Which will work differently for negative numbers than the FLOOR(x*1000)/1000 option.

```
data test ;
input x;
y = x - mod(x,0.001);
z = floor(x*1000)/1000;
cards;
0.0015
0.0019
0.001595
-0.00234
;
proc print; run;
```

Obs x y z 1 0.001500 0.001 0.001 2 0.001900 0.001 0.001 3 0.001595 0.001 0.001 4 -.002340 -.002 -.003

FLOOR() will go down to -.003 since it is smaller than -.002340. The MOD() will instead look more like a truncation of the first few digits. Essentially finding the round number that is closer to zero.

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

Posted in reply to buddha_d

11-13-2017 04:37 PM

Could your incoming values be negative as well as positive? Should -0.0015 round to -0.001 or to -0.002?

Depending on your answers, you might want to consider:

var = int(var * 1000) / 1000;

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

Posted in reply to buddha_d

11-13-2017 06:10 PM

Thank you all for the comments. No, there are no negative numbers. Let me give more clarification:

For example :

0.0450000 (0.040000-0.0450000) round off to 0.04 and anything above it eg: 0.0450001 (0.0450001-0.0459999) round off to 0.05.

Please let me know if need more clarification. Thanks in advance.

For example :

0.0450000 (0.040000-0.0450000) round off to 0.04 and anything above it eg: 0.0450001 (0.0450001-0.0459999) round off to 0.05.

Please let me know if need more clarification. Thanks in advance.

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

Posted in reply to buddha_d

11-13-2017 06:47 PM

So __ how do we know__ how many decimals you want?

is 0.00453 becoming 0.0045 or 0.004?

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

Posted in reply to Reeza

11-13-2017 07:01 PM

We are rounding off to two digits. (eg: 0.065 is equals to 0.06 , 0.0650001 equals to 0.07).

thanks reeza for prompt reply.

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

Posted in reply to buddha_d

11-13-2017 07:04 PM

Any of the above approaches can be easily modified to work with a two decimal place solution. If you have issues implementing a specific solution post your code, log and DETAIL what issue you're receiving.

buddha_d wrote:

We are rounding off to two digits. (eg: 0.065 is equals to 0.06 , 0.0650001 equals to 0.07).

thanks reeza for prompt reply.

Then why did you start off with one that rounded to three digits?

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

Posted in reply to Reeza

11-13-2017 08:58 PM

Sorry for putting it wrong first time . My problem (with any of the above code) is once the number shows as 0.005 or above (for round off two decimal), SAS would round off to 0.01. My case is even when it reaches 0.005, it should display as 0.00 (for 2 digit round off) . But if i give 0.0050001 then it should display as 0.01.

Have data:

0.0750001

0.0750000

0.0755920

0.0744999

0.0700001

want to display as:

0.08

0.07

0.08

0.07

0.07

I tried with above mentioned code manipulation and didn't find an answer.

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

Posted in reply to buddha_d

11-13-2017 09:06 PM

If you have issues implementing a specific solution post your code, log and DETAIL what issue you're receiving.

**If you have issues implementing a specific solution post your code, log and DETAIL what issue you're receiving. **

buddha_d wrote:

Sorry for putting it wrong first time . My problem (with any of the above code) is once the number shows as 0.005 or above (for round off two decimal), SAS would round off to 0.01. My case is even when it reaches 0.005, it should display as 0.00 (for 2 digit round off) . But if i give 0.0050001 then it should display as 0.01.

Have data:

0.0750001

0.0750000

0.0755920

0.07449990.0700001

want to display as:

0.08

0.07

0.08

0.07

0.07

I tried with above mentioned code manipulation and didn't find an answer.

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

Posted in reply to Reeza

11-13-2017 09:27 PM

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

Posted in reply to buddha_d

11-13-2017 10:31 PM

Looks fine to me.