Hello SAS world.
Today I'm trying to find an easy way (hopefully) to do some column rounding based on a value in the first digit to the RIGHT of a decimal point, with a computed column in EG
Essentially If the first digit is a '1' or greater, I want to round UP to the next whole number, otherwise.. it remains the same whole number. At the end.. the result data column will be formatted to be only a whole number, no decimal positions.
So:
'2.0122' would be returned as '2.00' or '2.0122'
'2.1203' would be '3.00' or '3.1203'
'4.6129' would be '5.00' or '5.6129'
'0.099' would be '0'
'0.111' would be '1' or '1.111'
My first thought was to convert the value to a character value, then use a sub-string to get the first 'digit', then to use a case statement to determine what to do..
I'm sure there is something easier, more efficient to do this with. I just haven't learned it yet.
I looked into FLOOR, CEIL, ROUND functions.. but I don't see anything that suggests I can control what position is judged, and how the controlling argument (.1) is assigned.
Any Ideas would be very appreciated.
You're hunting in the right ballpark. Try it this way:
if var >= int(var) + 0.1 then var = ceil(var);
else var = int(var);
The logic is designed for positive numbers. If you can have negative numbers, you might have to specify a different formula, based on a different set of rules.
Extract the first digit to the right of the decimal from a numeric variable
decimal = x - floor(x);
first_digit = floor(10*decimal);
Round according to your rules
if first_digit >= 1 then x = ceil(x);
else x = floor(x);
what about or a like:
if int(var+0.9) = int(var) then result=var;
else result=int(var+0.9);
You're hunting in the right ballpark. Try it this way:
if var >= int(var) + 0.1 then var = ceil(var);
else var = int(var);
The logic is designed for positive numbers. If you can have negative numbers, you might have to specify a different formula, based on a different set of rules.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.