<?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: pivot table: transforming some columns into rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/pivot-table-transforming-some-columns-into-rows/m-p/742893#M232473</link>
    <description>&lt;P&gt;Under the help of Kurt , I understand what you want now.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Have;
 input id var1 var2 var3 varA varB ;
 datalines;
1 1 0 1 1 0 
2 0 0 1 0 1
3 0 1 0 0 0
4 1 0 0 1 0
5 1 1 1 1 1 
;
run;
proc sql;
create table want as
select sum(var1*varA) as varA,sum(var1*varB) as varB from have
union all
select sum(var2*varA) as varA,sum(var2*varB) as varB from have
union all
select sum(var3*varA) as varA,sum(var3*varB) as varB from have
;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 21 May 2021 13:28:41 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2021-05-21T13:28:41Z</dc:date>
    <item>
      <title>pivot table: transforming some columns into rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pivot-table-transforming-some-columns-into-rows/m-p/742675#M232356</link>
      <description>&lt;P&gt;I have a dataset in which each row is a date and there are many columns. All these columns are binary.&lt;/P&gt;&lt;P&gt;I wanted to know how to create a (pivot?) table where some of those columns were the rows now and the cells were the sum of those cases when varA is 1 and varB is one.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let's say this is my data:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Have;
 input id var1 var2 var3 varA varB ;
 datalines;
1 1 0 1 1 0 
2 0 0 1 0 1
3 0 1 0 0 0
4 1 0 0 1 0
5 1 1 1 1 1 
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is the final result I'm looking for:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 input varA varB ;
 datalines;
3 1
1 1
2 2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In this case the rows would be var1 var2 and va3 respectively.&lt;/P&gt;&lt;P&gt;How could I do this? Is it possible with a summary table? Could I do this for both when var A and varB are 1 and when they are 0, in the same table?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 15:04:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pivot-table-transforming-some-columns-into-rows/m-p/742675#M232356</guid>
      <dc:creator>catkat96</dc:creator>
      <dc:date>2021-05-20T15:04:44Z</dc:date>
    </item>
    <item>
      <title>Re: pivot table: transforming some columns into rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pivot-table-transforming-some-columns-into-rows/m-p/742690#M232362</link>
      <description>&lt;P&gt;Its confusing to me how you take the HAVE data set and turn it into the WANT data set. Can you explain the logic?&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 15:54:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pivot-table-transforming-some-columns-into-rows/m-p/742690#M232362</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-05-20T15:54:25Z</dc:date>
    </item>
    <item>
      <title>Re: pivot table: transforming some columns into rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pivot-table-transforming-some-columns-into-rows/m-p/742697#M232366</link>
      <description>&lt;P&gt;See this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Have;
 input id var1 var2 var3 varA varB ;
 datalines;
1 1 0 1 1 0 
2 0 0 1 0 1
3 0 1 0 0 0
4 1 0 0 1 0
5 1 1 1 1 1 
;

data pretrans;
set have;
name = "varA";
v1 = var1 * vara;
v2 = var2 * vara;
v3 = var3 * vara;
output;
name = "varB";
v1 = var1 * varb;
v2 = var2 * varb;
v3 = var3 * varb;
output;
keep name v1-v3;
run;

proc summary data=pretrans nway;
class name;
var v1-v3;
output out=sum sum()=;
run;

proc transpose
  data=sum
  out=want
;
id name;
var v1-v3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;if you need to make this more flexible, use arrays in the pretrans step.&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 16:05:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pivot-table-transforming-some-columns-into-rows/m-p/742697#M232366</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-20T16:05:18Z</dc:date>
    </item>
    <item>
      <title>Re: pivot table: transforming some columns into rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pivot-table-transforming-some-columns-into-rows/m-p/742893#M232473</link>
      <description>&lt;P&gt;Under the help of Kurt , I understand what you want now.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Have;
 input id var1 var2 var3 varA varB ;
 datalines;
1 1 0 1 1 0 
2 0 0 1 0 1
3 0 1 0 0 0
4 1 0 0 1 0
5 1 1 1 1 1 
;
run;
proc sql;
create table want as
select sum(var1*varA) as varA,sum(var1*varB) as varB from have
union all
select sum(var2*varA) as varA,sum(var2*varB) as varB from have
union all
select sum(var3*varA) as varA,sum(var3*varB) as varB from have
;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 May 2021 13:28:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pivot-table-transforming-some-columns-into-rows/m-p/742893#M232473</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-05-21T13:28:41Z</dc:date>
    </item>
  </channel>
</rss>

