The test proceeds as follows:
(a) 
The pooled sample of all the observations is ranked. Average ranks are assigned to tied scores. 
(b) 
The ranks of the observations in each sample are summed, to give the rank sums ${R}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,k$. 
(c) 
The Kruskal–Wallis' test statistic $H$ is computed as:
i.e., $N$ is the total number of observations. If there are tied scores, $H$ is corrected by dividing by:
where $t$ is the number of tied scores in a sample and the summation is over all tied samples. 
G08AFF returns the value of
$H$, and also an approximation,
$p$, to the probability of a value of at least
$H$ being observed,
${H}_{0}$ is true. (
$H$ approximately follows a
${\chi}_{k1}^{2}$ distribution).
${H}_{0}$ is rejected by a test of chosen size
$\alpha $ if
$p<\alpha \text{.}$ The approximation
$p$ is acceptable unless
$k=3$ and
${l}_{1}$,
${l}_{2}$ or
${l}_{3}\le 5$ in which case tables should be consulted (e.g., O of
Siegel (1956)) or
$k=2$ (in which case the Median test (see
G08ACF) or the Mann–Whitney
$U$ test (see
G08AHF) is more appropriate).
 1: X(LX) – REAL (KIND=nag_wp) arrayInput
On entry: the elements of
X must contain the observations in the
K samples. The first
${l}_{1}$ elements must contain the scores in the first sample, the next
${l}_{2}$ those in the second sample, and so on.
 2: LX – INTEGERInput
On entry: $N$, the total number of observations.
Constraint:
${\mathbf{LX}}={\displaystyle \sum _{i=1}^{k}}{\mathbf{L}}\left(i\right)$.
 3: L(K) – INTEGER arrayInput
On entry: ${\mathbf{L}}\left(\mathit{i}\right)$ must contain the number of observations ${l}_{\mathit{i}}$ in sample $\mathit{i}$, for $\mathit{i}=1,2,\dots ,k$.
Constraint:
${\mathbf{L}}\left(\mathit{i}\right)>0$, for $\mathit{i}=1,2,\dots ,k$.
 4: K – INTEGERInput
On entry: $k$, the number of samples.
Constraint:
${\mathbf{K}}\ge 2$.
 5: W(LX) – REAL (KIND=nag_wp) arrayWorkspace
 6: H – REAL (KIND=nag_wp)Output
On exit: the value of the Kruskal–Wallis test statistic, $H$.
 7: P – REAL (KIND=nag_wp)Output
On exit: the approximate significance, $p$, of the Kruskal–Wallis test statistic.
 8: IFAIL – INTEGERInput/Output

On entry:
IFAIL must be set to
$0$,
$1\text{ or}1$. If you are unfamiliar with this parameter you should refer to
Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value
$1\text{ or}1$ is recommended. If the output of error messages is undesirable, then the value
$1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is
$0$.
When the value $\mathbf{1}\text{ or}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit:
${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see
Section 6).
If on entry
${\mathbf{IFAIL}}={\mathbf{0}}$ or
${{\mathbf{1}}}$, explanatory error messages are output on the current error message unit (as defined by
X04AAF).
For estimates of the accuracy of the significance
$p$, see
G01ECF. The
${\chi}^{2}$ approximation is acceptable unless
$k=3$ and
${l}_{1},{l}_{2}$ or
${l}_{3}\le 5$.
This example is taken from
Moore et al. (1972). There are
$5$ groups of sizes
$5$,
$8$,
$6$,
$8$ and
$8$. The data represent the weight gain, in pounds, of pigs from five different litters under the same conditions.