<?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: Produce and count all possible combinations of the values in a column in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420816#M12912</link>
    <description>&lt;P&gt;There are more than 100 courses in the my dataset.&lt;/P&gt;</description>
    <pubDate>Wed, 13 Dec 2017 15:00:25 GMT</pubDate>
    <dc:creator>vicky1229</dc:creator>
    <dc:date>2017-12-13T15:00:25Z</dc:date>
    <item>
      <title>Produce and count all possible combinations of the values in a column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420568#M12896</link>
      <description>&lt;P&gt;Can anyone help me to solve below problem? I want to count number of students that take all combinations of courses by gender. SAS Proc, data step or sql please.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;sample data that I have, in the sample there are only 4 courses, but there are more than 100 courses in my dataset.&amp;nbsp;&lt;/P&gt;&lt;P&gt;data student;&lt;/P&gt;&lt;P&gt;input Student_ID$ Course$ Gender$;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1001 Math F&lt;BR /&gt;1001 Computer F&lt;BR /&gt;1001 Physics F&lt;BR /&gt;1002 Math M&lt;BR /&gt;1002 Physics M&lt;BR /&gt;1003 Math M&lt;BR /&gt;1003 Chemstry M&lt;BR /&gt;1003 Computer M&lt;BR /&gt;1003 Physics M&lt;BR /&gt;1004 Chemstry M&lt;BR /&gt;1004 Computer M&lt;BR /&gt;1004 Physics M&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Table that I want to get, combination is all the possible combinations of courses, Count_F is number of Female students take the combination of courses,&amp;nbsp;&lt;SPAN&gt;Count_M is number of Male students take the combination of courses,&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Combination&lt;/TD&gt;&lt;TD&gt;Count_F&lt;/TD&gt;&lt;TD&gt;Count_M&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Math, Computer&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Math, Physics&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Math, Chemstry&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Computer, Physics&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Computer, Chemstry&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Physics, Chemstry&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Math, Computer, Physics&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;…&lt;/TD&gt;&lt;TD&gt;…&lt;/TD&gt;&lt;TD&gt;…&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Math, Computer, Physics, Chemstry&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Wed, 13 Dec 2017 15:01:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420568#M12896</guid>
      <dc:creator>vicky1229</dc:creator>
      <dc:date>2017-12-13T15:01:27Z</dc:date>
    </item>
    <item>
      <title>Re: Produce and count all possible combinations of the values in a column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420573#M12897</link>
      <description>&lt;P&gt;You'll need to explain more how your output is derived.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I see a Math F, and a Computer F and two Computer M's.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I see a result of Math, Computer 1M 1F.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't know your rules.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, post your data in a data step.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2017 20:36:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420573#M12897</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2017-12-12T20:36:28Z</dc:date>
    </item>
    <item>
      <title>Re: Produce and count all possible combinations of the values in a column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420586#M12898</link>
      <description>&lt;P&gt;Thanks HB.&lt;/P&gt;&lt;P&gt;I have explained more about the question.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2017 21:09:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420586#M12898</guid>
      <dc:creator>vicky1229</dc:creator>
      <dc:date>2017-12-12T21:09:28Z</dc:date>
    </item>
    <item>
      <title>Re: Produce and count all possible combinations of the values in a column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420614#M12900</link>
      <description>&lt;P&gt;I'm sure you realize ... if you have 10 possible courses, you are getting a table with roughly 1,000 rows.&amp;nbsp; At any rate, here is one approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;array courses {4}&amp;nbsp;Chemistry Computer Math Physics;&lt;/P&gt;
&lt;P&gt;do until (last.Student_ID);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by Student_ID;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if course='Chemistry' then chemistry=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; else if course='Computer' then computer=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; else if course='Math' then math=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; else if course='Physics' then physics=1;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;length combination $ 40;&lt;/P&gt;
&lt;P&gt;if chemistry=computer=math=physics=1 then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; Combination = 'Chemistry, Computer, Math, Physics';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;do k1=1 to 3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; first_course = vname(courses{k1});&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if courses{k1}=1 then do&amp;nbsp;k2=k1+1 to 4;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if courses{k2}=1 then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; combination = strip(first_course) || ', ' || vname(courses{k2});&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;do k1=1 to 2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; first_course = vname(courses{k1});&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if courses{k1}=1 then do k2=k1+1 to 3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;first_2 = strip(first_course) || ', ' || vname(courses{k2});&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do k3=k2+1 to 4;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if courses{k3}=1 then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;combination = strip(first_2) || ', ' || vname(courses{k3});&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;keep combination gender;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc freq data=want;&lt;/P&gt;
&lt;P&gt;tables combination * gender / norow nocol nopercent;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's untested code at the moment ... I&amp;nbsp;will&amp;nbsp;try to test it and&amp;nbsp;update it if necessary.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Testing results:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It gets the right numbers, but ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's not pretty.&amp;nbsp; (You can get a nicer looking table by switching from PROC FREQ to PROC TABULATE.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It doesn't print the zeros.&amp;nbsp; This also means if a combination doesn't exist in the data, there is no row in the report for that combination.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For more than 4 courses, there may be better approaches that aren't so lengthy and clumsy.&amp;nbsp; SAS does include functions that get all combinations (grayscale?).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2017 21:57:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420614#M12900</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-12-12T21:57:01Z</dc:date>
    </item>
    <item>
      <title>Re: Produce and count all possible combinations of the values in a column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420623#M12901</link>
      <description>&lt;P&gt;How many distinct&amp;nbsp;courses are included in your data?&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2017 22:19:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420623#M12901</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-12-12T22:19:29Z</dc:date>
    </item>
    <item>
      <title>Re: Produce and count all possible combinations of the values in a column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420634#M12902</link>
      <description>&lt;P&gt;I may have understood your requirements differently. Here's a solution that solves what I think is the problem...but I could be wrong!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tom&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=student;
	by Student_ID Course;
run;

data Inter01;
	length Course_List $200;
	retain Course_List Count_F Count_M;
	drop Student_ID Course Gender;
	set student;
	by Student_ID;

	if first.Student_ID
		then do;
		Count_F = 0;
		Count_M = 0;

		if Gender = 'F' then
			Count_F = 1;
		else Count_M = 1;
		Course_List = Course;
	end;
	else Course_List = strip(Course_List)||", "||strip(Course);

	if last.Student_ID then
		output;
run;

proc means data=Inter01 nway noprint;
	var Count_F Count_M;
	class Course_List;
	output out=Want(drop=_TYPE_ _FREQ_) sum()=;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Dec 2017 23:15:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420634#M12902</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2017-12-12T23:15:25Z</dc:date>
    </item>
    <item>
      <title>Re: Produce and count all possible combinations of the values in a column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420653#M12903</link>
      <description>&lt;P&gt;You want a frequency, by sex, of all within-student 2-way, 3-way, ... combinations of classes taken.&amp;nbsp; This is a good case for using the CALL LEXCOMBI call routine, which gets all non-missing combinations user-specified size, from a larger list of values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data student;
  input Student_ID$ Course$ Gender$;
datalines;
1001 Math F
1001 Computer F
1001 Physics F
1002 Math M
1002 Physics M
1003 Math M
1003 Chemstry M
1003 Computer M
1003 Physics M
1004 Chemstry M
1004 Computer M
1004 Physics M
;


proc sort data=student; by student_id course;run;

data need (keep=student_id gender crse: combo_size crse_combo);
  array _crse{10} $8 _temporary_;
  format crse_combo $char100.;
  retain NC 0;

  set student;
  by student_id;
  if first.student_id then do;
    call missing(of _crse{*});
    NC=0;
  end;
  NC+1;
  _crse{NC}=course;

  if last.student_id and NC&amp;gt;1;

  array crse{10} $9;

  array _i2[2];
  array _i3[3];
  array _i4[4];
  array _i5[5];
  array _i6[6];
  array _i7[7];
  array _i8[8];
  array _i9[9];
  array _i10[10];

  array all_i{2:10,10}
    _i2:  dum dum dum dum dum dum dum dum
    _i3:  dum dum dum dum dum dum dum
    _i4:  dum dum dum dum dum dum
    _i5:  dum dum dum dum dum
    _i6:  dum dum dum dum
    _i7:  dum dum dum 
    _i8:  dum dum 
    _i9:  dum 
    _i10:  ;

 if nc&amp;gt;1 then do combo_size=2 to NC;
   all_i{combo_size,1}=0;
   ncomb=comb(nc, combo_size);
   do j=1 to ncomb;
      select (combo_size);
        when (2)  rc=lexcombi(nc, combo_size, of _i2[*]);
        when (3)  rc=lexcombi(nc, combo_size, of _i3[*]);
        when (4)  rc=lexcombi(nc, combo_size, of _i4[*]);
        when (5)  rc=lexcombi(nc, combo_size, of _i5[*]);
        when (6)  rc=lexcombi(nc, combo_size, of _i6[*]);
        when (7)  rc=lexcombi(nc, combo_size, of _i7[*]);
        when (8)  rc=lexcombi(nc, combo_size, of _i8[*]);
        when (9)  rc=lexcombi(nc, combo_size, of _i9[*]);
        when (10) rc=lexcombi(nc, combo_size, of _i10[*]);
      end;
      do h=1 to combo_size;
        crse[h]=_crse[all_i{combo_size,h}];
      end;
      crse_combo=put(combo_size,2.) || ' ' || cat(of crse{*});
      output;
   end;
 end;  
run;

proc tabulate data=need noseps;
  class gender crse_combo;
  tables crse_combo=' ',gender=' '*N=' '*f=4. 
    / rts=100 box='Size / Course List' misstext='0';
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The proc sort is done, so that all combinations are in alphabetical order&amp;nbsp; (i.e. there are math/physics, but no physics/math).&lt;/LI&gt;
&lt;LI&gt;I declare index arrays for each expected combination size (I used a maximum of size 10).&lt;/LI&gt;
&lt;LI&gt;Each array (_i2, _i3, etc.) is used with call lexcombi for its own size.&amp;nbsp; You can't use "of _I{*}" with call lexcombi for a size other than its own.&amp;nbsp;&amp;nbsp; That's why you see the "select (combo_size)" group of statements.&lt;/LI&gt;
&lt;LI&gt;By declaring a 2-dimensional array ALL_i&amp;nbsp; (one row per expected combo_size), constructed of the individual _i arrays and repeated use of the DUM variable, I can use compact statements such as&amp;nbsp;&amp;nbsp; &lt;EM&gt;&lt;STRONG&gt;"crse[h]=_crse[all_i{combo_size,h}];"&lt;/STRONG&gt;&lt;/EM&gt; instead of another set of select statements.&amp;nbsp; Array all_i has an upper triangle of repeated use of the dum vars.&lt;/LI&gt;
&lt;LI&gt;&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Wed, 13 Dec 2017 01:34:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420653#M12903</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-12-13T01:34:15Z</dc:date>
    </item>
    <item>
      <title>Re: Produce and count all possible combinations of the values in a column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420783#M12911</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data student;
  input Student_ID$ Course$ Gender$;
  retain value 1;
datalines;
1001 Math F
1001 Computer F
1001 Physics F
1002 Math M
1002 Physics M
1003 Math M
1003 Chemstry M
1003 Computer M
1003 Physics M
1004 Chemstry M
1004 Computer M
1004 Physics M
;

proc transpose data=student out=temp ;
by Student_ID gender;
var value;
id course;
run;

proc sql noprint;
select distinct Course into : list separated by ' '
 from student;
select count(distinct course) into : n
 from student;
quit;

data temp1;
 set temp;
 array x{*} &amp;amp;list ;
 array y{&amp;amp;n};
 length combination $ 2000;
 k=-1;
 do i=1 to 2**&amp;amp;n ;
  rc=graycode(k,of y{*});
  yes=1;
  do j=1 to &amp;amp;n;
   if y{j}=1 and x{j}=. then yes=0;
  end;
  call missing(combination);
  if yes then do;
    do j=1 to &amp;amp;n;
      if y{j}=1 then combination=catx(',',combination,vname(x{j})) ;
    end;
	if not missing(combination) then output;
  end;
 end;
 keep gender combination;
run;
proc freq data=temp1 noprint;
table  gender*combination/out=temp2 list;
run;
proc sort data=temp2;by combination;run;
proc transpose data=temp2 out=want prefix=count_;
by combination;
var count;
id gender;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;P.S. modified. It seems it is not so easy.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2017 14:06:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420783#M12911</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-12-13T14:06:56Z</dc:date>
    </item>
    <item>
      <title>Re: Produce and count all possible combinations of the values in a column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420816#M12912</link>
      <description>&lt;P&gt;There are more than 100 courses in the my dataset.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2017 15:00:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420816#M12912</guid>
      <dc:creator>vicky1229</dc:creator>
      <dc:date>2017-12-13T15:00:25Z</dc:date>
    </item>
    <item>
      <title>Re: Produce and count all possible combinations of the values in a column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420979#M12927</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;Here is the smart solution you provided, with slight macro usage twist to enable data driven code generation for the array processing.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data student;
    input Student_ID$ Course$ Gender$;
    datalines;
1001 Math F
1001 Computer F
1001 Physics F
1002 Math M
1002 Physics M
1003 Math M
1003 Chemstry M
1003 Computer M
1003 Physics M
1004 Chemstry M
1004 Computer M
1004 Physics M
;
run;
&lt;BR /&gt;/*Extract the count of unique courses and store it in a macro variable*/
proc sql noprint;
    select count(distinct (course))
    into :g_numUnqCourse
    from work.student;
quit;

proc sort data=student;
    by student_id course;
run;

/* Declare a macro to auto generate SAS statements */
%macro InitDynArray(p_max);
    %local l_n;
    %do l_n=2 %to &amp;amp;p_max;
        %str(ARRAY _i&amp;amp;l_n[&amp;amp;l_n];)
    %end;
    ARRAY all_i{2:&amp;amp;p_max,&amp;amp;p_max}
    %do l_n=2 %to &amp;amp;p_max;
    _i&amp;amp;l_n:
        %do j=1 %to %eval(&amp;amp;p_max - &amp;amp;l_n);
            %str(dum )
        %end;
    %end;
    %str(;)
%mend;

/* Declare a macro to auto generate SAS statements */
%macro dynArrayCombo(p_max);
    %local l_n;
    %do l_n=2 %to &amp;amp;p_max;
        WHEN (&amp;amp;l_n)  rc=LEXCOMBI(nc, combo_size, of _i&amp;amp;l_n[*]);
    %end;
%mend;
&lt;BR /&gt;OPTIONS MPRINT; /*Display generated statements by the macro calls*/&lt;BR /&gt;
data need (keep=student_id gender crse: combo_size crse_combo);
    array _crse{&amp;amp;g_numUnqCourse} $8 _temporary_;
    format crse_combo $char100.;
    retain NC 0;
    set student;
    by student_id;

    if first.student_id then
    do;
        call missing(of _crse{*});
        NC=0;
    end;

    nc+1;
    _crse{nc}=course;

    if ((last.student_id) and (nc &amp;gt; 1));

    ARRAY crse{&amp;amp;g_numUnqCourse} $9;
    %InitDynArray(&amp;amp;g_numUnqCourse); /* Call a macro */

    if (nc &amp;gt; 1) then
        do combo_size=2 to nc;
            all_i{combo_size,1}=0;
            ncomb=comb(nc, combo_size);

            do j=1 to ncomb;
                select (combo_size);
                    %dynArrayCombo(&amp;amp;g_numUnqCourse) /* Call a macro */
                    otherwise;
                end; /* End of Select */

                do h=1 to combo_size;
                    crse[h]=_crse[all_i{combo_size,h}];
                end;

                crse_combo=put(combo_size,2.) || ': ' || catx(',',of crse{*});
                output;
            end; /* End of j=1 to ncomb */
        end; /* End of combo_size=2 to NC */
run;

proc tabulate data=need noseps;
    class gender crse_combo;
    tables crse_combo=' ',gender=' '*N=' '*f=4. 
        / rts=100 box='Size / Course List' misstext='0';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Ahmed&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2017 20:27:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420979#M12927</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2017-12-13T20:27:36Z</dc:date>
    </item>
    <item>
      <title>Re: Produce and count all possible combinations of the values in a column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420981#M12928</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13868"&gt;@AhmedAl_Attar&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I appreciate your macro-izing of the code I provided.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But to be honest, if the dataset is large, I would run a PROC SQL against the entire data set just to generate the maximum number of possible courses.&amp;nbsp; I would just parameterize the macro it with a number that would surely exceed all possible course counts.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;After all once you've macro-ized, you aren't subject to typing errors when coding for each combination size, so the penalty for a too-large number becomes negligible&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, if I had been true to efficiency-uber-alles, I would have avoided the proc sort, since the data were already sorted by student_id, but not course-within-student_id.&amp;nbsp; Instead, after reading in the array of courses for a given student, I would have used CALL SORTC of the array, followed by left-shifting of the&amp;nbsp;results to avoid blank course values at the lower bound of the array.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2017 20:43:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/420981#M12928</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-12-13T20:43:30Z</dc:date>
    </item>
    <item>
      <title>Re: Produce and count all possible combinations of the values in a column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/421041#M12929</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Here is yet another solution, While I have used some code from previous contributors, I've used Custom written function via Proc FCMP to produce and return all possible course combinations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The&amp;nbsp; getCombos(values [*] $, sep $) $; user function, accepts an array and a separator character, and returns a separated String of course combinations. The logic implemented inside that function was inspired from an old &lt;A href="http://www2.sas.com/proceedings/sugi29/045-29.pdf" target="_self"&gt;SUGI 29 paper&lt;/A&gt; .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc fcmp
    outlib=sasuser.functions.customFunctions;

    function getCombos(values [*] $, sep $) $;
        length combos $100 l_sep $1;
        length i vCount binaryDigit 4;
        length max_Type_  start i_tmp 8;
        length final_combo $1000;

        l_sep=ifc (sep='','I',sep) ;

        *put values[*];
        vCount=0;
        do i=1 to dim(values);
            vCount+ifc(values[i]='',0,1);
        end;

        max_Type_ = 2**vCount - 1;
        *put max_Type_;

        /*  Loop through till max_Type_ is reached*/
        do start=0 to max_Type_;
            i_tmp = start;

            /* Loop through all passed-in values */
            do j=vCount to 1 by -1;
                binaryDigit= int(i_tmp/(2**(j-1)) );
                if (binaryDigit = 1) then
                do;
                    combos = catx(',',combos,values[(vCount-j+1)]);

                    * Decrement i_tmp if binaryDigit is in types binary representation;
                    i_tmp = i_tmp - 2**(j-1);
                end;
            end; /*End of  j=vCount to 1 by -1*/

            /* Only maintain combinations of more than 1 value */
            if (index(combos,',') GT 0) then
            do;
                *put combos;
                final_combo = catx(l_sep,final_combo,combos);
                *put final_combo;
            end;
            combos='';
        end; /* End of start=0 to max_Type_*/

        return (final_combo);
    endsub;
run;

options cmplib=(sasuser.functions);

data student;
    input Student_ID$ Course$ Gender$;
    datalines;
1001 Math F
1001 Computer F
1001 Physics F
1002 Math M
1002 Physics M
1003 Math M
1003 Chemstry M
1003 Computer M
1003 Physics M
1004 Chemstry M
1004 Computer M
1004 Physics M
;
run;
proc sort data=student;
    by student_id course;
run;

data want(keep=student_id gender crse_combo);
    array _crse{10} $9 _temporary_;
    retain nc 0;
    set student;
    by student_id;
    length courses $1000;

    if first.student_id then
    do;
        call missing(of _crse{*});
        nc=0;
    end;

    nc+1;
    _crse{nc}=course;

    if ((last.student_id) and (nc &amp;gt; 1));
    courses = getCombos(_crse, "~");
    *put courses=;
    comboCount=countc(courses,'~')+1;
    do c=1 to comboCount;
        crse_combo=scan(courses,c,'~');
        combo_size=countc(crse_combo,',')+1;
        crse_combo= put(combo_size,2.) || ': ' || crse_combo;
        output;
    end;
run;

proc tabulate data=want noseps;
    class gender crse_combo;
    tables crse_combo=' ',gender=' '*N=' '*f=4. 
        / rts=100 box='Size / Course List' misstext='0';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;Ahmed&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2017 23:55:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/421041#M12929</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2017-12-13T23:55:51Z</dc:date>
    </item>
    <item>
      <title>Re: Produce and count all possible combinations of the values in a column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/421187#M12933</link>
      <description>&lt;P&gt;Thanks a lot Ahmed, your solution is great. I have one question for you, your code is working well when I test it in sample data set which has about 10 courses, but it gives me an error when I test in another data set which has about 100 courses in it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please give me a hint to make it work for large data set?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Dec 2017 14:38:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/421187#M12933</guid>
      <dc:creator>vicky1229</dc:creator>
      <dc:date>2017-12-14T14:38:12Z</dc:date>
    </item>
    <item>
      <title>Re: Produce and count all possible combinations of the values in a column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/421200#M12934</link>
      <description>Hi Vicky,&lt;BR /&gt;Without knowing the exact nor the nature of the error you are getting, I'm guessing it would have to do with &lt;BR /&gt;- Character length  ----&amp;gt; Change this line: format crse_combo $char100.;&lt;BR /&gt;- Memory allocated to your SAS session  ---&amp;gt; use -MEMSIZE 4GB on your SAS invocation command line. Increase the allocated GB as needed.&lt;BR /&gt;&lt;BR /&gt;With 100 courses --&amp;gt; ((2**100) -1) permutations!!&lt;BR /&gt;Which is really a very big number: 1.26765060023E30 !!&lt;BR /&gt;&lt;BR /&gt;Hope this helps,&lt;BR /&gt;Ahmed&lt;BR /&gt;</description>
      <pubDate>Thu, 14 Dec 2017 15:24:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Produce-and-count-all-possible-combinations-of-the-values-in-a/m-p/421200#M12934</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2017-12-14T15:24:07Z</dc:date>
    </item>
  </channel>
</rss>

