My apology
I did not post an article in September 2026 due to travel, so I consider this is a make-up submission. I expect to post another article for October 2026; consider this article the “September 2026” article.
Background
I recently ran into an issue when I was creating a variable using a ratio. In many cases, we get repeating values or an infinitely long string of numbers in a ratio calculation. For example, the symbol π is a ratio of the circumference of a circle to its diameter (3.142…). So, when we generate variables that are a ratio of two other variables, there’s a good chance that we will have a value with either repeating values or very long infinitely string of numbers (e.g., π).
In Stata, when we try to use these ratios for some of our estimations, we might get an error.
For example, I wanted to see what the average price is for a car with a weight to length ratio of 2930 / 186 = 15.752688172043. When I looked at the data, I can see that the price is $4099 for a vehicle with a weight to length ratio of 15.752688172043. However, when I use the Stata command to perform this simple evaluation, I get an error.
/ CLEAR data clear all // LOAD data sysuse auto2.dta // Summarize data sum weight sum length // Generate a ratio with weight / length gen wl_ratio = weight / length tab wl_ratio // There are 71 unique wl_ratio values // Summarize price per wl_ratio value summarize price if wl_ratio == 15.752688172043
What’s going on?
This is a problem of precision, particularly with values that have no finite length and how Stata mishandles these. In this example, I am asking Stata to give me the average price for vehicles with a weight to length ratio of 2930 / 186 = 15.752688172043. Instead of yielding a value of $4099, I get nothing in the output.
Solution – Use the “float” function
I was a little perplexed by this problem, and I was about to give up on a solution when I asked Claude to help me solve this problem. Claude’s response was to apply the “float” function, which I never heard of.
According to the Stata help file:
To implement this, I had to include float for the wl_ratio in my Stata command line:
// Summarize price per wl_ratio value using "float" summarize price if float(wl_ratio) == float(15.752688172043)
This generated the correct answer and fixed the precision issue.
Conclusions
When you generate calculations where the precision is needed to perform further calculations, it might be helpful to use the float function to mitigate potential errors. In the above example, I illustrate how the float function allowed me to generate the correct answer for a specific ratio. Without the float function, Stata is unable to determine the exact value and result in an error. So, if you run into a strange result based on previous calculations (in particular, ratio calculations), make sure that Stata is identifying the ratios correctly using the float function.
Here is the full Stata code for this example:
// CLEAR data clear all // LOAD data sysuse auto2.dta // Summarize data sum weight sum length // Generate a ratio with weight / length gen wl_ratio = weight / length tab wl_ratio // There are 71 unique wl_ratio values // Summarize price per wl_ratio value summarize price if wl_ratio == 15.752688172043 // Summarize price per wl_ratio value using "float" summarize price if float(wl_ratio) == float(15.752688172043)
Acknowledgements
I used Claude (Opus 5.5) to figure out this problem, and I wanted to give it credit.
Disclosures and Disclaimers
This is a work in progress, and I may update this in the future.
This is for educational purposes only.