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
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.
So truncation instead?
Always to the third decimal place or....?
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);
If you want 3 decimal places
newvalue = floor(value*1000)/1000;
@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.
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.
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;
So how do we know how many decimals you want?
is 0.00453 becoming 0.0045 or 0.004?
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.
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?
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.
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.
program, log and output are in the attachment
Looks fine to me.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: