Hello Statalist,
I am using Stata 15.1 and would like to request advice regarding a precision issue. I have data of the total scores in an assessment and am dividing the total score by the number of responses to the assessment to create an average assessment score. The calculations I produce need to be compared with other calculations produced in Oracle SQL. The issue I am running into is that a value in the average assessment score such as 4.55 when rounded to 1 decimal place is returned as 4.5, instead of 4.6. The value that Oracle SQL produced was 4.6. All columns are stored in double precision.
I read the help precision Stata documentation, and found that a proposed solution was to create a variable as a float-type to avoid these issues (i.e., section “How count x==float(1.1) solves the problem” of 13.12 Precision and problems therein). This approach worked for the first three observations of the sample data set provided in sample code below, but not the rest. Lastly, another proposed solution is to add 0.5 to the average assessment score, multiply by 10, and use the floor function to ensure that the number rounds down, and then divide by 10. This solution works in all cases, but I would like to understand why Stata is not rounding a number such as 4.55 to 4.6 and if there is a way to round to 4.6 without the solution that uses the floor function. In other words, I would prefer not having to perform additional transformations to the data other than rounding. The following is a sample code that recreates the issue. Attached is a snapshot of the data as well. The variable named “benchmark_avg_assessment_score “ is what I need the calculation of average score to match. These differences in values are relevant, and the values for the average assessment score have to match exactly for my purposes. Thank you in advance for any support provided.
set type double, permanently
set obs 6
gen total_assessment_score=.
replace total_assessment_score=364 if _n==1
replace total_assessment_score=189 if _n==2
replace total_assessment_score=159 if _n==3
replace total_assessment_score=82 if _n==4
replace total_assessment_score=123 if _n==5
replace total_assessment_score=123 if _n==6
gen count_of_scores=.
replace count_of_scores=80 if _n==1
replace count_of_scores=60 if _n==2
replace count_of_scores=60 if _n==3
replace count_of_scores=40 if _n==4
replace count_of_scores=60 if _n==5
replace count_of_scores=60 if _n==6
gen benchmark_avg_assessment_score=.
replace benchmark_avg_assessment_score=4.6 if _n==1
replace benchmark_avg_assessment_score=3.2 if _n==2
replace benchmark_avg_assessment_score=2.7 if _n==3
replace benchmark_avg_assessment_score=2.1 if _n==4
replace benchmark_avg_assessment_score=2.1 if _n==5
replace benchmark_avg_assessment_score=2.1 if _n==6
gen avg_assessment_score=total_assessment_score/count_of_scores
gen avg_assessment_score_rounded_1=round(avg_assessmen t_score, 0.1)
gen avg_assessment_score_rounded_2=round(float(avg_ass essment_score), 0.1)
gen avg_assessment_score_rounded_3=floor((10*avg_asses sment_score)+0.5)/10
0 Response to Precision issues with rounding of a variable
Post a Comment