BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ronein
Meteorite | Level 14

Hello

What is the way to round these numbers to 2 numbers after digits: 

5.750000  to 5.75

1.683700 to 1.68

4.0663  to 4.07

6.2703  to 6.27

 

I also dont think that this activity is called round because actually I need to take 2 numbers after point

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@Ronein wrote:
Apply format will not help me because I need to change the real number and not just change format.
The reason is that I need to merge the data with another data set (and in the other data set the numbers are with 4 digits after point)

First, be sure that what you see from one variable where it always has two digits after the decimal is not caused by a format, check the unformatted values.

 

If you are doing the merge/join in SQL, you don't need to create a new variable. You just need the ROUND function in the ON part of an SQL call, for example if a.variablename is the data set that has values with 2 digits after the decimal and b.variablename2 has lots of decimal places, use:

 

on a.variablename = round(b.variablename2, 0.01)

 

 

If you are doing this merge in a DATA step, then you probably do need to create a new variable to contain the rounded value.

 

This of course brings up the issue, discussed many times here in the SAS Communities, of machine precision, where the values still may not match (depending on how they were created) in the 15th decimal place, for example, and so you might have to round both variables (even though it appears that the one is already rounded to 2 decimal places).

--
Paige Miller

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Apply the 8.2 format

 

data have;
input num;
format num 8.2;
datalines;
5.750000 
1.683700 
4.0663   
6.2703   
;
Ronein
Meteorite | Level 14
Apply format will not help me because I need to change the real number and not just change format.
The reason is that I need to merge the data with another data set (and in the other data set the numbers are with 4 digits after point)
PaigeMiller
Diamond | Level 26

@Ronein wrote:
Apply format will not help me because I need to change the real number and not just change format.
The reason is that I need to merge the data with another data set (and in the other data set the numbers are with 4 digits after point)

First, be sure that what you see from one variable where it always has two digits after the decimal is not caused by a format, check the unformatted values.

 

If you are doing the merge/join in SQL, you don't need to create a new variable. You just need the ROUND function in the ON part of an SQL call, for example if a.variablename is the data set that has values with 2 digits after the decimal and b.variablename2 has lots of decimal places, use:

 

on a.variablename = round(b.variablename2, 0.01)

 

 

If you are doing this merge in a DATA step, then you probably do need to create a new variable to contain the rounded value.

 

This of course brings up the issue, discussed many times here in the SAS Communities, of machine precision, where the values still may not match (depending on how they were created) in the 15th decimal place, for example, and so you might have to round both variables (even though it appears that the one is already rounded to 2 decimal places).

--
Paige Miller
Ronein
Meteorite | Level 14

I want to have full match because the 2 digits after point are matched and the numbers before points are matched.

 


data have1;
input num;
format num 8.2;
datalines;
5.750000 
1.683700 
4.0663   
6.2703   
;
Run;


data have2;
input num;
format num 8.2;
datalines;
5.75
1.68
4.06  
6.27 
;
Run;

proc sql;
create table want as
select a.num
from have1 as a
inner join have2 as b
on a.num=b.num
;
quit;
PeterClemmensen
Tourmaline | Level 20

I see. Try this approach to numeric truncation without rounding then

 

data have;
input num;
trunc = int(num*10**2)/10**2;
datalines;
5.750000 
1.683700 
4.0663   
6.2703   
;
andreas_lds
Jade | Level 19

From the numbers shown you do what i learned some decades ago as "rounding".

So round(variable, 0.01) should give you what you want.

Remember to apply the function whenever values with decimals are checked for equality, because on every computer 0.1 is not the always the same as 0.1 - this has been explained so many times, i abstain from repeating it here.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 6 replies
  • 8721 views
  • 4 likes
  • 4 in conversation