Programming the statistical procedures from SAS

Chi Square

Reply
Contributor
Posts: 41

Chi Square

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

IndicatorAreaRange1Range2
0A17-2525-50
0A17-2525-50
0A17-2525-50
0A17-2525-50
0A0-1725-50
1A17-2525-50
1A0-1717-25
1A17-2525-50
0A17-2525-50
0A17-2525-50
0A17-2525-50
1B17-2525-50
1B17-2517-25
1B17-25U
1B17-25U
1B17-2525-50
1B17-2525-50
1B17-2525-50
1BU25-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?

Grand Advisor
Posts: 10,043

Re: Chi Square

Proc freq can use _all_.

It will generate a lot of output but

 

tables _all_*_all_ / <your other options here>;

will work.

If there are specific variables you don't want to include such as an ID variable then use a Drop= data set option to remove them.

Respected Advisor
Posts: 4,606

Re: Chi Square

Use variable lists in the tables statement and ODS OUTPUT to capture the statistics

 

proc freq data=allData;
tables (area -- range2)*indicator / norow nocol nopercent chisq;
ods output chisq=allDataChiSq;
run;
PG
Contributor
Posts: 41

Re: Chi Square

I think a better solution would be :

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;
Ask a Question
Discussion stats
  • 3 replies
  • 159 views
  • 3 likes
  • 3 in conversation