BookmarkSubscribeRSS Feed
shantanupl1
Obsidian | Level 7

Hi All,

 

I have the following dataset :

temp_values

35.3
36.1
36.2
36.6
36.5
36.5
36.6
36.2
36.2
36.2
35.9
36.5
36.3
36.4
36.2
36.3

I am calculating mean using proc means and the result in the output dataset for mean value appears to be 36.25. However if I filter mean value of 36.25 then this record is not appearing in filtered data and I checked that the value is actually less than 36.25. For the same data using proc sql i am getting mean as exact 36.25. I am unable to identify the cause why proc means is giving this weird value.

proc means data=r;
var temp_values;
output out=r1 mean=mean n=n;
run;

 

Also while converting the mean value to a character format of 5.1 the result is 36.2 but the result should be 36.3.

12 REPLIES 12
PaigeMiller
Diamond | Level 26

This is called machine precision, where a binary computer cannot represent fractions exactly and so mathematical calculations such as summing or averaging (or really, any other mathematical calculation) and so the results can be off by a tiny value (sometimes called epsilon) very close to zero ... and there's nothing wrong.

 

So PROC MEANS produces a result that is (for this data) a tiny little bit less than 36.25. How much less than 36.25? Well you can subtract 36.25 from the answer given by PROC MEANS and find out that the difference is -7.105427357601E-15, which is nothing to worry about and cannot be avoided with binary computers, and perfectly normal.

--
Paige Miller
shantanupl1
Obsidian | Level 7

Thank you very much for the solution.

But if i round the value to 1 decimal then the result is 36.3 and when we are using put function we are getting 36.2. 

If the value is little bit less than 36.25 then why the round function is giving 36.3?

 

And this was noticed because while using proc sql/proc univariate we are getting the result as exact 36.25 and I am getting the difference 0.1.

PaigeMiller
Diamond | Level 26

Why are you using the PUT function here?

--
Paige Miller
shantanupl1
Obsidian | Level 7

I want to convert the numeric value to character value for display purpose.

PaigeMiller
Diamond | Level 26

@shantanupl1 wrote:

I want to convert the numeric value to character value for display purpose.


This was not part of your original problem statement, and so I do not understand what the question or problem is regarding PUT.

--
Paige Miller
shantanupl1
Obsidian | Level 7
I mentioned in the last statement that while converting the value to a character format of 5.1 I am getting the value as 36.2.
PaigeMiller
Diamond | Level 26

But why are you using the PUT function in the first place? How does that help anything? Why do you want to display numbers as character? I am totally lost here, as your original problem had nothing to do with displaying as character.

--
Paige Miller
shantanupl1
Obsidian | Level 7
I want to display my mean values in the format as mean (sd) . And because of this i need to convert those numeric values of mean and standard deviation to character to display the values in the above format.
shantanupl1
Obsidian | Level 7
My problem was always the same. The mean value for the above data should be 36.25. But the proc means procedure is giving the value little less than 36.25(reason mentioned by you). So when i was converting that value into a character format using put function(which generally works similarly as round function) the result is 36.2. But when i am using round function the result i am getting is 36.3.
a1=put(mean,5.1)
a2=round(mean,0.1)
Also when I am using proc sql or proc univariate on the same data i am getting the mean as exact 36.25.
So i am curious about the working of those functions.
PaigeMiller
Diamond | Level 26

So when i was converting that value into a character format using put function(which generally works similarly as round function) the result is 36.2.

 

This is the part I don't understand. Why? Most people do not (except in rare situations) convert numbers to characters to display them. You have explained that you want to do it, you have not explained why you are doing this.

 

You can do the proper rounding on numbers, now that you know what the issue is. The ROUND function will round your nearly 36.25 to exactly 36.25, and then if you really want, you can round it again to one decimal point. I add that the usual rule of thumb is that if your data is measured to one decimal place, then the average is reported with one more decimal place, in other words, two decimal places. None of which require character values or converting to character, which will not solve the problem you have with the number very close to (but not exactly equal to) 36.25.

--
Paige Miller
Reeza
Super User

PUT() truncates at the decimal point requested 36.25 -> 36.2

ROUND() rounds at the decimal point requested 36.25 -> 36.3

 


@shantanupl1 wrote:
My problem was always the same. The mean value for the above data should be 36.25. But the proc means procedure is giving the value little less than 36.25(reason mentioned by you). So when i was converting that value into a character format using put function(which generally works similarly as round function) the result is 36.2. But when i am using round function the result i am getting is 36.3.
a1=put(mean,5.1)
a2=round(mean,0.1)
Also when I am using proc sql or proc univariate on the same data i am getting the mean as exact 36.25.
So i am curious about the working of those functions.

 

Reeza
Super User

However if I filter mean value of 36.25 then this record is not appearing in filtered data and I checked that the value is actually less than 36.25

Your mean data does not need to be an observation in the data set. If you're referring to the output from PROC MEANS then it's likely that you have 36.2500001 or something so you need to round it before you compare it.

 

if round(mean, 0.01) = 36.25 then do....;

Also while converting the mean value to a character format of 5.1 the result is 36.2 but the result should be 36.3.

Without the code you used we have no idea if that was done correctly or not. Also, check beyond 1 decimal place. If the number is very close to the boundary the difference could be smaller and causes it to round incorrectly. 

 

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 12 replies
  • 2285 views
  • 1 like
  • 3 in conversation