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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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