Solved
Contributor
Posts: 64

# Get Frequency of all combinations of dummy variables

DATA have;
INPUT x1-x3;
DATALINES;
1 1 1
1 0 0
0 0 1
1 0 0
1 1 1
;;;;
run;

i want output that looks something like this:

 just x1 1 x1 and x2 0 x1 and x3 0 x1, x2 and x3 2 just x2 0 x2 and x3 0 just x3 1

thank you!

Accepted Solutions
Solution
‎11-04-2016 04:56 PM
Super User
Posts: 13,498

## Re: Get Frequency of all combinations of dummy variables

Several ways:

proc freq data=have;

tables x1 * x2 * x3 / list;

run;

is probably the easiest.

All Replies
Solution
‎11-04-2016 04:56 PM
Super User
Posts: 13,498

## Re: Get Frequency of all combinations of dummy variables

Several ways:

proc freq data=have;

tables x1 * x2 * x3 / list;

run;

is probably the easiest.

Valued Guide
Posts: 505

## Re: Get Frequency of all combinations of dummy variables

``````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;
%let pwr=3;
data mkefmt(keep=fmtname start end label);;
retain fmtname "\$bin2cmb";
length label \$15;
retain label;
do bin=0 to 2**&pwr - 1;
start=put(bin,binary&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;
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

``````
Contributor
Posts: 64

## Re: Get Frequency of all combinations of dummy variables

yes that with the sparse option worked. Thank you.
Posts: 5,519

## Re: Get Frequency of all combinations of dummy variables

If the number of Xn variables is not known:

``````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;``````
PG
☑ This topic is solved.