How to return the percentile of variable in Stata? For example, say my variable is bmi,…
- User
How to return the percentile of variable in Stata? For example, say my variable is bmi, where is the 1% mark, where the 25%, and where the 50% (median)? Thanks.
Assistant
In Stata, you can use the
_pctilecommand to find the percentiles of a variable. Here's how you can do it:stata_pctile bmi, percentiles(1 25 50)This command will return the values at the specified percentiles.
Alternatively, you can use the
summarizecommand with thedetailoption, which provides a comprehensive summary including percentiles:statasummarize bmi, detailThis will give you:
- 1st percentile
- 25th percentile (first quartile)
- 50th percentile (median)
- 75th percentile (third quartile)
- And other detailed statistics
If you want to store these values in local macros for further use, you can do:
stata_pctile bmi, percentiles(1 25 50) local p1 = r(r1) local p25 = r(r2) local median = r(r3)Each method has its advantages:
_pctileis more direct if you just want specific percentilessummarize, detailprovides a more comprehensive statistical overview