<?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: All possible shopping lists in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324855#M72152</link>
    <description>&lt;P&gt;Hi Astounding,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;based on Ksharp's first solution in the link that Reeza posted, I managed to write a code that gets a solution similar to yours, although it is in several steps:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input item price;
datalines;
1 10
2 20
3 30
4 40
5 50
; 
run;
/*first get the total prices of all combinations */
proc transpose data=have out=temp;
 var price;
run;

proc summary data=temp;
 class col:;
 output out=total_price;
run;

data total_price;
set total_price;
total_price = sum(of col1-col5);
run;

/*now get the combinations of the item names*/
proc transpose data=have out=temp2;
 var item;
run;
proc summary data=temp2;
 class col:;
 output out=item_names;
run;

data item_names;
 set item_names;
 length item_names $ 20;
 item_names=catx(' ',of col:);
 keep item_names _type_;
run;

/*and now merging item_names with total_price by _TYPE_*/
DATA shopping_lists;
 MERGE total_price item_names;
 BY _TYPE_;
 RUN;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Your code is in one step, and it has do-end loops for each item. Is it possible to have a macro which will do the process automatically for any number of items?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 15 Jan 2017 01:50:44 GMT</pubDate>
    <dc:creator>ilikesas</dc:creator>
    <dc:date>2017-01-15T01:50:44Z</dc:date>
    <item>
      <title>All possible shopping lists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324789#M72127</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppose that there are 5 items in a store:&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 96pt;" border="0" width="128" cellspacing="0" cellpadding="0"&gt;&lt;COLGROUP&gt;&lt;COL style="width: 48pt;" span="2" width="64" /&gt; &lt;/COLGROUP&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" style="height: 15.0pt; width: 48pt;"&gt;item&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;price&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;10&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;2&lt;/TD&gt;
&lt;TD align="right"&gt;20&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;3&lt;/TD&gt;
&lt;TD align="right"&gt;30&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;4&lt;/TD&gt;
&lt;TD align="right"&gt;40&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;5&lt;/TD&gt;
&lt;TD align="right"&gt;50&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming that you can buy each item only once, how would it be possible to create all possible shopping lists (consisting of1 item, 2 items ... 5 items) and to calculate the total price of each shopping list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks! &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 14 Jan 2017 03:56:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324789#M72127</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2017-01-14T03:56:58Z</dc:date>
    </item>
    <item>
      <title>Re: All possible shopping lists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324796#M72129</link>
      <description>&lt;P&gt;I'm not sure if I understand what you're looking for, but it sounds like you want all combinations of 1 out of 5, 2 out of 5, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is a much easier solution available if you have access to proc plan. The following does the same thing through a series of macros and data steps. The final solution will be in a file called 'want'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An explanation of the code can be found at:&amp;nbsp;&lt;A href="https://www.google.ca/url?sa=t&amp;amp;rct=j&amp;amp;q=&amp;amp;esrc=s&amp;amp;source=web&amp;amp;cd=2&amp;amp;cad=rja&amp;amp;uact=8&amp;amp;ved=0ahUKEwiO6-H_8sDRAhVKOSYKHW3PAQ0QFggiMAE&amp;amp;url=http%3A%2F%2Fwww2.sas.com%2Fproceedings%2Fsugi23%2FPosters%2Fp177.pdf&amp;amp;usg=AFQjCNEX8L4Eq_lNH975pkfgUPOBb33Rig&amp;amp;sig2=2znAQzU5VHOjAGFzjCYKKg" target="_blank"&gt;https://www.google.ca/url?sa=t&amp;amp;rct=j&amp;amp;q=&amp;amp;esrc=s&amp;amp;source=web&amp;amp;cd=2&amp;amp;cad=rja&amp;amp;uact=8&amp;amp;ved=0ahUKEwiO6-H_8sDRAhVKOSYKHW3PAQ0QFggiMAE&amp;amp;url=http%3A%2F%2Fwww2.sas.com%2Fproceedings%2Fsugi23%2FPosters%2Fp177.pdf&amp;amp;usg=AFQjCNEX8L4Eq_lNH975pkfgUPOBb33Rig&amp;amp;sig2=2znAQzU5VHOjAGFzjCYKKg&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HTH,&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data fmtdata;&lt;BR /&gt; infile cards dlm='09'x;&lt;BR /&gt; retain fmtname 'pcodes' type 'I';&lt;BR /&gt; input start label;&lt;BR /&gt; cards;&lt;BR /&gt;1 10&lt;BR /&gt;2 20&lt;BR /&gt;3 30&lt;BR /&gt;4 40&lt;BR /&gt;5 50&lt;BR /&gt;;&lt;BR /&gt;proc format cntlin = fmtdata;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;%macro comb(dsout2=&amp;amp;dsout,dsin2=__d1,k=,n=,len=) ;&lt;BR /&gt;%local dsin2 dsout2 numvar i start stop n k len ;&lt;BR /&gt;%let start = 1 ;&lt;BR /&gt;%let stop = %eval( &amp;amp;n - &amp;amp;k) ;&lt;BR /&gt;data &amp;amp;dsout2 ;&lt;BR /&gt;set &amp;amp;dsin2 ;&lt;BR /&gt;array _vals {&amp;amp;n} $ ;&lt;BR /&gt;array combo {&amp;amp;k} $ &amp;amp;len ;&lt;BR /&gt;keep j combo1 - combo&amp;amp;k ;&lt;BR /&gt;retain j 1 ;&lt;BR /&gt;%do i = 1 %to &amp;amp;k;&lt;BR /&gt;%let stop = %eval(&amp;amp;stop + 1) ;&lt;BR /&gt;do var&amp;amp;i = &amp;amp;start to &amp;amp;stop ;&lt;BR /&gt;%let start = %str(%(var&amp;amp;i + 1 %)) ;&lt;BR /&gt;%end ;&lt;BR /&gt;%do i = 1 %to &amp;amp;k ;&lt;BR /&gt;combo{&amp;amp;i} = _vals{var&amp;amp;i} ;&lt;BR /&gt;%end ;&lt;BR /&gt;output ;&lt;BR /&gt;j + 1 ;&lt;BR /&gt;%do i = 1 %to &amp;amp;k ;&lt;BR /&gt;end ;&lt;BR /&gt;%end ;&lt;BR /&gt;run;&lt;BR /&gt;%mend comb;&lt;/P&gt;
&lt;P&gt;%macro setup(dsout=_NULL_,dsin=&amp;amp;syslast,var=,ks=) ;&lt;BR /&gt;%local dsout dsin var totvar ks ;&lt;BR /&gt;proc freq data=&amp;amp;dsin noprint ;&lt;BR /&gt;tables &amp;amp;var / out=__count ;&lt;BR /&gt;data _NULL_ ;&lt;BR /&gt;set __count end=last ;&lt;BR /&gt;retain len oldlen 0 ;&lt;BR /&gt;nk + 1 ;&lt;BR /&gt;len = length(&amp;amp;var) ;&lt;BR /&gt;if ( len &amp;gt; oldlen ) then oldlen = len ;&lt;BR /&gt;if count &amp;gt; 1 then do;&lt;BR /&gt;put "WARNING: The variable %upcase(&amp;amp;var)" /&lt;BR /&gt;"WARNING: has “ count “ occurrences of the value "&lt;BR /&gt;var;&lt;BR /&gt;end ;&lt;BR /&gt;if last then do ;&lt;BR /&gt;if &amp;amp;ks &amp;gt; nk then do;&lt;BR /&gt;put "ERROR: K greater than the Number of values (N)" ;&lt;BR /&gt;put "ERROR: Value of K: &amp;amp;ks Value of N: " nk ;&lt;BR /&gt;put "ERROR: EXITING Macro!!!!!!!!!!!!!!!!!!" ;&lt;BR /&gt;end ;&lt;BR /&gt;call symput('totvar',trim(left(put(nk,5.))) ) ;&lt;BR /&gt;call symput('length',put(oldlen,3.) );&lt;BR /&gt;end ;&lt;BR /&gt;run ;&lt;BR /&gt;%if &amp;amp;ks &amp;gt; &amp;amp;totvar %then %goto exit ;&lt;BR /&gt;data __d1 ;&lt;BR /&gt;set __count end=last ;&lt;BR /&gt;array _vals {&amp;amp;totvar} $ &amp;amp;length;&lt;BR /&gt;retain _vals1-_vals&amp;amp;totvar ;&lt;BR /&gt;keep _vals1-_vals&amp;amp;totvar ;&lt;BR /&gt;_vals{_N_} = &amp;amp;var ;&lt;BR /&gt;if last then output ;&lt;BR /&gt;%comb(k=&amp;amp;ks,n=&amp;amp;totvar,len=&amp;amp;length)&lt;BR /&gt;run ;&lt;BR /&gt;%exit: %mend setup ;&lt;/P&gt;
&lt;P&gt;%setup(var=start,ks=1,dsin=fmtdata,dsout=combin1)&lt;BR /&gt;%setup(var=start,ks=2,dsin=fmtdata,dsout=combin2)&lt;BR /&gt;%setup(var=start,ks=3,dsin=fmtdata,dsout=combin3)&lt;BR /&gt;%setup(var=start,ks=4,dsin=fmtdata,dsout=combin4)&lt;BR /&gt;%setup(var=start,ks=5,dsin=fmtdata,dsout=combin5)&lt;/P&gt;
&lt;P&gt;data want (drop=i j);&lt;BR /&gt; set combin1 (rename=(combo1=item1))&lt;BR /&gt; combin2 (rename=(combo1-combo2=item1-item2))&lt;BR /&gt; combin3 (rename=(combo1-combo3=item1-item3))&lt;BR /&gt; combin4 (rename=(combo1-combo4=item1-item4))&lt;BR /&gt; combin5 (rename=(combo1-combo5=item1-item5));&lt;BR /&gt; array combos(5) item1-item5;&lt;BR /&gt; array prices(5);&lt;BR /&gt; do i=1 to 5;&lt;BR /&gt; prices(i)=input(combos(i),pcodes.);&lt;BR /&gt; end;&lt;BR /&gt; total=sum(of prices(*));&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 14 Jan 2017 05:35:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324796#M72129</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-01-14T05:35:51Z</dc:date>
    </item>
    <item>
      <title>Re: All possible shopping lists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324805#M72133</link>
      <description>&lt;P&gt;See the solutions here:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/Selecting-distinct-Combinations/m-p/318086" target="_blank"&gt;https://communities.sas.com/t5/Base-SAS-Programming/Selecting-distinct-Combinations/m-p/318086&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;'s solution&amp;nbsp;using GRAYCODE()&amp;nbsp;is probably the easiest. You can also load the temporary array from a data set if desired.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 14 Jan 2017 07:08:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324805#M72133</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-01-14T07:08:57Z</dc:date>
    </item>
    <item>
      <title>Re: All possible shopping lists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324810#M72135</link>
      <description>&lt;P&gt;Tested solution given:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data prices;
  input item price;
  n+1; drop n;
  call symput('items',left(n));
datalines;
1 10
2 20
3 30
4 40
5 50
; run;
%put ITEMS= &amp;amp;items;

proc transpose data=prices 
         out=price_array(drop=_name_)
         prefix=I;       
  var price;
  id item;
run;

proc sql;
  select name into: vnames separated by ' '
  from sashelp.vcolumn where memname=upcase('price_array');
quit;
%put NAMES= &amp;amp;vnames;

data _null_;
   varnames = '"'||tranwrd("&amp;amp;vnames",' ','" "')||'"';
   call symput('varnames',varnames);
run;

data shop_list;
     length items_list $100;
     retain items_list;
 set price_array;
    array ix {&amp;amp;items} $ (&amp;amp;varnames);
    array pr {&amp;amp;items} &amp;amp;vnames;
    
    do i=1 to &amp;amp;items;
       items_list = ' '; 
       price = pr(i); 
       items_list = ix(i);
       output;
       if i &amp;lt; &amp;amp;items then do;          
          do j=i+1 to &amp;amp;items;
             items_list = catx(' ',items_list,ix(j));
             price = price + pr(j);
             output;
          end;
       end;
   end;
   keep items_list price;
run;




    

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 14 Jan 2017 08:15:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324810#M72135</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-01-14T08:15:07Z</dc:date>
    </item>
    <item>
      <title>Re: All possible shopping lists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324830#M72137</link>
      <description>&lt;P&gt;Since we're assuming there are 5 items, there is no need for macro language. &amp;nbsp;The program can be simplified to:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;array items {5} item1-item5;&lt;/P&gt;
&lt;P&gt;array prices {5} price1-price5;&lt;/P&gt;
&lt;P&gt;array v {5} v1-v5;&lt;/P&gt;
&lt;P&gt;if _n_=1 then do i=1 to 5;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;items{i}=item;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;prices{i}=price;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That much in effect transposes the existing data. &amp;nbsp;Then continue the same DATA step to find all combinations:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;do v1=0, price1;&lt;/P&gt;
&lt;P&gt;do v2=0, price2;&lt;/P&gt;
&lt;P&gt;do v3=0, price3;&lt;/P&gt;
&lt;P&gt;do v4=0, price4;&lt;/P&gt;
&lt;P&gt;do v5=0, price5;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;item_list='NNNNN';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;do i=1 to 5;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if v{i} then substr(item_list, i, 1)='Y';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;total_cost = sum(of v1-v5);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;output;&lt;/P&gt;
&lt;P&gt;end; end; end; end; end;&lt;/P&gt;
&lt;P&gt;stop;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now this does give you an extra observation, where no items were purchased. &amp;nbsp;You get the total cost, plus a 5-character ITEM_LIST consisting of a set of "N" and "Y" values. &amp;nbsp;If the first character is "Y", the first item was purchased. &amp;nbsp;If the second character is "Y", the second item was purchased, etc.&lt;/P&gt;</description>
      <pubDate>Sat, 14 Jan 2017 14:56:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324830#M72137</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-14T14:56:59Z</dc:date>
    </item>
    <item>
      <title>Re: All possible shopping lists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324847#M72148</link>
      <description>&lt;P&gt;Hi Shmuel,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I ran the code, but it seems to be missing some combinations. For example, it omits combinations&amp;nbsp;such as (i1,i5) or (i1,i2,i4).&lt;/P&gt;
&lt;P&gt;There should be 31 combinations (if you don't count the empty combination), but here there are 15.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Sun, 15 Jan 2017 01:38:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324847#M72148</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2017-01-15T01:38:34Z</dc:date>
    </item>
    <item>
      <title>Re: All possible shopping lists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324848#M72149</link>
      <description>&lt;P&gt;hi art297,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I ran the code but it gave me an empty "want" data table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, data fmtdata has 4 columns and 2 observations, and not the item and column data - maybe that is the reason why want is empty?&lt;/P&gt;</description>
      <pubDate>Sun, 15 Jan 2017 01:04:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324848#M72149</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2017-01-15T01:04:09Z</dc:date>
    </item>
    <item>
      <title>Re: All possible shopping lists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324855#M72152</link>
      <description>&lt;P&gt;Hi Astounding,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;based on Ksharp's first solution in the link that Reeza posted, I managed to write a code that gets a solution similar to yours, although it is in several steps:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input item price;
datalines;
1 10
2 20
3 30
4 40
5 50
; 
run;
/*first get the total prices of all combinations */
proc transpose data=have out=temp;
 var price;
run;

proc summary data=temp;
 class col:;
 output out=total_price;
run;

data total_price;
set total_price;
total_price = sum(of col1-col5);
run;

/*now get the combinations of the item names*/
proc transpose data=have out=temp2;
 var item;
run;
proc summary data=temp2;
 class col:;
 output out=item_names;
run;

data item_names;
 set item_names;
 length item_names $ 20;
 item_names=catx(' ',of col:);
 keep item_names _type_;
run;

/*and now merging item_names with total_price by _TYPE_*/
DATA shopping_lists;
 MERGE total_price item_names;
 BY _TYPE_;
 RUN;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Your code is in one step, and it has do-end loops for each item. Is it possible to have a macro which will do the process automatically for any number of items?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Jan 2017 01:50:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324855#M72152</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2017-01-15T01:50:44Z</dc:date>
    </item>
    <item>
      <title>Re: All possible shopping lists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324857#M72153</link>
      <description>&lt;P&gt;It's possible, but there is a lot to consider concerning what parameters should exist. &amp;nbsp;Normally, any of these would represent possibilities.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Name of the input data set&lt;/P&gt;
&lt;P&gt;Name of the output data set&lt;/P&gt;
&lt;P&gt;Maximum number of items to select ... will it always match the maximum number available in the data set?&lt;/P&gt;
&lt;P&gt;Names of the PRICE variables (are they always price1-priceN?)&lt;/P&gt;
&lt;P&gt;Names of the ITEM variables (are they always item1-itemN?)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Other considerations that would affect how the program (or how the macro) is written:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you want a variable showing how many items were selected?&lt;/P&gt;
&lt;P&gt;Can any PRICE values be zero?&lt;/P&gt;</description>
      <pubDate>Sun, 15 Jan 2017 02:52:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324857#M72153</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-15T02:52:09Z</dc:date>
    </item>
    <item>
      <title>Re: All possible shopping lists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324861#M72155</link>
      <description>&lt;P&gt;I guess that macros in this case would be complex. I did manage to do it in one step by using Reeza's solution to the link that he posted by adding a second price array to his code. Here it is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 *can load temporary array from dataset if required;

 array x1[5]  _temporary_ (10 20 30 40 50  );
 array x2[5] $2 _temporary_ ('1' '2' '3' '4' '5');
 *array for output;
 array y1(5) ;
 array y2(5);

 n=dim(x1);

 *loop overall all possible output ranges;
 do k=1 to n;

 *Determine number of combinations required;
 ncomb=comb(n, k);

  *Create all combinations;
  do j=1 to ncomb;

     *create the combination;
     call lexcomb(j, k, of x1[*]);
     call lexcomb(j, k, of x2[*]);  
     *Set all values to missing;
     call missing(of y1(*));
     call missing(of y2(*)); 
     *Copy over the values to the array for output;
     do i=1 to k;
        y1(i)=x1(i);
		y2(i)=x2(i);
     end;
 
     *output record;
     output;
 
  end;
end;
run;

data want;
set want;
total_price = sum (of y11 - y15);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Maybe this code could be turned into a macro if the item and price variables can be put inside the 2 temporary arrays. I am not sure if the observations of a row can be put into an array, unless it is necessary to transpose the "have" and like this put the now columns into the temporary arrays&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Jan 2017 03:40:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324861#M72155</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2017-01-15T03:40:48Z</dc:date>
    </item>
    <item>
      <title>Re: All possible shopping lists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324863#M72157</link>
      <description>&lt;P&gt;thanks Reeza for the link, using your solution I could also obtain the total price of each shopping list!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also useful to learn about this graycode function, althought just from its name its not so obvious that it has got something to do with combinations!&lt;/P&gt;</description>
      <pubDate>Sun, 15 Jan 2017 03:59:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324863#M72157</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2017-01-15T03:59:37Z</dc:date>
    </item>
    <item>
      <title>Re: All possible shopping lists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324864#M72158</link>
      <description>&lt;P&gt;You can load a temporary array from a dataset.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See the example here:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/f052b5223fecca066b1f" target="_blank"&gt;https://gist.github.com/statgeek/f052b5223fecca066b1f&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Jan 2017 04:04:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324864#M72158</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-01-15T04:04:04Z</dc:date>
    </item>
    <item>
      <title>Re: All possible shopping lists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324866#M72160</link>
      <description>&lt;P&gt;I see, that is how it is possible to get the observations of a variable into an array without transposing (related to my reply to Astounding)&lt;/P&gt;</description>
      <pubDate>Sun, 15 Jan 2017 04:33:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324866#M72160</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2017-01-15T04:33:03Z</dc:date>
    </item>
    <item>
      <title>Re: All possible shopping lists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324885#M72169</link>
      <description>&lt;P&gt;And a macro version ... not very flexible and making assumptions about the required parameters and processing ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro combinations (have=, want=, n_items=);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%local i;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data &amp;amp;want;&lt;/P&gt;
&lt;P&gt;array items {&amp;amp;n_items} item1-item&amp;amp;n_items;&lt;/P&gt;
&lt;P&gt;array prices {&amp;amp;n_items} price1-price&amp;amp;n_items;&lt;/P&gt;
&lt;P&gt;array v {&amp;amp;n_items};&lt;/P&gt;
&lt;P&gt;do i=1 to 5;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set &amp;amp;have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;items{i} = item;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;prices{i} = price;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;%do i=1 %to &amp;amp;n_items;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;do v&amp;amp;i = 0, price&amp;amp;i;&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;length item_list $ &amp;amp;n_items;&lt;/P&gt;
&lt;P&gt;item_list = repeat('N', &amp;amp;n_items-1);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;do i=1 to &amp;amp;n_items;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if v{i} then substr(item_list, i, 1)='Y';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;total_cost = sum(of v{*});&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;output;&lt;/P&gt;
&lt;P&gt;%do i=1 %to &amp;amp;n_items;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;end;&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;stop;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%mend combinations;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code is untested at this point. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Even though I've written several programs of this type, I've never seen fit to convert it to a macro. &amp;nbsp;The requirements always seem to change enough each time so that it's easier to copy and modify a SAS program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Jan 2017 13:28:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324885#M72169</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-15T13:28:59Z</dc:date>
    </item>
    <item>
      <title>Re: All possible shopping lists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324888#M72170</link>
      <description>&lt;P&gt;It didn't work because the tab characters (in the first data step) were lost in the post. I changed them to commas, as shown below, and the code produces the desired file. I only posted it because I noticed that no one had responded by the time I saw your initial request. However, while it does indeed work, the other responses had much more efficient code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data fmtdata;&lt;BR /&gt;infile cards dlm=',';&lt;BR /&gt;retain fmtname 'pcodes' type 'I';&lt;BR /&gt;input start label;&lt;BR /&gt;cards;&lt;BR /&gt;1,10&lt;BR /&gt;2,20&lt;BR /&gt;3,30&lt;BR /&gt;4,40&lt;BR /&gt;5,50&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc format cntlin = fmtdata;&lt;BR /&gt;run;&lt;BR /&gt;%macro comb(dsout2=&amp;amp;dsout,dsin2=__d1,k=,n=,len=) ;&lt;BR /&gt;%local dsin2 dsout2 numvar i start stop n k len ;&lt;BR /&gt;%let start = 1 ;&lt;BR /&gt;%let stop = %eval( &amp;amp;n - &amp;amp;k) ;&lt;BR /&gt;data &amp;amp;dsout2 ;&lt;BR /&gt;set &amp;amp;dsin2 ;&lt;BR /&gt;array _vals {&amp;amp;n} $ ;&lt;BR /&gt;array combo {&amp;amp;k} $ &amp;amp;len ;&lt;BR /&gt;keep j combo1 - combo&amp;amp;k ;&lt;BR /&gt;retain j 1 ;&lt;BR /&gt;%do i = 1 %to &amp;amp;k;&lt;BR /&gt;%let stop = %eval(&amp;amp;stop + 1) ;&lt;BR /&gt;do var&amp;amp;i = &amp;amp;start to &amp;amp;stop ;&lt;BR /&gt;%let start = %str(%(var&amp;amp;i + 1 %)) ;&lt;BR /&gt;%end ;&lt;BR /&gt;%do i = 1 %to &amp;amp;k ;&lt;BR /&gt;combo{&amp;amp;i} = _vals{var&amp;amp;i} ;&lt;BR /&gt;%end ;&lt;BR /&gt;output ;&lt;BR /&gt;j + 1 ;&lt;BR /&gt;%do i = 1 %to &amp;amp;k ;&lt;BR /&gt;end ;&lt;BR /&gt;%end ;&lt;BR /&gt;run;&lt;BR /&gt;%mend comb;&lt;BR /&gt;%macro setup(dsout=_NULL_,dsin=&amp;amp;syslast,var=,ks=) ;&lt;BR /&gt;%local dsout dsin var totvar ks ;&lt;BR /&gt;proc freq data=&amp;amp;dsin noprint ;&lt;BR /&gt;tables &amp;amp;var / out=__count ;&lt;BR /&gt;data _NULL_ ;&lt;BR /&gt;set __count end=last ;&lt;BR /&gt;retain len oldlen 0 ;&lt;BR /&gt;nk + 1 ;&lt;BR /&gt;len = length(&amp;amp;var) ;&lt;BR /&gt;if ( len &amp;gt; oldlen ) then oldlen = len ;&lt;BR /&gt;if count &amp;gt; 1 then do;&lt;BR /&gt;put "WARNING: The variable %upcase(&amp;amp;var)" /&lt;BR /&gt;"WARNING: has “ count “ occurrences of the value "&lt;BR /&gt;var;&lt;BR /&gt;end ;&lt;BR /&gt;if last then do ;&lt;BR /&gt;if &amp;amp;ks &amp;gt; nk then do;&lt;BR /&gt;put "ERROR: K greater than the Number of values (N)" ;&lt;BR /&gt;put "ERROR: Value of K: &amp;amp;ks Value of N: " nk ;&lt;BR /&gt;put "ERROR: EXITING Macro!!!!!!!!!!!!!!!!!!" ;&lt;BR /&gt;end ;&lt;BR /&gt;call symput('totvar',trim(left(put(nk,5.))) ) ;&lt;BR /&gt;call symput('length',put(oldlen,3.) );&lt;BR /&gt;end ;&lt;BR /&gt;run ;&lt;BR /&gt;%if &amp;amp;ks &amp;gt; &amp;amp;totvar %then %goto exit ;&lt;BR /&gt;data __d1 ;&lt;BR /&gt;set __count end=last ;&lt;BR /&gt;array _vals {&amp;amp;totvar} $ &amp;amp;length;&lt;BR /&gt;retain _vals1-_vals&amp;amp;totvar ;&lt;BR /&gt;keep _vals1-_vals&amp;amp;totvar ;&lt;BR /&gt;_vals{_N_} = &amp;amp;var ;&lt;BR /&gt;if last then output ;&lt;BR /&gt;%comb(k=&amp;amp;ks,n=&amp;amp;totvar,len=&amp;amp;length)&lt;BR /&gt;run ;&lt;BR /&gt;%exit: %mend setup ;&lt;BR /&gt;%setup(var=start,ks=1,dsin=fmtdata,dsout=combin1)&lt;BR /&gt;%setup(var=start,ks=2,dsin=fmtdata,dsout=combin2)&lt;BR /&gt;%setup(var=start,ks=3,dsin=fmtdata,dsout=combin3)&lt;BR /&gt;%setup(var=start,ks=4,dsin=fmtdata,dsout=combin4)&lt;BR /&gt;%setup(var=start,ks=5,dsin=fmtdata,dsout=combin5)&lt;BR /&gt;data want (drop=i j);&lt;BR /&gt;set combin1 (rename=(combo1=item1))&lt;BR /&gt;combin2 (rename=(combo1-combo2=item1-item2))&lt;BR /&gt;combin3 (rename=(combo1-combo3=item1-item3))&lt;BR /&gt;combin4 (rename=(combo1-combo4=item1-item4))&lt;BR /&gt;combin5 (rename=(combo1-combo5=item1-item5));&lt;BR /&gt;array combos(5) item1-item5;&lt;BR /&gt;array prices(5);&lt;BR /&gt;do i=1 to 5;&lt;BR /&gt;prices(i)=input(combos(i),pcodes.);&lt;BR /&gt;end;&lt;BR /&gt;total=sum(of prices(*));&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Jan 2017 14:38:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/324888#M72170</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-01-15T14:38:53Z</dc:date>
    </item>
    <item>
      <title>Re: All possible shopping lists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/325027#M72238</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data price;
   array p[5];
   input p[*];
   cards;
10 20 30 40 50
;;;;
   run;
proc print;
   run;
proc summary data=price chartype;
   class p:;
   output out=comb(drop=_freq_);
   run;
data comb;
   set comb;
   total = sum(of p:);
   run;
proc print;
   run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG title="Capture.PNG" alt="Capture.PNG" src="https://communities.sas.com/t5/image/serverpage/image-id/6743iBDB01415066181A2/image-size/original?v=v2&amp;amp;px=-1" border="0" /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Jan 2017 13:50:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-possible-shopping-lists/m-p/325027#M72238</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2017-01-16T13:50:28Z</dc:date>
    </item>
  </channel>
</rss>

