Skip to main content
v2026.11,986 entries · CC-BY 4.0

Mann-Whitney (ranksum) Test in Stata

Stata’s ranksum command syntax for the Mann-Whitney test, reading the z statistic it reports instead of a U value, and the tie correction Stata applies to the variance automatically.

Written and maintained by CASRAI Editorial Board

Published · Last updated

On this page

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

To run the Mann-Whitney U test in Stata, type ranksum varname, by(groupvar), where varname is the outcome and groupvar has exactly two categories. Stata prints each group’s rank sum, the expected rank sum, a z statistic and the two-sided p-value (Prob > |z|). With 200 or fewer observations it also prints an exact p-value.

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:

  • exact requests the exact permutation-based p-value instead of the normal approximation. Stata’s manual says the exact p-value is computed by default when the combined sample size is 200 or fewer, and that you must specify exact to get it for larger samples (up to 1,000, with computation time growing quickly as the sample approaches that size); noexact suppresses it. The exact p-value is reported in addition to the normal-approximation p-value, not instead of it (Stata Base Reference Manual, ranksum).
  • porder reports 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.

Ties also do not block the exact p-value. Stata ranks tied values with averaged ranks and builds the exact p-value from the randomization distribution of the rank sum over all ways of assigning those ranks to the first group, so it remains available when ties are present. The two p-values usually agree closely; report whichever you decided on in advance, and state which one it is.

Worked example: reading real ranksum output

Stata’s own manual illustrates the command with a fuel-additive experiment: 24 cars, 12 with the additive (treat = 1) and 12 without, and mpg as the outcome. The command and its output are:

. ranksum mpg, by(treat)

Two-sample Wilcoxon rank-sum (Mann-Whitney) test

       treat |      Obs    Rank sum    Expected
-------------+---------------------------------
   Untreated |       12         128         150
     Treated |       12         172         150
-------------+---------------------------------
    Combined |       24         300         300

Unadjusted variance      300.00
Adjustment for ties       -4.04
                      ----------
Adjusted variance        295.96

H0: mpg(treat==Untreated) = mpg(treat==Treated)
             z =  -1.279
    Prob > |z| =   0.2010
    Exact prob =   0.2117

How to read it:

  • The two rank sums add to 300, the total of ranks 1 to 24. Under the null each group would expect 150; the untreated cars sit at 128 and the treated cars at 172.
  • The variance block shows the tie correction at work: the unadjusted variance of 300.00 is reduced by 4.04 to 295.96, and z is computed from the adjusted figure.
  • z = -1.279 gives a two-sided p of 0.2010, and the exact p-value is 0.2117. Both are above 0.05, so the test does not reject equality of the two mpg distributions. With only 24 observations the exact value is computed by default, and the manual notes that the normal approximation is similar here.

To obtain the U statistic that SPSS and R report, use the manual’s relation U = T – n1(n1 + 1)/2, where T is the rank sum of the first group. For the untreated group above, U = 128 – 12(13)/2 = 50. Adding porder reports the probability that a random draw from the first group exceeds one from the second, which Stata computes as U/(n1n2); here that is 50/144, about 0.35. The same experiment run through the median test, median mpg, by(treat) exact, gives Pearson chi2(1) = 0.6667 (p = 0.414) and Fisher’s exact p = 0.684, the same conclusion from a less powerful test (Stata manual).

When you write the result up, report the group sizes, the z statistic and p-value, and an effect size; the APA statistics reporting guide gives the formats, and choosing and reporting an effect size covers the options. The assumptions behind the equal-shapes caveat are set out in Mann-Whitney U test assumptions, and nonparametric statistics places the test among its alternatives.

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.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Mann-Whitney (ranksum) Test in Stata

2 free questions a day, no account.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →