<?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: Get Frequency of all combinations of dummy variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Get-Frequency-of-all-combinations-of-dummy-variables/m-p/303390#M64432</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;HAVE

Up to 40 obs WORK.HAVE total obs=5

Obs   X1  X2  X3

 1    1   1   1
 2    1   0   0
 3    0   0   1
 4    1   0   0
 5    1   1   1

WANT

Obs    STR        FRQUENCY

 1     NONE           0
 2     X3             1
 3     X2             0
 4     X2 X3          0
 5     X1             2
 6     X1 X3          0
 7     X1 X2          0
 8     X1 X2 X3       2

SOLUTION

* slight reformat - easy to do;
DATA have;
retain one 1;
INPUT str $3.;
DATALINES;
111
100
001
100
111
;;;;
run;
&lt;BR /&gt;* create a format for preloading;
%let pwr=3;
data mkefmt(keep=fmtname start end label);;
 retain fmtname "$bin2cmb";
 length label $15;
 retain label;
 do bin=0 to 2**&amp;amp;pwr - 1;
    start=put(bin,binary&amp;amp;pwr..);
    end=start;
    do pos=1 to 3;
      if substr(start,pos,1)="1" then label=catx(" ",label,cats("X",put(pos,1.)));
    end;
    if label="" then label="NONE";
    output;
    label="";
 end;
run;quit;

/*
Up to 40 obs WORK.MKEFMT total obs=8

Obs    FMTNAME     LABEL       START    END

 1     $bin2cmb    NONE         000     000
 2     $bin2cmb    X3           001     001
 3     $bin2cmb    X2           010     010
 4     $bin2cmb    X2 X3        011     011
 5     $bin2cmb    X1           100     100
 6     $bin2cmb    X1 X3        101     101
 7     $bin2cmb    X1 X2        110     110
 8     $bin2cmb    X1 X2 X3     111     111
*/

proc format cntlin=mkefmt;
run;quit;

proc summary data=wrk.have  completetypes missing nonobs nway n;
   class str/preloadfmt  order=data;
   format str $bin2cmb.;
   output out=wrk.wantwps(drop=_type_);
run;quit;

proc print data=wantwps;
run;quit;

Obs    STR         _FREQ_

 1     NONE           0
 2     X3             1
 3     X2             0
 4     X2 X3          0
 5     X1             2
 6     X1 X3          0
 7     X1 X2          0
 8     X1 X2 X3       2

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 08 Oct 2016 19:39:20 GMT</pubDate>
    <dc:creator>rogerjdeangelis</dc:creator>
    <dc:date>2016-10-08T19:39:20Z</dc:date>
    <item>
      <title>Get Frequency of all combinations of dummy variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-Frequency-of-all-combinations-of-dummy-variables/m-p/303287#M64400</link>
      <description>&lt;P&gt;DATA have;&lt;BR /&gt; INPUT x1-x3;&lt;BR /&gt;DATALINES;&lt;BR /&gt;1 1 1&lt;BR /&gt;1 0 0&lt;BR /&gt;0 0 1&lt;BR /&gt;1 0 0&lt;BR /&gt;1 1 1&lt;BR /&gt;;;;; &lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i want output that looks something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="147"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="89"&gt;just x1&lt;/TD&gt;
&lt;TD width="58"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;x1 and x2&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;x1 and x3&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;x1, x2 and x3&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;just x2&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;x2 and x3&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;just x3&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2016 20:27:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-Frequency-of-all-combinations-of-dummy-variables/m-p/303287#M64400</guid>
      <dc:creator>SAShole</dc:creator>
      <dc:date>2016-10-07T20:27:11Z</dc:date>
    </item>
    <item>
      <title>Re: Get Frequency of all combinations of dummy variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-Frequency-of-all-combinations-of-dummy-variables/m-p/303291#M64403</link>
      <description>&lt;P&gt;Several ways:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc freq data=have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; tables x1 * x2 * x3 / list;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;is probably the easiest.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2016 20:32:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-Frequency-of-all-combinations-of-dummy-variables/m-p/303291#M64403</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-10-07T20:32:07Z</dc:date>
    </item>
    <item>
      <title>Re: Get Frequency of all combinations of dummy variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-Frequency-of-all-combinations-of-dummy-variables/m-p/303390#M64432</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;HAVE

Up to 40 obs WORK.HAVE total obs=5

Obs   X1  X2  X3

 1    1   1   1
 2    1   0   0
 3    0   0   1
 4    1   0   0
 5    1   1   1

WANT

Obs    STR        FRQUENCY

 1     NONE           0
 2     X3             1
 3     X2             0
 4     X2 X3          0
 5     X1             2
 6     X1 X3          0
 7     X1 X2          0
 8     X1 X2 X3       2

SOLUTION

* slight reformat - easy to do;
DATA have;
retain one 1;
INPUT str $3.;
DATALINES;
111
100
001
100
111
;;;;
run;
&lt;BR /&gt;* create a format for preloading;
%let pwr=3;
data mkefmt(keep=fmtname start end label);;
 retain fmtname "$bin2cmb";
 length label $15;
 retain label;
 do bin=0 to 2**&amp;amp;pwr - 1;
    start=put(bin,binary&amp;amp;pwr..);
    end=start;
    do pos=1 to 3;
      if substr(start,pos,1)="1" then label=catx(" ",label,cats("X",put(pos,1.)));
    end;
    if label="" then label="NONE";
    output;
    label="";
 end;
run;quit;

/*
Up to 40 obs WORK.MKEFMT total obs=8

Obs    FMTNAME     LABEL       START    END

 1     $bin2cmb    NONE         000     000
 2     $bin2cmb    X3           001     001
 3     $bin2cmb    X2           010     010
 4     $bin2cmb    X2 X3        011     011
 5     $bin2cmb    X1           100     100
 6     $bin2cmb    X1 X3        101     101
 7     $bin2cmb    X1 X2        110     110
 8     $bin2cmb    X1 X2 X3     111     111
*/

proc format cntlin=mkefmt;
run;quit;

proc summary data=wrk.have  completetypes missing nonobs nway n;
   class str/preloadfmt  order=data;
   format str $bin2cmb.;
   output out=wrk.wantwps(drop=_type_);
run;quit;

proc print data=wantwps;
run;quit;

Obs    STR         _FREQ_

 1     NONE           0
 2     X3             1
 3     X2             0
 4     X2 X3          0
 5     X1             2
 6     X1 X3          0
 7     X1 X2          0
 8     X1 X2 X3       2

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 08 Oct 2016 19:39:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-Frequency-of-all-combinations-of-dummy-variables/m-p/303390#M64432</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2016-10-08T19:39:20Z</dc:date>
    </item>
    <item>
      <title>Re: Get Frequency of all combinations of dummy variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-Frequency-of-all-combinations-of-dummy-variables/m-p/303420#M64445</link>
      <description>&lt;P&gt;If the number of &lt;EM&gt;Xn&lt;/EM&gt; variables is not known:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
set have;
length cat $32;
cat=cats(of x:);
keep cat;
run;

proc sql;
create table want as 
select cat, count(*) as n
from temp
group by cat;
select * from want;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 09 Oct 2016 04:43:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-Frequency-of-all-combinations-of-dummy-variables/m-p/303420#M64445</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-10-09T04:43:09Z</dc:date>
    </item>
    <item>
      <title>Re: Get Frequency of all combinations of dummy variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-Frequency-of-all-combinations-of-dummy-variables/m-p/309414#M66538</link>
      <description>yes that with the sparse option worked. Thank you.</description>
      <pubDate>Fri, 04 Nov 2016 20:56:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-Frequency-of-all-combinations-of-dummy-variables/m-p/309414#M66538</guid>
      <dc:creator>SAShole</dc:creator>
      <dc:date>2016-11-04T20:56:55Z</dc:date>
    </item>
  </channel>
</rss>

