SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RandoDando
Pyrite | Level 9

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?

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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
PG

View solution in original post

5 REPLIES 5
ballardw
Super User

@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?

RandoDando
Pyrite | Level 9
No, I do not. If it is 10.04, I would want just 10.
PGStats
Opal | Level 21

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
PG
RandoDando
Pyrite | Level 9
I know it is out of the ordinary, but this is only for a displayed value and not being used in any other analyses.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 1262 views
  • 2 likes
  • 4 in conversation