SAS Independence Test Loop

时间:2016-08-31 12:27:27

标签: sas

I am a new user of SAS. Hope you could help me with this. For example, I have a data set with 35 categorical variables.
cat1 cat2 cat3 cat4 ... cat35
I want to create a table consisting the result of independence test between all possible combination of two categorical variables and its p-values.
I used proc freq but I can only do it manually, two variables at a time. To save time, I'm thinking to use macro, but I don't know how to do it.

The output is a table
Variable1 | Variable2 | P-value
----------------------------------------------
cat1 | cat2 | p-value of cat1 & cat2
cat1 | cat3 | p-value of cat1 & cat3
...
cat34 | cat35 | p-value of cat34 & cat35

Open for suggestions. Thanks!

Edit: My current code is
proc freq data = mydata; tables cat1*cat2 / chisq; output out=myoutput pchi cramv; run;
proc sql; create table myoutput as select "cat1" as X, "cat2" as Y, P_PCHI from myoutput; quit;
This will give a table
X | Y | P-value
-----------------------------------
cat1 | cat2 | p-value of cat1 & cat2

2 个答案:

答案 0 :(得分:1)

我认为您应该可以使用以下语法对proc freq调用中的tables语句执行此操作:

tables (cat1-cat35) * (cat1-cat35);

我已调整您的代码以捕获SAS数据集中的所需输出:

data example;
  do cat1 = 'a','b','c';
    do cat2 = 'd','e','f';
      do cat3 = 'g','h','i';
        output;
      end;
    end;
  end;
run;

ods output chisq = mychisq(where = (statistic = 'Chi-Square'));

proc freq data = example; 
tables (cat1-cat3) * (cat1-cat3) / chisq; 
output out=myoutput pchi cramv; 
run;

这会以稍微不同的格式为您提供输出,但您应该可以从那里对其进行排序。

答案 1 :(得分:0)

循环所有对的一种方法是

do i=1 to 34;
     do j=i+1 to 35;
       .....