I can test this now, but I think this would come close enough:
var = round(var - 1e-12, 0.01);
Man, Astounding awesome. Could you please give me logic for this??
thanks a ton......
The basic idea is to round to the nearest 0.01. That would solve most of the problem, except that standard rounding would round 0.015 to 0.02 instead of 0.01. So subtract a tiny amount, and then round to the nearest 0.01. If you actually have a value that is 0.0150000000001, this formula will be incorrect and will round to 0.01 instead of 0.02. The "tiny amount" being subtracted would be enough to alter the result.
How can I adjust the formula for varying digits (bold letters) for eg:
data new;
input x;
var = round(x - 1e-12, 0.01);
cards;
0.0750001
0.0750000
0.0755920
0.0744999
0.0700001
0.050000001
0.049999999
;
run;
the results are:
0.08
0.07
0.08
0.07
0.07
0.05
0.05
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.