expanding a SAS macro for Kruskal–Wallis analysis with Dunn's post hoc test

Posts: 66

expanding a SAS macro for Kruskal–Wallis analysis with Dunn's post hoc test

For those who would like to run a Kruskal-Wallis (KW) test on their non-parametric data, followed by a Dunn's post hoc test, Elliott has published a macro KW_MC on this site www.alanelliott.com/kw

Let's say you run the macro KW_MC (Kruskal-Wallis with Multiple Comaprison) on data containing 3 groups (A, B, C). This will produce an overall comparison across the 3 groups (a small table with title Kruskal-Wallis Test) and a Dunn's post hoc test in 3 paired comparison (A vs B, B vs C, C vs A, the table on the last page with the title Kruskal-Wallis Tied Ranks). This macro works well for data with just one group variable, which is specified by a single global macro variable "GROUP". My data are, however, more complex in that I have 4 grouping variables:

  1. exp_name (project)
  2. replicate
  3. trt_group (groups)
  4. respType (type of the response variable)

My data are in long-format and all the response variables are grouped into 2 column- one with the type (respType) and the other with the value (respVar).

I would like to run the KW analysis across all the subgroup combinations (exp_name x respType x trt_group x replicate) and for each of these combinations run a Dunn's post hoc test between any of the two groups (in trt_label). I've tweaked the PROC NPAR1WAY (see KW_MC.sas) and results of the overall comparison are output to the dataset 'kw_overall.' However, I got stuck when attempting to modify the codes following the the PROC NPAR1WAY. I didn't even find a dataset that contains the complete statistics of Duntrtn's post hoc (however, these are passed to the RTF file). Can anyone please advise on how to tweak the macro so it can take multiple grouping variables (in my case: exp_name x replicate x trt_group x respType )?  Although this question may sound like a statistical one, it is actually more about macro writing skills.

My explanation for each file:

  1. data.xls contains a subset of my data
  2. KW_MC.sas contains Elliott's macro codes. (I've tweaked the PROC NPAR1WAY but leaving the rank part unchanged)
  3. KW_MC_example3.sas is where the macro is invoked and output is rearranged into a table
  4. sasrtf.rtf contains the output stored (you can see a KW test is done correctly for each subgroup combination, but Dunn's test, on the last page, is incorrect. It has summed up all the wilcoxon scores and produced just one table)
  5. KW_overall_mbExpansion_someData.rtf shows the overall comparisons for all the subgroup combinations. The first 3 columns are the grouping variable (exp_name, respType,  replicate; trt_group ignored) and the last two are statistics of the overall comparison (or Kruskal-Wallis Test). My goal is to merge this table with the Dunn's post hoc results. I am needing help to get the output from the Dunn's test.

Thanks in advance!!

Respected Advisor
Posts: 2,655

Re: expanding a SAS macro for Kruskal–Wallis analysis with Dunn's post hoc test

Some points to consider:

Kruskal-Wallis is set up for a one-way analysis of variance.  Multiple levels of a single variable.  If you have more than one independent variable, it isn't appropriate.  If you have equal replication for all levels, a Friedman test can be applied for a two-way analysis of variance.

So long as you have equal replication in "exp_name x respType x trt_group x replicate" for all levels, you could treat it as a one-way analysis (like a means model in GLMMs).  However, I don't see the reason for including respType--it seems to code for a response variable, and its inclusion opens up a multivariate approach (which, at least the ranks should overcome)--or replicate, which in the sample data has the same value(=1) for all observations.  Create a dummy variable that has all levels of the four-way interaction (3x6x3x1 for the example data) and pass that to the macro.  Now, unfortunately I don't remember if rank transformation is well-preserved as far as coming up with main effect and interaction estimates.  I know NPAR1WAY doesn't support estimates as in the linear model procs.

I would really consider rank transforming the data prior to analysis, and using PROC GLIMMIX or something similar, to analyze this data.

Steve Denham

New Contributor
Posts: 2

Re: expanding a SAS macro for Kruskal–Wallis analysis with Dunn's post hoc test



I am attempting to run the Dunn Macro package of code right now. I am about half way through but have gotten snagged on a data step. It seems like there are more coming up as well. 


data KW_MC_TMP5;set KW_MC_TMP5;
1294 N=_N_;
1295 keep n mean1-mean&groups;
WARNING: Apparent symbolic reference GROUPS not resolved.
ERROR: Missing numeric suffix on a numbered variable list (mean1-mean).
ERROR 22-322: Syntax error, expecting one of the following: a name, ;, _ALL_, _CHARACTER_,

ERROR 200-322: The symbol is not recognized and will be ignored.


Is there a way to resolve this? I am relatively new to SAS and trying to learn while using (not highly recommended) but any advice with this will be greatly appreciated. 

The upcoming steps have similar synthax. 


data KW_MC_TMP6;set KW_MC_TMP6;
keep n n1-n&groups;


data KW_MC_TMP7;set KW_MC_TMP7;
keep n gp1-gp&groups;





Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation