Opens in a new tab

Mann-Whitney U Test in Stata
ranksum Output, porder and Exact p-Values

The Mann-Whitney U test in Stata is run through ranksum, which reports rank sums and a z statistic and no U value. The porder option adds a probability of superiority, and small samples call for a decision about exact p-values before the result is worded.

What You'll Need
Stata open with the file loaded
A numeric two-group variable
An ordinal or skewed outcome

IN SHORT

What ranksum Compares and What It Prints

The Mann-Whitney U test compares two independent groups through the ranks of their values, and it addresses whether values in one group tend to be larger than in the other. To run a Mann-Whitney U test in Stata, type ranksum followed by the outcome variable and by() naming the grouping variable; the command reports rank sums and a z statistic, not the U value itself. The porder option adds the estimated probability that a random observation from the first group exceeds one from the second, and an exact p-value is available for small samples in releases that include it. OnlineSPSS, a specialist statistical consultancy for academic, clinical and professional research, carries out and reviews this analysis through an in-house PhD-level statistician.

In This Guide

1
Check the Grouping Variable
2
Look at the Distributions
3
Run ranksum With by()
4
Add porder for the Size
5
Request the Exact p-Value
6
Keep Commands and Output Together

ANALYSIS SUPPORT

Analysis Support for This Method

A two-sample rank test in Stata answers which group tends to score higher. If the difference in means, in the outcome's own units, is the quantity of interest, a t-test with an interval answers it when its assumptions are tenable.

Mann-Whitney U Test

A rank-based comparison of two independent groups, reported with an effect size that suits ranks and chosen for ordinal data or where the mean misleads.

Explore Mann-Whitney U Test
Independent Samples t-Test

A difference in means with its interval and a standardized effect, for a continuous outcome in which the mean is a meaningful summary of each group.

Explore Independent Samples t-Test

BEFORE YOU RUN IT

What ranksum Reads and What It Leaves Out

The command needs two unrelated groups and a rankable outcome, and the wording of the finding depends on choices made before the output appears.

Two Independent Groups Each participant contributes one value in one group. Before-and-after scores on the same people belong to a paired rank test, and clustered rows break the independence behind the p-value.

An Outcome That Can Be Ranked Ordinal ratings and continuous values both qualify, but ratings produce many ties. ranksum adjusts the variance for them, and the share of tied values is worth noting in the report.

A Question About Ranks The test addresses whether one group tends to have larger values. It reads as a comparison of medians only if the two distributions have a similar shape, so the claim is worded to match.

Sample Size and the p-Value With small groups the normal approximation can be rough, so an exact p-value is preferable. The documentation for the release in use says when it is printed and up to what sample size.

IN STATA

How to Run a Mann-Whitney U Test in Stata

ranksum comes with official Stata, so nothing is installed, and the by() variable needs exactly two values among the rows used in the test.

1

Check the Grouping Variable

Tabulate the grouping variable to confirm that it has exactly two groups and to see how many rows each group holds.

Check: A third code, such as one for missing, makes the command stop, and a group of only a handful of rows limits what any rank test can show. Look at both before running ranksum.

2

Look at the Distributions

Draw a box plot or histogram of the outcome in each group and compare shape, spread and the amount of tied values.

Watch: Two groups on a five-point rating can share the same median while the rank test still finds a difference, because ranks use every value and not only the middle one.

3

Run ranksum With by()

Run ranksum with the outcome and by(groupvar), then read the rank sums, the z statistic and Prob > |z|.

Tip: Stata prints each group’s observed rank sum beside its expected value under the null. A group whose observed sum lies well above the expected one is the group tending to score higher.

4

Add porder for the Size

Rerun with porder to display the estimated probability that a random draw from the first group exceeds one from the second.

Tip: On this scale 0.5 means neither group tends to be higher. Name the group order in the sentence, since swapping the groups reflects the value around 0.5.

5

Request the Exact p-Value

For small groups, add exact where the release offers it, and compare the result with the approximate p-value.

Check: Read the documentation of your release to see when the exact value is printed by default and up to what sample size. In the text, name which p-value is reported.

6

Keep Commands and Output Together

Save the commands in a do-file and keep the log, so that the plots, the test and the probability can be regenerated in order.

Tip: Note in the do-file which group comes first in the sort order, because porder and the direction of the rank-sum difference both depend on it.

Stata Commands
tabulate group
graph box outcome, over(group)
ranksum outcome, by(group)
ranksum outcome, by(group) porder
ranksum outcome, by(group) exact

READING THE OUTPUT

Reading the Rank-Sum Table and Its Test Lines

The table of rank sums comes first and the test lines follow, and each line answers a different part of the sentence that reports the result.

Output element What it tells you What to check
Header: Two-sample Wilcoxon rank-sum (Mann-Whitney) test Stata’s label for the test that ran (wording varies by release) That one name for the test is used throughout the manuscript
Group rows: obs, rank sum, expected Group sizes, observed rank sums and the sums expected under the null That obs matches the n per group after missing values
unadjusted variance, adjustment for ties, adjusted variance Variance of the rank sum before and after the tie correction That the tie line is present when ratings repeat
Null line, Ho: or H0: by release, outcome(group==0) = outcome(group==1) The null that the two distributions are the same That the alternative is worded as a tendency to be higher
z Standardized difference between observed and expected rank sums That it is not labeled as U in the text
Two-sided Prob line under z Two-sided approximate p-value That it is quoted unless the exact value is chosen
Exact prob line (when printed; wording varies by release) Exact two-sided p-value from the randomization distribution That small-sample reports use and name it
P{outcome(group==0) > outcome(group==1)} (porder) Estimated probability of superiority That the group order is named

REPORTING

Wording a Rank-Sum Result in a Report

The statistic is easy to quote, but the claim a reader takes from the sentence depends on the size, the p-value used and the shape of the groups.

Report the test as the Mann-Whitney U test run with ranksum, giving the group sizes, z = ___ and p = ___, and say whether the approximate or the exact p-value is quoted. Add the probability that a random observation from group A exceeds one from group B, P = ___ from porder, since the statistic alone does not show how far the groups separate. Give a median and interquartile range for each group as description, and say that they are descriptive because the test addresses ranks. State the handling of ties and the rows excluded for missing values. Word the finding as one group tending to score higher, and reserve a statement about medians for groups with similar distribution shapes. If a journal expects U, say how it was obtained from the rank sums.

Exact or Approx. pGroup Order NamedTie Handling StatedDescriptives LabeledSuperiority Estimate

COMMON MISTAKES

Traps in Carrying ranksum Output Into a Report

The test is simple to run and easy to describe loosely, so the six traps below sit mostly in the wording of the result.

U Reported From z Output ranksum prints a z statistic and rank sums, with no U value. A sentence quoting U = ___ that was never computed from the rank sums reports a number the output does not contain.

porder Quoted as a Correlation The porder value is a probability centered on 0.5, while a rank-biserial correlation runs from -1 to 1 and is centered on 0. Quoting one under the other's name misstates the size, so the measure is named with its scale.

No Size Beside the p-Value A p-value does not say how far the groups separate. The porder probability, or another effect size suited to ranks, is reported beside it, with the group order named.

Approximate p in a Tiny Sample With a handful of rows per group the normal approximation can be rough. The exact p-value is requested where the release supports it, and the choice is stated.

Ties Left Undescribed Ordinal ratings repeat heavily. Stata applies the tie adjustment, yet a report silent on how many values are tied hides how little the ranks can separate the groups.

Dependent Rows Treated as Independent Two measurements on the same participants, or participants nested in clinics or classes, break the independence behind the test. A paired or clustered approach is chosen from the design.

Rank-sum output saved, the wording still loose? Independent Statistical Review compares the sentence in your draft with what ranksum printed.

Request an Assessment

What's Next

Three or more independent groups are compared with the Kruskal-Wallis Test.
Two measurements on the same participants lead to the Wilcoxon Signed-Rank Test.
An ordered outcome with several predictors is modeled by Ordinal Regression.

FREQUENTLY ASKED QUESTIONS

Questions About ranksum, porder and Exact Values

Stata names the test differently from most papers and prints no U, so its output needs translating into a journal's terms, and small samples add a choice of p-value.

ranksum does: it ranks the combined sample, sums the ranks in each group and reports a z statistic and a p-value. With the ranksum command in Stata, the by() option names the grouping variable. The Mann-Whitney U and the rank-sum forms of the test lead to the same p-value, so Stata’s label differs from the name in most papers without changing the result.

Add the porder option to ranksum, which displays an estimate of the probability that a random value from the first group exceeds one from the second. A value of 0.5 means that neither group tends to be higher. The first group is the one with the lower code, so the order is named in the text.

In small samples, where the normal approximation behind the z statistic may be rough. Releases that offer the exact option for ranksum compute it from the randomization distribution, and the documentation of your release gives the sample sizes at which it is printed. Report which p-value is quoted.

An in-house PhD-level statistician at OnlineSPSS goes through the output, the distribution plots and the sentence in your draft, and compares the wording with the effect size and the p-value reported. The stage falls under Results Reporting & Interpretation, and the conclusion drawn for your field remains yours.

Your data and project details are handled confidentially, and project information is not published. Researcher names and institutions are not used publicly without permission. An NDA is available where required and can be arranged before sensitive materials are shared. Read more about confidentiality and data security.

You share your research question, design and relevant materials, such as the dataset or the output you have, and a statistically trained Research Coordinator reviews the request and responds the same day. The analysis, deliverables, timeline and one fixed project fee are then confirmed before any work begins, so the timeline follows from the agreed scope.

Still have a question? Find more answers in our full FAQ.

View All FAQs →

NEXT STEP

Does the Sentence Say
More Than Ranks Can?

A rank test supports a statement about one group tending to score higher most directly, so the sentence in the draft is held against what ranksum actually printed.

No Commitment Required

Strictly Confidential

Fixed Project Fee

In-House PhD-Level Statisticians

Same-Day Response

Serving Researchers Since 2014