I have a set of values which have a large number of digits to the right of the decimal. I need a way to round to the nearest tenth while NOT rounding up to the next integer if the decimal value is >= .95 . For instance:
Raw Value -- Preferred Rounded Value
2.9546732 -- 2.9
4.5423 -- 4.5
4.5734 -- 4.6
0.983764 -- 0.9
9.89473 -- 9.9
9.99999964 -- 9.9
Any idea on how to perform this?
This is most unusual. But can be done:
data _null_;
do x = 2.9546732, 4.5423, 4.5734, 0.983764, 9.89473, 9.99999964;
y = int(x) + ifn(x-int(x) >= 0.9, 0.9, round(x-int(x),0.1));
put x= y=;
end;
run;
x=2.9546732 y=2.9 x=4.5423 y=4.5 x=4.5734 y=4.6 x=0.983764 y=0.9 x=9.89473 y=9.9 x=9.99999964 y=9.9
@RandoDando wrote:
I have a set of values which have a large number of digits to the right of the decimal. I need a way to round to the nearest tenth while NOT rounding up to the next integer if the decimal value is >= .95 . For instance:
Raw Value -- Preferred Rounded Value
2.9546732 -- 2.9
4.5423 -- 4.5
4.5734 -- 4.6
0.983764 -- 0.9
9.89473 -- 9.9
9.99999964 -- 9.9
Any idea on how to perform this?
Do you have a similar aversion to rounding down to an integer such as when the value is 10.04 where the nearest tenth would be 10.0?
This is most unusual. But can be done:
data _null_;
do x = 2.9546732, 4.5423, 4.5734, 0.983764, 9.89473, 9.99999964;
y = int(x) + ifn(x-int(x) >= 0.9, 0.9, round(x-int(x),0.1));
put x= y=;
end;
run;
x=2.9546732 y=2.9 x=4.5423 y=4.5 x=4.5734 y=4.6 x=0.983764 y=0.9 x=9.89473 y=9.9 x=9.99999964 y=9.9
num = int(num*10) / 10;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.