SAS Enterprise Guide

Desktop productivity for business analysts and programmers
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cnilsen
Quartz | Level 8

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.

 

 

 

 

 

- Chris N.
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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);

 

 

--
Paige Miller
Shmuel
Garnet | Level 18

what about or a like:

if int(var+0.9) = int(var) then result=var;
else result=int(var+0.9);
Astounding
PROC Star

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.

cnilsen
Quartz | Level 8
This worked awsome. I made a small change to use FLOOR in the else result so I ending up with whole numbers no matter what.. Thanks to all who posted with solutions!
- Chris N.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2447 views
  • 2 likes
  • 4 in conversation