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!
Several ways:
proc freq data=have;
tables x1 * x2 * x3 / list;
run;
is probably the easiest.
Several ways:
proc freq data=have;
tables x1 * x2 * x3 / list;
run;
is probably the easiest.
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;
* 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**&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;
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
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;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: