Written and maintained by CASRAI Editorial Board
Last updated
Stata does not have a command called mannwhitney. The Mann-Whitney U test — also known as the Wilcoxon rank-sum test, since they are the same test derived two different ways — runs in Stata under the command ranksum. If you search Stata’s help for “Mann-Whitney” and come up empty, this is why: the command name follows the rank-sum derivation (Wilcoxon, 1945), not the U-statistic one (Mann and Whitney, 1947). This guide covers the ranksum syntax, how to read its output — including the z statistic Stata reports instead of a raw U value — and the tie correction Stata applies automatically to the test’s variance whenever ranks are tied.
The ranksum command syntax
The basic form compares a continuous or ordinal variable across two independent groups defined by a second variable:
ranksum varname, by(groupvar)
groupvar must take exactly two values — ranksum is a two-sample test only. For more than two independent groups, Stata’s nonparametric equivalent is the Kruskal-Wallis test, run via kwallis, not ranksum.
Two options extend the base command:
exactrequests the exact permutation-based p-value instead of the normal approximation. Stata computes the exact p-value automatically once the combined sample size is small enough (roughly under 200 observations across both groups combined); the option forces it beyond that threshold, which can be slow on larger samples.porderreports an additional probability-order statistic: the estimated probability that a randomly drawn observation from the first group exceeds one from the second group. It’s a directly interpretable effect-size-style number alongside the significance test.
A worked example, comparing a continuous outcome across a two-level treatment variable:
ranksum score, by(treatment)
Reading the output
ranksum prints a compact block rather than a single test statistic. The parts that matter:
- obs, rank sum, expected — for each group, Stata lists the number of observations, the actual sum of ranks assigned to that group’s observations, and the sum of ranks you’d expect under the null hypothesis of no difference between groups (which is just the group’s share of the total rank sum if group membership were unrelated to rank).
- z — the test statistic itself, computed from how far the observed rank sum for one group deviates from its expected value, standardized by the variance of that rank sum under the null. This is what you report and compare against a critical value, or read the paired p-value directly from.
- Prob > |z| — the two-sided p-value corresponding to that z statistic.
There is no U statistic in the printed output at all — Stata reports the equivalent test entirely through the rank-sum framing (observed vs. expected rank sum, and the z built from it), not the U-statistic framing SPSS and R’s wilcox.test() typically show by default. The two are mathematically convertible, but if you’re writing up results for a venue that expects a reported U value, you’ll need to convert Stata’s rank sum to U yourself (U = rank sum − n(n+1)/2 for the relevant group) or use the porder effect-size statistic instead, which is often what an equivalent-U figure is being requested for in the first place.
The tie correction Stata applies automatically
The Wilcoxon rank-sum test’s z statistic depends on the variance of the rank-sum statistic under the null hypothesis. When there are no tied values in the data, that variance has a simple closed form. Real data routinely has ties — repeated scale scores, rounded measurements, count-like outcomes — and ties reduce that variance relative to the no-ties case, because tied observations get the same (averaged) rank rather than spreading out across the full range of possible ranks.
Stata’s ranksum does not require a separate option to account for this. It always uses the tie-adjusted variance to compute z, whether or not any ties are present in your data — when there are no ties, the adjustment term is simply zero and the formula reduces to the standard case automatically. You do not need to check your data for ties first, run a different command, or add a flag; the correction is built into the z calculation every time the command runs. This is worth knowing specifically because it’s easy to assume, coming from software that surfaces a “ties present, exact p-value not computed” warning, that Stata’s silence on the subject means it isn’t handling ties at all — it is, it just doesn’t narrate it.
One practical consequence: if your data has a meaningful number of ties, the exact permutation-based p-value (exact option) becomes less reliable as an alternative, because standard exact-test algorithms for the rank-sum statistic assume no ties; Stata falls back to the normal approximation with the tie-adjusted variance in that situation instead of a true exact calculation. For data with many ties (e.g. a coarse ordinal scale), the tie-adjusted normal approximation from the default ranksum output is generally the number to report, not a forced exact result.
Assumptions, briefly
Mann-Whitney/Wilcoxon rank-sum tests whether the two groups’ distributions are stochastically equal — in practice, most commonly interpreted (and reported) as a difference in medians. That interpretation is only strictly valid when both groups’ distributions have a similar shape and spread; if the shapes differ substantially, a significant result reflects a difference in distribution shape generally, not necessarily a difference in central tendency. Check this visually (a simple graph box score, over(treatment) or two overlaid histograms) before reporting the result as a median difference specifically. CASRAI covers the underlying assumption and reporting question in more depth in the SPSS version of this guide, which walks through the same equal-shapes caveat with worked output.
The median-test alternative
Stata has a second, distinct nonparametric two-sample command worth knowing about: median groupvar1, by(groupvar2), which is not the same test as ranksum despite the superficial name similarity to “median difference.” The median command runs the median test — it collapses the pooled sample into “above” and “below” the grand median and tests whether group membership is independent of that split, via a chi-squared test (Pearson’s, with a continuity-corrected version also available). This is a substantially less powerful test than ranksum because it discards the actual rank ordering within each side of the split and reduces every observation to a binary above/below indicator. Its main practical use is as a check when you specifically want a test that makes no assumption about the two groups’ distribution shapes being similar — a robustness cross-check against a ranksum result, not a routine substitute for it. median also has its own tie-handling option, medianties(), controlling how observations exactly equal to the grand median are allocated (below, above, dropped, or split) — a genuinely separate tie-handling mechanism from the one built into ranksum, since the two commands are testing different things.
Frequently asked questions
What is the Stata command for the Mann-Whitney U test?
ranksum. Stata names it after the Wilcoxon rank-sum derivation of the test rather than the Mann-Whitney U derivation; both names refer to the same statistical test.
Why doesn’t Stata report a U statistic for the Mann-Whitney test?
Because ranksum implements the test through the rank-sum framing, not the U-statistic framing. It reports observed and expected rank sums and a z statistic instead. The two framings are mathematically equivalent and convertible, but Stata’s printed output never shows U directly.
Does Stata’s ranksum command correct for tied values automatically?
Yes. ranksum always computes z using the tie-adjusted variance of the rank-sum statistic, with no separate option required — when there are no ties in the data, the adjustment term is zero and the result matches the untied formula exactly.
What’s the difference between ranksum and median in Stata?
ranksum is the Mann-Whitney/Wilcoxon rank-sum test, comparing full rank orderings between two groups. median runs a separate, less powerful test that only checks whether each group falls above or below the pooled grand median more or less often than chance, via a chi-squared test.
How many groups can ranksum compare?
Exactly two. For more than two independent groups, use Stata’s kwallis command for the Kruskal-Wallis test instead.
For the equivalent test run in SPSS, see CASRAI’s guide to the Mann-Whitney U test in SPSS. For comparing more than two groups nonparametrically, see the Kruskal-Wallis test. For the paired-samples nonparametric equivalent, see the Wilcoxon signed-rank test. For the parametric alternative and when to prefer each, see running a t-test in Stata and CASRAI’s general t-test guide. For checking the assumption that motivates choosing a nonparametric test in the first place, see testing the normality assumption and levels of measurement. If you’re deciding which statistical package to run this in, see CASRAI’s comparison of SPSS vs. Stata.








