BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
buddha_d
Pyrite | Level 9

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

20 REPLIES 20
Reeza
Super User

So truncation instead? 

 

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

Reeza
Super User

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);
PaigeMiller
Diamond | Level 26

If you want 3 decimal places

 

newvalue = floor(value*1000)/1000;
--
Paige Miller
PaigeMiller
Diamond | Level 26

@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
Tom
Super User Tom
Super User

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. 

Astounding
PROC Star

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;

buddha_d
Pyrite | Level 9
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.
Reeza
Super User

So how do we know how many decimals you want? 

 

is 0.00453 becoming 0.0045 or 0.004?

buddha_d
Pyrite | Level 9

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.

Reeza
Super User

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? 

buddha_d
Pyrite | Level 9

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. 

Reeza
Super User

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. 


 

buddha_d
Pyrite | Level 9

program, log and output are in the attachment 

 

Reeza
Super User

Looks fine to me. 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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