Contributor
Posts: 20

Wald-Wolfowitz (or Runs) test for randomness

The following SAS link provides the code to implement Run's test. Does anyone know how to implement this code with a by group option in the data step. In otherwords, is it possible to compute the test statistic simultaneously by an id variable.

Wald-Wolfowitz (or Runs) test for randomness
http://support.sas.com/kb/33/092.html
Super Contributor
Posts: 365

Re: Wald-Wolfowitz (or Runs) test for randomness

Hello DB_ECON,

This is what I've got formally applying BY statement:
[pre]
data one;
do id=1 to 5;
drop i;
do i=1 to 75;
d=rannor(123);
n=75;
output;
end;
end;
run;
proc standard data=one out=two mean=0;
var d;
by ID;
run;
data runcount;
keep ID runs numpos numneg n;
set two end=last;
retain runs 0 numpos 0;
if FIRST.ID then do; runs=0; numpos=0; ld=.; end;
else do; prevpos=( ld GE 0 ); currpos=( D GE 0 ); ld=lag(D); end;
if currpos and prevpos then numpos+1;
else if currpos and ^prevpos then do;
runs+1;
numpos+1;
end;
else if ^currpos and prevpos then runs+1;
if last.ID then do;
numneg=n-numpos;
output;
end;
by ID;
run;
data waldwolf;
label z='Wald-Wolfowitz Z' pvalue='Pr > |Z|';
set runcount;
mu = ( (2*numpos*numneg) / (numpos+numneg) ) + 1;
sigmasq = ( (2*numpos*numneg) * (2*numpos*numneg-numneg-numpos) ) /
( ( (numpos+numneg)**2 ) * (numpos+numneg-1) );
sigma=sqrt(sigmasq);
drop sigmasq;
if N GE 50 then Z = (Runs - mu) / sigma;
else if Runs-mu LT 0 then Z = (Runs-mu+0.5)/sigma;
else Z = (Runs-mu-0.5)/sigma;
pvalue=2*(1-probnorm(abs(Z)));
by ID;
run;
title 'Wald-Wolfowitz Test for Randomness';
title2 'H0: The data are random';
proc print data=waldwolf label noobs;
id ID;
var z pvalue;
format pvalue pvalue.;
run;
[/pre]
Sincerely,
SPR
Discussion stats