/*
**    PROC     PWAY
**    Author: Paul Dudgeon    pdudgeon@papyrus.mhri.edu.au
**     Biostatistics & Psychometrics Unit  (voice) +61-3-9389-2914
**     Mental Health Research Institute    (fax)   +61-3-9387-5061
**     Private Bag No. 11
**     Parkville.   Vic.   3052
**     AUSTRALIA
**    Provided without guarantees for free public, non-commercial use.
**    Date: 15 July 1997
**
**    Purpose: Produces an p-way listing of response values
**             to p categorial variables
**
**    Format: dm = pway(ncats,l,c);
**
**    INPUT:   ncat = (p X 1) vector of variables, where ncat[p] indicates
**                    the number of categories in in the p-th variable
**             l    = scalar indicating whether lowest category number is
**                    0 or 1
**             c    = column with the fastest change of values - 1 = first,
**                    0 = last
**
**    OUTPUT:  dm   = (q X p) design matrix, where p = prodc(ncat)
**
**
*/


PROC(1)=pway(ncats,l,c);
   LOCAL s,dm;

   s = ones(prodc(ncats),1);
   dm = (recserrc(seqa(1,1,rows(s))-1,ncats*ones(1,rows(s)))+1)';

   if l eq 0;
      dm = dm - 1;
   endif;

   if c eq 0;
      dm = rev(dm')';
   endif;

   RETP(dm);
ENDP;