BookmarkSubscribeRSS Feed
DB_ECON
Calcite | Level 5
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
3 REPLIES 3
SPR
Quartz | Level 8 SPR
Quartz | Level 8
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
rosiecao2509
Fluorite | Level 6
Hi,

I am trying to run your code, but the value for numneg and n is missing. Can you advise me on this please?
PGStats
Opal | Level 21

The runs test is available in the autoreg procedure which does support by-processing. It should go like;

proc autoreg data=myData;

by myByVariable;

model myVariable= / runs;

run;

 

Note proc autoreg requires licence to SAS/ETS.

PG

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!

Register now

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 5338 views
  • 2 likes
  • 4 in conversation