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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.