<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to make a Chi-square independence test for each item of the frequency table? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-Chi-square-independence-test-for-each-item-of-the/m-p/771695#M244937</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/343293"&gt;@commitsudoku&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could create a suitable BY variable in a view or dataset so that each value of that variable corresponds to one pair of &lt;FONT face="courier new,courier"&gt;Origin&lt;/FONT&gt; values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Simple example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create view want as
select 1 as c, origin, type from sashelp.cars where origin ne 'USA'
  union all
select 2 as c, origin, type from sashelp.cars where origin ne 'Europe'
  union all
select 3 as c, origin, type from sashelp.cars where origin ne 'Asia';
quit;

proc freq data=want;
by c;
tables origin*type / chisq;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It should be possible to create the view (maybe as a DATA step view) &lt;EM&gt;without&lt;/EM&gt;&amp;nbsp;using hardcoded values of &lt;FONT face="courier new,courier"&gt;Origin&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;c&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Addendum&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;Here is one way avoiding hardcoded values:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let dsn=sashelp.cars;
%let var1=origin;
%let var2=type;

data want(keep=_byval1 _byval2 &amp;amp;var1 &amp;amp;var2 _count) / view=want;
if _n_=1 then do;
  dcl hash h();
  h.definekey("&amp;amp;var1", "&amp;amp;var2");
  h.definedata("&amp;amp;var1", "&amp;amp;var2", '_count');
  h.definedone();
  dcl hiter hi('h');

  dcl hash k(dataset:"&amp;amp;dsn", ordered:'a');
  k.definekey("&amp;amp;var1");
  k.definedone();
  dcl hiter hi1('k');
  dcl hiter hi2('k');
end;
set &amp;amp;dsn end=last;
if h.find()=0 then _count+1;
else _count=1;
h.replace();
if last;
do while(hi1.next()=0);
  _byval1=&amp;amp;var1;
  hi2.setcur();
  do while(hi2.next()=0);
    _byval2=&amp;amp;var1;
    do while(hi.next()=0);
      if &amp;amp;var1=_byval1 | &amp;amp;var1=_byval2 then output;
    end;
  end;
end;
run;

proc freq data=want;
by _byval1 _byval2;
weight _count;
tables &amp;amp;var1*&amp;amp;var2 / chisq /* missing */;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For an example with ten BY groups use these settings:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let var1=DeathCause;
%let var2=Smoking_Status;
%let dsn=sashelp.heart(where=(~missing(&amp;amp;var1)));&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 02 Oct 2021 14:54:54 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2021-10-02T14:54:54Z</dc:date>
    <item>
      <title>How to make a Chi-square independence test for each item of the frequency table?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-Chi-square-independence-test-for-each-item-of-the/m-p/771547#M244860</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;For instance, in SASHELP.CARS, if I want to see if the Origin and the Type of the cars are independent, I can test for&amp;nbsp;independence between these two variables, using :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC FREQ data=sashelp.cars;
tables  (origin)*type  / chisq   ;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then, I get a Chi-square test value at 35,66 and a p-value&amp;lt;0.0001 ; so, I can reject the null hypotesis for the globality of the frequency table. But, what happens if I want to do this for each item of Origin; not only to see if there is a broad dependence, but to test the&amp;nbsp;independence for Asia, then Europe, then USA and so on ? Is there a better way that making the following code for each region ?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;PROC FREQ data=sashelp.cars;
where origin in ('Asia', 'Europe');
tables  (origin)*type  / chisq   ;
run;&lt;/PRE&gt;&lt;P&gt;Is there an option to have the Chi-square test for each item of the PROF FREQ, not only for the whole?&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Oct 2021 09:18:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-Chi-square-independence-test-for-each-item-of-the/m-p/771547#M244860</guid>
      <dc:creator>commitsudoku</dc:creator>
      <dc:date>2021-10-01T09:18:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to make a Chi-square independence test for each item of the frequency table?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-Chi-square-independence-test-for-each-item-of-the/m-p/771558#M244871</link>
      <description>&lt;P&gt;First sort the data by origin. Then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC FREQ data=sashelp.cars;
by origin;
tables origin*type  / chisq;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Oct 2021 10:49:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-Chi-square-independence-test-for-each-item-of-the/m-p/771558#M244871</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-10-01T10:49:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to make a Chi-square independence test for each item of the frequency table?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-Chi-square-independence-test-for-each-item-of-the/m-p/771589#M244892</link>
      <description>Well it's more or less the idea, but still I don't get a result for the Chi-squre, because the PROC FREQ doesn't put an Origin vs all the others. With the BY statement, I only get a table for that Origin and the Khi-square test cannot the applied.</description>
      <pubDate>Fri, 01 Oct 2021 13:55:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-Chi-square-independence-test-for-each-item-of-the/m-p/771589#M244892</guid>
      <dc:creator>commitsudoku</dc:creator>
      <dc:date>2021-10-01T13:55:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to make a Chi-square independence test for each item of the frequency table?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-Chi-square-independence-test-for-each-item-of-the/m-p/771592#M244893</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/343293"&gt;@commitsudoku&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Well it's more or less the idea, but still I don't get a result for the Chi-squre, because the PROC FREQ doesn't put an Origin vs all the others. With the BY statement, I only get a table for that Origin and the Khi-square test cannot the applied.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, you can't get that from one run of PROC FREQ. You would need two runs of PROC FREQ to get that. Or perhaps you can coerce PROC CATMOD to do this, but I leave that to others (if indeed it is possible).&lt;/P&gt;</description>
      <pubDate>Fri, 01 Oct 2021 14:35:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-Chi-square-independence-test-for-each-item-of-the/m-p/771592#M244893</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-10-01T14:35:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to make a Chi-square independence test for each item of the frequency table?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-Chi-square-independence-test-for-each-item-of-the/m-p/771695#M244937</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/343293"&gt;@commitsudoku&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could create a suitable BY variable in a view or dataset so that each value of that variable corresponds to one pair of &lt;FONT face="courier new,courier"&gt;Origin&lt;/FONT&gt; values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Simple example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create view want as
select 1 as c, origin, type from sashelp.cars where origin ne 'USA'
  union all
select 2 as c, origin, type from sashelp.cars where origin ne 'Europe'
  union all
select 3 as c, origin, type from sashelp.cars where origin ne 'Asia';
quit;

proc freq data=want;
by c;
tables origin*type / chisq;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It should be possible to create the view (maybe as a DATA step view) &lt;EM&gt;without&lt;/EM&gt;&amp;nbsp;using hardcoded values of &lt;FONT face="courier new,courier"&gt;Origin&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;c&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Addendum&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;Here is one way avoiding hardcoded values:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let dsn=sashelp.cars;
%let var1=origin;
%let var2=type;

data want(keep=_byval1 _byval2 &amp;amp;var1 &amp;amp;var2 _count) / view=want;
if _n_=1 then do;
  dcl hash h();
  h.definekey("&amp;amp;var1", "&amp;amp;var2");
  h.definedata("&amp;amp;var1", "&amp;amp;var2", '_count');
  h.definedone();
  dcl hiter hi('h');

  dcl hash k(dataset:"&amp;amp;dsn", ordered:'a');
  k.definekey("&amp;amp;var1");
  k.definedone();
  dcl hiter hi1('k');
  dcl hiter hi2('k');
end;
set &amp;amp;dsn end=last;
if h.find()=0 then _count+1;
else _count=1;
h.replace();
if last;
do while(hi1.next()=0);
  _byval1=&amp;amp;var1;
  hi2.setcur();
  do while(hi2.next()=0);
    _byval2=&amp;amp;var1;
    do while(hi.next()=0);
      if &amp;amp;var1=_byval1 | &amp;amp;var1=_byval2 then output;
    end;
  end;
end;
run;

proc freq data=want;
by _byval1 _byval2;
weight _count;
tables &amp;amp;var1*&amp;amp;var2 / chisq /* missing */;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For an example with ten BY groups use these settings:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let var1=DeathCause;
%let var2=Smoking_Status;
%let dsn=sashelp.heart(where=(~missing(&amp;amp;var1)));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Oct 2021 14:54:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-Chi-square-independence-test-for-each-item-of-the/m-p/771695#M244937</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-10-02T14:54:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to make a Chi-square independence test for each item of the frequency table?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-Chi-square-independence-test-for-each-item-of-the/m-p/771764#M244970</link>
      <description>&lt;P&gt;Are you talking about one-way chisq test.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc freq data=sashelp.cars;
where origin='Asia';
table type/chisq(testp=(0.1 0.1 0.60 0.1 0.05 0.05 ));
run;&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Oct 2021 14:55:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-Chi-square-independence-test-for-each-item-of-the/m-p/771764#M244970</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-10-02T14:55:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to make a Chi-square independence test for each item of the frequency table?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-Chi-square-independence-test-for-each-item-of-the/m-p/773854#M245889</link>
      <description>Perfect, thank you very much! It's a longer solution than what I expected but this is exactly what I wanted. Thank you very much!</description>
      <pubDate>Wed, 13 Oct 2021 08:11:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-Chi-square-independence-test-for-each-item-of-the/m-p/773854#M245889</guid>
      <dc:creator>commitsudoku</dc:creator>
      <dc:date>2021-10-13T08:11:06Z</dc:date>
    </item>
  </channel>
</rss>

