BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
NewUsrStat
Lapis Lazuli | Level 10

Hi guys, 

suppose to have the following table: 

 

data DB;
input ID :$20. Resp1 :$20. Resp2 :$20. Resp3 :$20.;
cards;
1 a b .
2 a c .
3 d . .
4 a b f
5 a b .
....
;

Is there a way to get the number of all combinations? 

Desired output: 

 

Combination Freq
ab 2
ac 1
d 1
abf 1
...... ...

 

 

Thank you in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

I love this kind of question.

EDITED

data DB;
input ID :$20. Resp1 :$20. Resp2 :$20. Resp3 :$20.;
cards;
1 a b .
2 a c .
3 d . .
4 a b f
5 a b .
;
data temp;
 set db;
 array r{3} $ Resp1-Resp3;
 call sortc(of r{*});
 do n=1 to dim(r);
   if missing(r{n}) then r{n}='09'x;
 end;

 array x{3};
 length combination $ 80;
 k=-1;
 do i=1 to 2**dim(x);
   rc=graycode(k,of x{*});
   call missing(combination);
   do j=1 to dim(x);
    if x{j}=1 then combination=cats(combination,r{j});
   end;
   if not findc(combination,'09'x) and not missing(combination) then output; 
 end;
 keep combination;
run;
proc freq data=temp noprint;
table combination/out=want ;
run;

View solution in original post

3 REPLIES 3
Astounding
PROC Star
The easy way to count observations in a SAS data set is PROC FREQ:

proc freq data=DB;
tables resp1 * resp2 * resp3 /
list missing;
run;
Ksharp
Super User

I love this kind of question.

EDITED

data DB;
input ID :$20. Resp1 :$20. Resp2 :$20. Resp3 :$20.;
cards;
1 a b .
2 a c .
3 d . .
4 a b f
5 a b .
;
data temp;
 set db;
 array r{3} $ Resp1-Resp3;
 call sortc(of r{*});
 do n=1 to dim(r);
   if missing(r{n}) then r{n}='09'x;
 end;

 array x{3};
 length combination $ 80;
 k=-1;
 do i=1 to 2**dim(x);
   rc=graycode(k,of x{*});
   call missing(combination);
   do j=1 to dim(x);
    if x{j}=1 then combination=cats(combination,r{j});
   end;
   if not findc(combination,'09'x) and not missing(combination) then output; 
 end;
 keep combination;
run;
proc freq data=temp noprint;
table combination/out=want ;
run;
ballardw
Super User

A question that might become of interest would be  is there a difference in the "combination"

1 a b .
2 a . b

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 726 views
  • 1 like
  • 4 in conversation