/* Runs test(Greater than 0.5 or not) for U(0,1) sequence */ new; cls; x=rndu(100,1); call halfrunstest(x); proc halfrunstest(x); local n,y,nA,nB,R,ER,varR,i,Z,pval; if ismiss(x); errorlog "Warning: missing data found."; x=packr(x); endif; if not(x>=0 and x<=1); errorlog "Error: This is not U(0,1)."; retp("."); endif; n=rows(x); y=(x.>0.5); nA=sumc(y); nB=n-nA; R=1; i=2; do while i<=n; if y[i-1]/=y[i]; R=R+1; endif; i=i+1; endo; ER=2*nA*nB/(nA+nB)+1; varR=2*nA*nB*(2*nA*nB-nA-nB)/((nA+nB)^2*(nA+nB-1)); Z=(R-ER)/sqrt(varR); pval=2*(1-cdfn(abs(Z))); /* results */ print "Runs Test(x>0.5 or not):"; print/rz "n =" n; print/rz "nA =" nA; print/rz "nB =" nB; print/rz "R =" R; print "Z =" Z; print "pval=" pval; if nA<20 or nB<20; print "(Waring: P-value above is not the case. See Table instead.)"; endif; retp(Z); endp;