<?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: Obtain all possible pairs from a list in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470473#M120408</link>
    <description>&lt;P&gt;I&amp;nbsp; have a class to attend at my college in 5 mins,. If you are familiar with proc format cntrl in/cntrl out look up formatted dataset, you could replace my hash with formats which essentially does the same. Or i can look into it in 3 hours&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Alternatively, you can filter the Cartesian product given by&amp;nbsp; paigemiller to only output the pairs with a where condition&lt;/P&gt;</description>
    <pubDate>Thu, 14 Jun 2018 22:33:59 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-06-14T22:33:59Z</dc:date>
    <item>
      <title>Obtain all possible pairs from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470463#M120402</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have the following data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input element$ 1-2 qt;&lt;BR /&gt;datalines;&lt;BR /&gt;A 1&lt;BR /&gt;B 2&lt;BR /&gt;Cc3&lt;BR /&gt;D 4&lt;BR /&gt;Ee5&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to obtain all possible pairs like this:&lt;/P&gt;
&lt;P&gt;A B&lt;/P&gt;
&lt;P&gt;A Cc&lt;/P&gt;
&lt;P&gt;A D&lt;/P&gt;
&lt;P&gt;A Ee&lt;/P&gt;
&lt;P&gt;B Cc&lt;/P&gt;
&lt;P&gt;B D&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jun 2018 22:07:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470463#M120402</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2018-06-14T22:07:59Z</dc:date>
    </item>
    <item>
      <title>Re: Obtain all possible pairs from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470466#M120403</link>
      <description>&lt;P&gt;Will this help?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input element$ 1-2 qt;
datalines;
A 1
B 2
Cc3
D 4
Ee5
;
run;

proc sql;
select max(qt) into :m
from have;
quit;

data want;
if _n_=1 then do;
if 0 then set have;
  dcl hash H (dataset:'have') ;
   h.definekey  ("qt") ;
   h.definedata ("element") ;
   h.definedone () ;
end;
do i=1 to &amp;amp;m-1;
do j=i+1 to &amp;amp;m;
	rc=h.find(key:i);
	elementa=element;
	rc=h.find(key:j);
	elementb=element;
output;
end;
end;
keep elementa elementb;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Jun 2018 22:21:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470466#M120403</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-06-14T22:21:19Z</dc:date>
    </item>
    <item>
      <title>Re: Obtain all possible pairs from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470468#M120404</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
    create table want as select a.element,b.element as element1 from have as a,have as b;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Jun 2018 22:24:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470468#M120404</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-06-14T22:24:45Z</dc:date>
    </item>
    <item>
      <title>Re: Obtain all possible pairs from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470470#M120406</link>
      <description>&lt;P&gt;Hi novinsorin,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am not familiar with hash, is it possible to get the solution in a macro form similar to this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;select element&lt;BR /&gt;into :element_list separated by ' '&lt;BR /&gt;from have&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;BR /&gt;proc sql;&lt;BR /&gt;select count(element)&lt;BR /&gt;into :n&lt;BR /&gt;from have&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;%put &amp;amp;element_list;&lt;BR /&gt;%put &amp;amp;n;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;%macro m1;&lt;BR /&gt;data want;&lt;BR /&gt;%do i=1 %to &amp;amp;n;&lt;BR /&gt;%let var1=%scan(&amp;amp;element_list,&amp;amp;i);&lt;BR /&gt;var1=&amp;amp;var1;&lt;/P&gt;
&lt;P&gt;%end;&lt;BR /&gt;%mend m1;&lt;BR /&gt;%m1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jun 2018 22:30:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470470#M120406</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2018-06-14T22:30:12Z</dc:date>
    </item>
    <item>
      <title>Re: Obtain all possible pairs from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470472#M120407</link>
      <description>&lt;P&gt;Hi PaigeMiller,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;this code is quite simple, and I didn't even think of it!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But this code has repetitions, that is, since I have 5 elements it gives me all the pairs twice and also the pair of each element with itself&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;5sq = 25 pairs,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but if I want the unique non-same pairs I should get (25-5)/2 = 10 pairs&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jun 2018 22:33:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470472#M120407</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2018-06-14T22:33:28Z</dc:date>
    </item>
    <item>
      <title>Re: Obtain all possible pairs from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470473#M120408</link>
      <description>&lt;P&gt;I&amp;nbsp; have a class to attend at my college in 5 mins,. If you are familiar with proc format cntrl in/cntrl out look up formatted dataset, you could replace my hash with formats which essentially does the same. Or i can look into it in 3 hours&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Alternatively, you can filter the Cartesian product given by&amp;nbsp; paigemiller to only output the pairs with a where condition&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jun 2018 22:33:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470473#M120408</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-06-14T22:33:59Z</dc:date>
    </item>
    <item>
      <title>Re: Obtain all possible pairs from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470477#M120411</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12982"&gt;@ilikesas&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi PaigeMiller,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;this code is quite simple, and I didn't even think of it!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But this code has repetitions, that is, since I have 5 elements it gives me all the pairs twice and also the pair of each element with itself&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;5sq = 25 pairs,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but if I want the unique non-same pairs I should get (25-5)/2 = 10 pairs&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Minor modification of &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;'s code;&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table want as
   select distinct a.element,b.element as elementb
   from have as a, have as b
   where a.element&amp;lt;b.element;
quit;&lt;/PRE&gt;
&lt;P&gt;You did not request unique solutions or ordered, so add the distinct to get the one group and the where to select the ordered pair.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jun 2018 22:43:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470477#M120411</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-06-14T22:43:55Z</dc:date>
    </item>
    <item>
      <title>Re: Obtain all possible pairs from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470487#M120413</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12982"&gt;@ilikesas&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi PaigeMiller,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;this code is quite simple, and I didn't even think of it!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But this code has repetitions, that is, since I have 5 elements it gives me all the pairs twice and also the pair of each element with itself&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;5sq = 25 pairs,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but if I want the unique non-same pairs I should get (25-5)/2 = 10 pairs&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That's not all possible, that's all possible unique combinations, irrespective of order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another method is using ALLCOMB().&lt;/P&gt;
&lt;P&gt;See this example here.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jun 2018 00:51:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470487#M120413</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-06-15T00:51:24Z</dc:date>
    </item>
    <item>
      <title>Re: Obtain all possible pairs from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470498#M120419</link>
      <description>&lt;P&gt;These combinations include duplicates and same-element combinations, whereas I don't want these duplicates and same element combinations - how would I in this case delete the duplicates and same value combs?&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jun 2018 01:36:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470498#M120419</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2018-06-15T01:36:13Z</dc:date>
    </item>
    <item>
      <title>Re: Obtain all possible pairs from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470505#M120423</link>
      <description>&lt;P&gt;Sorry, I forgot the link.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let n=5;              /* total number of items */          
%let k=2;              /* number of items per row */
/* generate combinations of n items taken k at a time */
data Comb(keep=c1-c&amp;amp;k);
array c[&amp;amp;k] (&amp;amp;k.*0);   /* initialize array to 0 */
array list[&amp;amp;n] _temporary_ (1, 2, 3, 4, 5); /*values in the array to set into groups of 2*/
ncomb = comb(&amp;amp;n, &amp;amp;k);  /* number of combinations */
do j = 1 to ncomb;
   rc = lexcombi(&amp;amp;n, &amp;amp;k, of c[*]);
   
   do i=1 to dim(c);
        c(i)=list(c(i));
   end;
   output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: and forgot it again:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/iml/2014/07/28/lexicographic-combinations.html&amp;nbsp;" target="_blank"&gt;https://blogs.sas.com/content/iml/2014/07/28/lexicographic-combinations.html&amp;nbsp;&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jun 2018 02:42:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470505#M120423</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-06-15T02:42:27Z</dc:date>
    </item>
    <item>
      <title>Re: Obtain all possible pairs from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470515#M120427</link>
      <description>&lt;P&gt;The solution by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;yields 10 records as wanted&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Please accept it (or&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;'s solution if you prefer) as the answer to your question&lt;/P&gt;
&lt;P&gt;2. To save everyone's time, do try to ask the full question right away rather than adding conditions as you go&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jun 2018 04:35:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470515#M120427</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-06-15T04:35:13Z</dc:date>
    </item>
    <item>
      <title>Re: Obtain all possible pairs from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470516#M120428</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input element$ 1-2 qt;
datalines;
A 1
B 2
Cc3
D 4
Ee5
;
run;
DATA fmtDataset;
 SET have;
 RETAIN fmtname 'fmtcode' type 'N';
 RENAME qt = Start;
 LABEL =element;
RUN; 
PROC FORMAT CNTLIN=fmtDataset;
RUN; 
proc sql;
select max(qt) into :m
from have;
quit;

data want;
do i=1 to &amp;amp;m-1;
do j=i+1 to &amp;amp;m;
elementa=put(i,fmtcode.); 
elementb=put(j,fmtcode.); 
output;
end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Jun 2018 05:06:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470516#M120428</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-06-15T05:06:23Z</dc:date>
    </item>
    <item>
      <title>Re: Obtain all possible pairs from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470593#M120436</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input element$ 1-2 qt;
datalines;
A 1
B 2
Cc3
D 4
Ee5
;

data want;
 set have end=last nobs=nobs;
 array x{999} $ 32 _temporary_;
 x{_n_}=element;
 if last then do;
   do i=1 to nobs-1;
     var1=x{i};
     do j=i+1 to nobs;
       var2=x{j};output;
	 end;
   end;
 end;
 keep var1 var2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Jun 2018 13:47:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470593#M120436</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-06-15T13:47:09Z</dc:date>
    </item>
    <item>
      <title>Re: Obtain all possible pairs from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470608#M120439</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12982"&gt;@ilikesas&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi PaigeMiller,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;this code is quite simple, and I didn't even think of it!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But this code has repetitions, that is, since I have 5 elements it gives me all the pairs twice and also the pair of each element with itself&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;5sq = 25 pairs,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but if I want the unique non-same pairs I should get (25-5)/2 = 10 pairs&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; (and probably others) have pointed out ... you can then reduce the number of pairs to what you want via any number of methods in SAS (SQL&amp;nbsp;or data step) ... but your original problem did not ask for "unique non-same pairs", it asked for "all possible pairs" (in fact, you asked for "all possible pairs" twice, once in the title and once in the text of your original message).&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jun 2018 14:22:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtain-all-possible-pairs-from-a-list/m-p/470608#M120439</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-06-15T14:22:17Z</dc:date>
    </item>
  </channel>
</rss>

