<?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: Proportion calculation for each pairwise combination in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/666999#M199670</link>
    <description>&lt;P&gt;You can try the below code , i updated the &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt; code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select 
    catx("-", a._name_, b._name_) as combination,
    sum(a.col1*b.col1) / coalesce(sum(a.col1), 1) as proportion, sum(a.col1*b.col1) as type1, coalesce(sum(a.col1), 1) as type2
from 
    have as a inner join
    have as b on a.id=b.id and a._name_ ne b._name_
group by a._name_, b._name_;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 05 Jul 2020 07:01:36 GMT</pubDate>
    <dc:creator>Jagadishkatam</dc:creator>
    <dc:date>2020-07-05T07:01:36Z</dc:date>
    <item>
      <title>Proportion calculation for each pairwise combination</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/666975#M199662</link>
      <description>&lt;P&gt;Hello friends!&lt;/P&gt;&lt;P&gt;I was hoping someone could help me think through this programming question. I have a dataset that looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data my_data;&lt;BR /&gt;input id option1 option2 option3 option 4;&lt;BR /&gt;cards;&lt;BR /&gt;A 1 0 1 1&lt;/P&gt;&lt;P&gt;B 1 1 1 1&lt;/P&gt;&lt;P&gt;C 0 1 1 1&amp;nbsp;&lt;/P&gt;&lt;P&gt;D 0 1 0 1&lt;/P&gt;&lt;P&gt;E 1 1 1 1&lt;/P&gt;&lt;P&gt;F 0 0 1 1;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I need to do is calculate a specific proportion for EACH pairwise combination. The proportion would essentially be the: (# of individuals in BOTH options) / (# of individuals in the first option). For example, I would need the calculation for Option1_Option2 to be: # option 1 AND option 2 / # option 1 = 1/3. Similarly, Option2_Option1 would be: # option 1 AND option 2 / # option 2 = 1/4. And so on...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need the resulting table output to look like this:&lt;/P&gt;&lt;P&gt;Option1_Option2 2/3&lt;/P&gt;&lt;P&gt;Option1_Option3&amp;nbsp; 3/3&lt;/P&gt;&lt;P&gt;Option1_Option4 3/3&lt;/P&gt;&lt;P&gt;Option2_Option1 2/4&lt;/P&gt;&lt;P&gt;Option2_Option3 3/4&lt;/P&gt;&lt;P&gt;Option2_Option4 4/4&lt;/P&gt;&lt;P&gt;Option3_Option1 3/5&lt;/P&gt;&lt;P&gt;Option3_Option2 3/5&lt;/P&gt;&lt;P&gt;Option3_Option4 5/5&lt;/P&gt;&lt;P&gt;Option4_Option1 3/6&lt;/P&gt;&lt;P&gt;Option4_Option2 4/6&lt;/P&gt;&lt;P&gt;Option4_Option3 5/6&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Where I get tripped up in the coding is the pairwise combinations that I need to look at. Any advice or help would be hugely appreciated! Thank you in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jul 2020 03:12:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/666975#M199662</guid>
      <dc:creator>telc24</dc:creator>
      <dc:date>2020-07-05T03:12:19Z</dc:date>
    </item>
    <item>
      <title>Re: Proportion calculation for each pairwise combination</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/666979#M199663</link>
      <description>&lt;P&gt;A well formulated question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, it would seem to me that Option1_Option2 should be 2/3&amp;nbsp; (AE)/(ABE) right?&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jul 2020 03:04:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/666979#M199663</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-07-05T03:04:41Z</dc:date>
    </item>
    <item>
      <title>Re: Proportion calculation for each pairwise combination</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/666980#M199664</link>
      <description>&lt;P&gt;Yes! Sorry about that! I played around with the mock dataset a few times and didn't catch that change!&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jul 2020 03:06:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/666980#M199664</guid>
      <dc:creator>telc24</dc:creator>
      <dc:date>2020-07-05T03:06:33Z</dc:date>
    </item>
    <item>
      <title>Re: Proportion calculation for each pairwise combination</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/666983#M199666</link>
      <description>&lt;P&gt;This should do it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data my_data;
input id $ option1 option2 option3 option4;
cards;
A 1 0 1 1
B 1 1 1 1
C 0 1 1 1 
D 0 1 0 1
E 1 1 1 1
F 0 0 1 1
;

proc transpose data=my_data out=have;
var option:;
by id;
run;

proc sql;
create table want as
select 
    catx("-", a._name_, b._name_) as combination,
    sum(a.col1*b.col1) / coalesce(sum(a.col1), 1) as proportion
from 
    have as a inner join
    have as b on a.id=b.id and a._name_ ne b._name_
group by a._name_, b._name_;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Jul 2020 03:18:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/666983#M199666</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-07-05T03:18:05Z</dc:date>
    </item>
    <item>
      <title>Re: Proportion calculation for each pairwise combination</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/666984#M199667</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;this is brilliant!! Thank you so much! You just saved me so much time &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jul 2020 03:33:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/666984#M199667</guid>
      <dc:creator>telc24</dc:creator>
      <dc:date>2020-07-05T03:33:23Z</dc:date>
    </item>
    <item>
      <title>Re: Proportion calculation for each pairwise combination</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/666986#M199669</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;, thank you again for your help! I have one more question that would help me automate things. I also have one more identifier that I want to account for (the "type" variable below).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data my_data;&lt;/P&gt;&lt;P&gt;input id $ type option1 option2 option3 option4;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;A 1 1 0 1 1&lt;/P&gt;&lt;P&gt;B 2 1 1 1 1&lt;/P&gt;&lt;P&gt;C 1 0 1 1 1&lt;/P&gt;&lt;P&gt;D 2 0 1 0 1&lt;/P&gt;&lt;P&gt;E 1 1 1 1 1&lt;/P&gt;&lt;P&gt;F 2 0 0 1 1&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Currently, the code you generously provided includes the proportion calculation for types 1 AND 2. Is there an easy way to additionally add a column for the proportion calculation for type 1 only? And then a column for the proportion calculation for type 2 only?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm not very familiar with using proc sql to generate tables, so any general resources to help me learn would also be greatly appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you, again!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jul 2020 04:00:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/666986#M199669</guid>
      <dc:creator>telc24</dc:creator>
      <dc:date>2020-07-05T04:00:08Z</dc:date>
    </item>
    <item>
      <title>Re: Proportion calculation for each pairwise combination</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/666999#M199670</link>
      <description>&lt;P&gt;You can try the below code , i updated the &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt; code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select 
    catx("-", a._name_, b._name_) as combination,
    sum(a.col1*b.col1) / coalesce(sum(a.col1), 1) as proportion, sum(a.col1*b.col1) as type1, coalesce(sum(a.col1), 1) as type2
from 
    have as a inner join
    have as b on a.id=b.id and a._name_ ne b._name_
group by a._name_, b._name_;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Jul 2020 07:01:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/666999#M199670</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2020-07-05T07:01:36Z</dc:date>
    </item>
    <item>
      <title>Re: Proportion calculation for each pairwise combination</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/667008#M199673</link>
      <description>&lt;PRE&gt;data my_data;
input id $ option1 option2 option3 option4;
cards;
A 1 0 1 1
B 1 1 1 1
C 0 1 1 1 
D 0 1 0 1
E 1 1 1 1
F 0 0 1 1
;

proc corr data=my_data out=have(where=(_type_='SSCP'))  sscp noprint;
run;
proc transpose data=have(where=(_name_ ne 'Intercept')) out=have2(index=(name)) name=name;
by _name_;
var option: ;
run;

proc summary data=my_data ;
var option: ;
output out=total sum=;
run;
proc transpose data=total out=total2 name=name ;
var option: ;
run;

data want;
 merge have2 total2(rename=(col1=col2));
 by name;
 want=col1/col2;
run;&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Jul 2020 12:04:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/667008#M199673</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-07-05T12:04:29Z</dc:date>
    </item>
    <item>
      <title>Re: Proportion calculation for each pairwise combination</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/667010#M199675</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt;&amp;nbsp;.Currently, your code is giving me additional columns for the numerator of the "combination" variable (variable=type1) and the denominator of the "combination" variable (variable=type2).&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;What I'm looking for is a similar "combination" calculation variable for instances for the individuals who are type=1. And then a similar "combination" calculation variable for the individuals who are type=2. Is there a way to add an if/then or when statement to proc sql to do so? For example, the table output for the dataset shown below would look like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Dataset:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Arial',sans-serif; color: #333333;"&gt;data my_data;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Arial',sans-serif; color: #333333;"&gt;input id $ type option1 option2 option3 option4;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Arial',sans-serif; color: #333333;"&gt;cards;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Arial',sans-serif; color: #333333;"&gt;A 1 1 0 1 1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Arial',sans-serif; color: #333333;"&gt;B 2 1 1 1 1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Arial',sans-serif; color: #333333;"&gt;C 1 0 1 1 1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Arial',sans-serif; color: #333333;"&gt;D 2 0 1 0 1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Arial',sans-serif; color: #333333;"&gt;E 1 1 1 1 1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Arial',sans-serif; color: #333333;"&gt;F 2 0 0 1 1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Arial',sans-serif; color: #333333;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Arial',sans-serif; color: #333333;"&gt;Output table:&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Arial',sans-serif; color: #333333;"&gt;Option1-Option2 2/3 1/2 1/1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Arial',sans-serif; color: #333333;"&gt;Option1-Option3&amp;nbsp;3/3 2/2 1/1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Arial',sans-serif; color: #333333;"&gt;Option1-Option4 3/3 2/2 1/1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Arial',sans-serif; color: #333333;"&gt;Option2-Option1 2/4 1/2 1/2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Arial',sans-serif; color: #333333;"&gt;etc.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jul 2020 12:56:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/667010#M199675</guid>
      <dc:creator>telc24</dc:creator>
      <dc:date>2020-07-05T12:56:49Z</dc:date>
    </item>
    <item>
      <title>Re: Proportion calculation for each pairwise combination</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/667064#M199690</link>
      <description>&lt;P&gt;Here are two methods for obtaining proportions by type, assuming that ids are unique keys:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data my_data;
input id $ type option1 option2 option3 option4;
cards;
A 1 1 0 1 1
B 2 1 1 1 1
C 1 0 1 1 1 
D 2 0 1 0 1
E 1 1 1 1 1
F 2 0 0 1 1
;

proc transpose data=my_data out=have;
var option:;
by id type;
run;

proc sql;
create table want1 as
select 
    catx("-", a._name_, b._name_) as combination,
    sum(a.col1 and b.col1) / coalesce(sum(a.col1), 1) as proportion,
    sum(a.type=1 and a.col1 and b.col1) / coalesce(sum(a.type=1 and a.col1), 1) as proportionType1,
    sum(a.type=2 and a.col1 and b.col1) / coalesce(sum(a.type=2 and a.col1), 1) as proportionType2
from 
    have as a inner join
    have as b on a.id=b.id and a._name_ ne b._name_
group by a._name_, b._name_;
quit;


proc sql;
create table want2 as
select aa.combination, proportion, proportionType1, proportionType2 from

   (select 
        catx("-", a._name_, b._name_) as combination,
        sum(a.col1 and b.col1) / coalesce(sum(a.col1), 1) as proportion
    from 
        have as a inner join
        have as b on a.id=b.id and a._name_ ne b._name_
    group by a._name_, b._name_) as aa left join

   (select 
        catx("-", a._name_, b._name_) as combination,
        sum(a.col1 and b.col1) / coalesce(sum(a.col1), 1) as proportionType1
    from 
        have as a inner join
        have as b on a.id=b.id and a._name_ ne b._name_
    where a.type = 1
    group by a._name_, b._name_) as bb on aa.combination = bb.combination left join

   (select 
        catx("-", a._name_, b._name_) as combination,
        sum(a.col1 and b.col1) / coalesce(sum(a.col1), 1) as proportionType2
    from 
        have as a inner join
        have as b on a.id=b.id and a._name_ ne b._name_
    where a.type = 2
    group by a._name_, b._name_) as cc on aa.combination = cc.combination
    ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To scale this up to, say, 20 types, you would need a different approach involving an extra transposition.&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jul 2020 21:35:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proportion-calculation-for-each-pairwise-combination/m-p/667064#M199690</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-07-05T21:35:38Z</dc:date>
    </item>
  </channel>
</rss>

