标题: 如何通过程序直接计算频数,并进行卡方检验 [打印本页] 作者: shiyiming 时间: 2011-5-25 23:51 标题: 如何通过程序直接计算频数,并进行卡方检验 如题,我想了一种解决办法但是觉得有点复杂了,求更简单的方法。
data tmp;
input y x@@;
cards;
1 0 1 1 0 0 1 0
1 0 0 1 0 0 1 0
1 0 1 0 1 0 1 0
1 1 1 0 0 0 0 0
1 0 1 0 0 0 0 0
;
data tm;
set tmp end=last;
retain n1 0;
retain n2 0;
retain n3 0;
retain n4 0;
if x=0 and y=0 then n1+1;
if x=0 and y=1 then n2+1;
if x=1 and y=0 then n3+1;
if x=1 and y=1 then n4+1;
if last then output;
run;
data tm2(keep=a b x);
set tm;
a=0 ;
b=0;
x=n1;
output;
a=0 ;
b=1;
x=n2;
output;
a=1 ;
b=0;
x=n3;
output;
a=1 ;
b=1;
x=n4;
output;
run;
proc freq;
weight x;
tables a*b/chisq ;*measures;
run;作者: shiyiming 时间: 2011-5-26 07:32 标题: Re: 如何通过程序直接计算频数,并进行卡方检验 Firstly, seemingly we can use FREQ directly;
When to compute frequency at first in our own code, we may consider SQL more convenient; the ways are like the followings.
I hope it is helpful.
JingJu
[code:36cmh6gr]proc freq data =tmp;
tables x*y/chisq ;
run; [/code:36cmh6gr]
[code:36cmh6gr]proc sql;
create table tmp1 as
select count(*) as w, x, y from tmp group by x, y;
quit;
proc freq data =tmp1;
weight w;
tables x*y/chisq;
run;[/code:36cmh6gr]作者: shiyiming 时间: 2011-5-26 14:15 标题: Re: 如何通过程序直接计算频数,并进行卡方检验 Thank you very much!