<?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: creating new columns and rows with summary data using proc sql in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/creating-new-columns-and-rows-with-summary-data-using-proc-sql/m-p/684975#M24263</link>
    <description>&lt;P&gt;First, make your data intelligent by transposing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=wide out=long (rename=(_name_=item_use) where=(col1 = 1));
by personid;
var bought_hat--wore_scarf;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then extract the use type and the item from item_use:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data final;
set long;
item = scan(item_use,2);
bought = 0;
wore = 0;
select (scan(item_use,1));
  when ('bought') bought = 1; 
  when ('wore') wore = 1;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and you can use SAS procedures like SUMMARY, FREQ or SQL to get your sums.&lt;/P&gt;</description>
    <pubDate>Fri, 18 Sep 2020 14:04:48 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-09-18T14:04:48Z</dc:date>
    <item>
      <title>creating new columns and rows with summary data using proc sql</title>
      <link>https://communities.sas.com/t5/New-SAS-User/creating-new-columns-and-rows-with-summary-data-using-proc-sql/m-p/684967#M24262</link>
      <description>&lt;P&gt;I have a dataset that looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;personID&lt;/TD&gt;&lt;TD&gt;bought_hat&lt;/TD&gt;&lt;TD&gt;wore_hat&lt;/TD&gt;&lt;TD&gt;bought_gloves&lt;/TD&gt;&lt;TD&gt;wore_gloves&lt;/TD&gt;&lt;TD&gt;bought_scarf&lt;/TD&gt;&lt;TD&gt;wore_scarf&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;(1=yes, 0=no)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and I want to use proc sql to summarise the total bought/wore of each item of clothing as well as a total for all items of clothing like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;item&lt;/TD&gt;&lt;TD&gt;total bought&lt;/TD&gt;&lt;TD&gt;total worn&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;hat&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;gloves&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;scarf&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;all items&lt;/TD&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;any ideas?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i know i can use proc freq, but in reality i have far more than two columns for each item of clothing, and far more than three items of clothing, and far more than three people.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks&lt;/P&gt;&lt;P&gt;ghbg&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2020 13:32:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/creating-new-columns-and-rows-with-summary-data-using-proc-sql/m-p/684967#M24262</guid>
      <dc:creator>ghbg</dc:creator>
      <dc:date>2020-09-18T13:32:44Z</dc:date>
    </item>
    <item>
      <title>Re: creating new columns and rows with summary data using proc sql</title>
      <link>https://communities.sas.com/t5/New-SAS-User/creating-new-columns-and-rows-with-summary-data-using-proc-sql/m-p/684975#M24263</link>
      <description>&lt;P&gt;First, make your data intelligent by transposing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=wide out=long (rename=(_name_=item_use) where=(col1 = 1));
by personid;
var bought_hat--wore_scarf;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then extract the use type and the item from item_use:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data final;
set long;
item = scan(item_use,2);
bought = 0;
wore = 0;
select (scan(item_use,1));
  when ('bought') bought = 1; 
  when ('wore') wore = 1;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and you can use SAS procedures like SUMMARY, FREQ or SQL to get your sums.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2020 14:04:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/creating-new-columns-and-rows-with-summary-data-using-proc-sql/m-p/684975#M24263</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-09-18T14:04:48Z</dc:date>
    </item>
    <item>
      <title>Re: creating new columns and rows with summary data using proc sql</title>
      <link>https://communities.sas.com/t5/New-SAS-User/creating-new-columns-and-rows-with-summary-data-using-proc-sql/m-p/685031#M24268</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; In the interest of providing alternatives, again, if you transpose your data but modify it slightly differently, then you can use either PROC REPORT or PROC TABULATE to create these results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_0-1600446330047.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/49540i98F7119299D274A7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_0-1600446330047.png" alt="Cynthia_sas_0-1600446330047.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fakedata;
infile datalines;
input personID bought_hat wore_hat bought_gloves wore_gloves bought_scarf wore_scarf;
return;
datalines;
1 1	0 1	1 0 0
2 1	1 1 0 1 0
3 0	0 1	1 1 0
;
run;

proc transpose 
   data=fakedata 
   out=tr_long ;
by personid;
var bought_hat--wore_scarf;
run;

data final;
  length Item $15 Status $8;
  set tr_long;
  Item = propcase(scan(_name_,2,'_'));
  Status = propcase(scan(_name_,1,'_'));
run;

proc report data=final;
title 'Summary with PROC REPORT';
  column item status,col1;
  define item / group style(column)=Header;
  define status/ across ' ';
  define col1 / 'Total';
  rbreak after / summarize style=Header;
  compute after;
    item='Total';
  endcomp;
run;
title;
  
proc tabulate data=final f=3.0;
title 'Summary with PROC TABULATE';
  class item status;
  var col1;
  table item=' ' all='Total'*{style=Header},
        status=' '*col1='Total'*sum=' '/
        box={label='Item' style={vjust=b}};
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2020 16:28:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/creating-new-columns-and-rows-with-summary-data-using-proc-sql/m-p/685031#M24268</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2020-09-18T16:28:43Z</dc:date>
    </item>
  </channel>
</rss>

