简体   繁体   中英

SAS Chi Square Test

I have the following dataset on which I intend to perform a chi square test (all variables being categorical).

Indicator   Area    Range1  Range2
0   A   17-25   25-50
0   A   17-25   25-50
0   A   17-25   25-50
0   A   17-25   25-50
0   A   0-17    25-50
1   B   17-25   25-50
1   B   0-17    17-25
1   B   17-25   25-50     

The test is required to be perform at all levels namely for range1,range2 & area.One way to do it is to create a macro to do the same.But I have around 300 variables & to call the macro 300 times is not efficient. The code that I use for 3 variables is as follows:

options mprint mlogic symbolgen;
%macro chi_test(vars_test);
proc freq data =testdata.AllData;
tables &vars_test*Indicator/ norow nocol nopercent chisq ;
output out=stats_&vars_test &vars_test PCHI;
run;
data all_chi;
set stats_:;
run;
%mend chi_test;
%chi_test(Range1);
%chi_test(Range2);
%chi_test(Area);

Can any one help out?

Why not just transpose the data and use BY group processing.

First add a unique row identifier so that PROC TRANSPOSE can convert your variables to a single column.

data have_extra;
  row+1;
  set have;
run;

proc transpose data=have_extra out=tall ;
  by row indicator ;
  var area range1 range2 ;
run;

Then order the records by the original variable name.

proc sort; by _name_ ; run;

Then you can run your CHI-SQ for each of your original variables.

proc freq data =tall ;
  by _name_;
  tables col1*Indicator/ norow nocol nopercent chisq ;
  output out=all_chi  PCHI;
run;

If all your variables are categorical then you can use _all_ in the tables statement, along with ods output for the dataset. This creates a single dataset with all the combinations of variables * Indicator.

If you wanted, you can apply dataset options (where=, keep=, drop= etc) against the output dataset.

data have;
input Indicator   Area $   Range1 $ Range2 $;
datalines;
0   A   17-25   25-50
0   A   17-25   25-50
0   A   17-25   25-50
0   A   17-25   25-50
0   A   0-17    25-50
1   B   17-25   25-50
1   B   0-17    17-25
1   B   17-25   25-50
;
run;

ods select chisq;
ods output chisq=want;
proc freq data=have;
tables _all_*Indicator/ norow nocol nopercent chisq;
run;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM