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
@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).
Apply the 8.2 format
data have;
input num;
format num 8.2;
datalines;
5.750000
1.683700
4.0663
6.2703
;
@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).
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;
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
;
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.
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!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.