<?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: Using proc sql to create all possible pairs of two variables in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Using-proc-sql-to-create-all-possible-pairs-of-two-variables/m-p/234571#M5922</link>
    <description>&lt;P&gt;If I may step in here ... I think the following modification of the two array definitions should work for you:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array l{&amp;amp;nbLetters} _character_;
array pair{&amp;amp;nbLetters} $17; /* Minimum required length depends on how you concatenate your strings */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I really like PG's solution because it's so elegant. Please note, however, that there are rare, exceptional cases where it would not work without further modification. For example, if the first character variable contained values such as "a:a" and "a_a", the PROC TRANSPOSE step would fail. But if your character strings are "well-behaved", no problems should occur.&lt;/P&gt;</description>
    <pubDate>Fri, 13 Nov 2015 12:42:56 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2015-11-13T12:42:56Z</dc:date>
    <item>
      <title>Using proc sql to create all possible pairs of two variables</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Using-proc-sql-to-create-all-possible-pairs-of-two-variables/m-p/234439#M5910</link>
      <description>&lt;P&gt;I am trying to use proc sql to create all possible pairs of two variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, if my starting data appears like this...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;letter number&lt;/P&gt;&lt;P&gt;a 1&lt;/P&gt;&lt;P&gt;b 2&lt;/P&gt;&lt;P&gt;c 3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like the output data to look like...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;pair1 pair2 pair3&lt;/P&gt;&lt;P&gt;a-1 b-2 c-3&lt;/P&gt;&lt;P&gt;a-1 b-3 c-2&lt;/P&gt;&lt;P&gt;a-2 b-1 c-3&lt;/P&gt;&lt;P&gt;a-2 b-3 c-2&lt;/P&gt;&lt;P&gt;a-3 b-1 c-2&lt;/P&gt;&lt;P&gt;a-3 b-2 c-1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have been able to get a single column with all 9 possible pairs, but when I try to put multiple pairs on a single line, things get messed up.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Nov 2015 18:47:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Using-proc-sql-to-create-all-possible-pairs-of-two-variables/m-p/234439#M5910</guid>
      <dc:creator>lcain</dc:creator>
      <dc:date>2015-11-12T18:47:22Z</dc:date>
    </item>
    <item>
      <title>Re: Using proc sql to create all possible pairs of two variables</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Using-proc-sql-to-create-all-possible-pairs-of-two-variables/m-p/234472#M5913</link>
      <description>&lt;P&gt;What you want is not a cartesian product as produced by SQL. In a cartesian product, the pair a-1 would occur only once...&lt;/P&gt;
&lt;P&gt;What you require is a list of all permutations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input letter $ number;
datalines;
a 1
b 2
c 3
;

proc sql noprint;
select count(letter) into :nbLetters from have;
quit;

proc transpose data=have out=temp(drop=_name_);
var number;
id letter;
run;

data want;
set temp;
array l{&amp;amp;nbLetters} _numeric_;
array pair{&amp;amp;nbLetters} $8;

do i = 1 to fact(&amp;amp;nbLetters);
    call allperm(i, of l{*});
    do j = 1 to &amp;amp;nbLetters;
        pair{j} = catx("-", vname(l{j}), l{j});
        end;
    output;
    end;
keep pair:;
run;

proc print data=want noobs; run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;you could replace &lt;STRONG&gt;allperm&lt;/STRONG&gt; by &lt;STRONG&gt;lexperm&lt;/STRONG&gt; to get only permutations of distinct nonmissing values.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Nov 2015 20:29:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Using-proc-sql-to-create-all-possible-pairs-of-two-variables/m-p/234472#M5913</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2015-11-12T20:29:27Z</dc:date>
    </item>
    <item>
      <title>Re: Using proc sql to create all possible pairs of two variables</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Using-proc-sql-to-create-all-possible-pairs-of-two-variables/m-p/234542#M5921</link>
      <description>&lt;P&gt;Thanks so much. In my actual data, both variables are character strings. The length of the variables range from 3-8 characters. How would I edit the code to work for two character strings?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again!&lt;/P&gt;</description>
      <pubDate>Fri, 13 Nov 2015 05:01:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Using-proc-sql-to-create-all-possible-pairs-of-two-variables/m-p/234542#M5921</guid>
      <dc:creator>lcain</dc:creator>
      <dc:date>2015-11-13T05:01:26Z</dc:date>
    </item>
    <item>
      <title>Re: Using proc sql to create all possible pairs of two variables</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Using-proc-sql-to-create-all-possible-pairs-of-two-variables/m-p/234571#M5922</link>
      <description>&lt;P&gt;If I may step in here ... I think the following modification of the two array definitions should work for you:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array l{&amp;amp;nbLetters} _character_;
array pair{&amp;amp;nbLetters} $17; /* Minimum required length depends on how you concatenate your strings */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I really like PG's solution because it's so elegant. Please note, however, that there are rare, exceptional cases where it would not work without further modification. For example, if the first character variable contained values such as "a:a" and "a_a", the PROC TRANSPOSE step would fail. But if your character strings are "well-behaved", no problems should occur.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Nov 2015 12:42:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Using-proc-sql-to-create-all-possible-pairs-of-two-variables/m-p/234571#M5922</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2015-11-13T12:42:56Z</dc:date>
    </item>
    <item>
      <title>Re: Using proc sql to create all possible pairs of two variables</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Using-proc-sql-to-create-all-possible-pairs-of-two-variables/m-p/234588#M5923</link>
      <description>&lt;P&gt;Thanks! That worked. I wasn't allowing for a long enough string for my concatenation.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Nov 2015 13:07:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Using-proc-sql-to-create-all-possible-pairs-of-two-variables/m-p/234588#M5923</guid>
      <dc:creator>lcain</dc:creator>
      <dc:date>2015-11-13T13:07:53Z</dc:date>
    </item>
    <item>
      <title>Re: Using proc sql to create all possible pairs of two variables</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Using-proc-sql-to-create-all-possible-pairs-of-two-variables/m-p/234697#M5931</link>
      <description>&lt;P&gt;One could code the transposing into the data step and thus avoid PROC TRANSPOSE:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length string1
       string2 $8;
input string1 string2;
cards;
first one
second two
third three
fourth four
;

proc sql noprint;
select count(*) into :n from have;
quit;

data want;
array s[&amp;amp;n] $8;
array t[&amp;amp;n] $8;
array p[&amp;amp;n] $17;
do i=1 by 1 until(e);
  set have end=e;
  s[i]=string1;
  t[i]=string2;
end;
do i=1 to fact(&amp;amp;n);
  call allperm(i, of t[*]);
  do j=1 to &amp;amp;n;
    p[j]=catx('-', s[j], t[j]);
  end;
  output;
end;
keep p:;
run;

proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Nov 2015 20:24:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Using-proc-sql-to-create-all-possible-pairs-of-two-variables/m-p/234697#M5931</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2015-11-13T20:24:21Z</dc:date>
    </item>
    <item>
      <title>Re: Using proc sql to create all possible pairs of two variables</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Using-proc-sql-to-create-all-possible-pairs-of-two-variables/m-p/236965#M6104</link>
      <description>&lt;P&gt;Another solution:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input letter $ number;
datalines;
a 1
b 2
c 3
;


PROC SQL;
   CREATE TABLE WORK.temp1 AS 
   SELECT t1.letter
      FROM WORK.HAVE t1;
QUIT;

PROC SQL;
   CREATE TABLE WORK.temp2 AS 
   SELECT t1.number
      FROM WORK.HAVE t1;
QUIT;

PROC SQL;
   CREATE TABLE WORK.temp3 AS 
   SELECT t1.letter, 
          t2.number
      FROM WORK.temp1 t1
           CROSS JOIN WORK.temp2 t2;
QUIT;

PROC SQL;
   CREATE TABLE WORK.WANT0 AS 
   SELECT DISTINCT t1.letter, 
          t1.number, 
          t2.letter AS letter1, 
          t2.number AS number1, 
          t3.letter AS letter2, 
          t3.number AS number2
      FROM WORK.temp3 t1
           CROSS JOIN WORK.temp3 t2
           CROSS JOIN WORK.temp3 t3
      WHERE t1.letter &amp;lt; t2.letter AND t2.letter &amp;lt; t3.letter AND t1.number NOT = t2.number AND t2.number NOT = t3.number 
           AND t1.number NOT = t3.number;
QUIT;
PROC SQL;
   CREATE TABLE WORK.WANT AS 
   SELECT /* VAR1 */
            (CATX('-',t1.letter,put(t1.number,z.))) AS VAR1, 
          /* VAR2 */
            (CATX('-',t1.letter1, put( t1.number1,z.))) AS VAR2, 
          /* VAR3 */
            (CATX('-',t1.letter2,PUT(t1.number2,Z.))) AS VAR3
      FROM WORK.WANT0 t1;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 30 Nov 2015 14:45:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Using-proc-sql-to-create-all-possible-pairs-of-two-variables/m-p/236965#M6104</guid>
      <dc:creator>ToniMasso</dc:creator>
      <dc:date>2015-11-30T14:45:11Z</dc:date>
    </item>
  </channel>
</rss>

