DATA Step, Macro, Functions and more

rounding off to lower number

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 100
Accepted Solution

rounding off to lower number

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
2 weeks ago
Super User
Posts: 5,518

Re: rounding off to lower number

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.

View solution in original post


All Replies
Super User
Posts: 19,878

Re: rounding off to lower number

So truncation instead? 

 

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

Super User
Posts: 19,878

Re: rounding off to lower number

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);
Trusted Advisor
Posts: 1,934

Re: rounding off to lower number

If you want 3 decimal places

 

newvalue = floor(value*1000)/1000;
Trusted Advisor
Posts: 1,934

Re: rounding off to lower number

Posted in reply to PaigeMiller

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.

Super User
Super User
Posts: 7,083

Re: rounding off to lower number

[ Edited ]

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. 

Super User
Posts: 5,518

Re: rounding off to lower number

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;

Frequent Contributor
Posts: 100

Re: rounding off to lower number

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.
Super User
Posts: 19,878

Re: rounding off to lower number

So how do we know how many decimals you want? 

 

is 0.00453 becoming 0.0045 or 0.004?

Frequent Contributor
Posts: 100

Re: rounding off to lower number

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.

Super User
Posts: 19,878

Re: rounding off to lower number

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? 

Frequent Contributor
Posts: 100

Re: rounding off to lower number

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. 

Super User
Posts: 19,878

Re: rounding off to lower number

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.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. 


 

Frequent Contributor
Posts: 100

Re: rounding off to lower number

program, log and output are in the attachment 

 

Super User
Posts: 19,878

Re: rounding off to lower number

Looks fine to me. 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 20 replies
  • 132 views
  • 0 likes
  • 5 in conversation