<?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: proc sql having clause pivot table in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-having-clause-pivot-table/m-p/286259#M59485</link>
    <description>Don't use SQL to create the actual pivot. SQL is mainly for data manipulation / aggregation,  not advanced report layout.&lt;BR /&gt;Take a look at TABULATE and REPORT  procedures instead.</description>
    <pubDate>Thu, 21 Jul 2016 20:18:53 GMT</pubDate>
    <dc:creator>LinusH</dc:creator>
    <dc:date>2016-07-21T20:18:53Z</dc:date>
    <item>
      <title>proc sql having clause pivot table</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-having-clause-pivot-table/m-p/286155#M59472</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to use Proc Sql to basically do the equivalent of a pivot table that can be done using excel. I have a dataset with a number of columns and row that I import into sas. For completeness, I have transcribed it using datalines and input below. Following my code is the result. But it does not match with the desired output. The code does not do the filter and aggregation properly. My desired output (done on excel) is at the end.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help you can provide to fix my broke code and improve any steps for efficiency is much appreciated. Thank you so much!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Code:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; have;&lt;BR /&gt;input namel $ endbal netof accst $ balsheet$ lien_position $ impairec $ lien_pp rep_cat $;&lt;BR /&gt;datalines;&lt;BR /&gt;am2 1000 1000 active he first yes 1 he1&lt;BR /&gt;am3 2000 2000 active he second no 2 he1&lt;BR /&gt;am4 1415 1100 active fm first no 1 fm1&lt;BR /&gt;am5 1600 1000 active fm second no 2 fm2&lt;BR /&gt;am5 2200 900 active he first yes 1 he2&lt;BR /&gt;am6 3000 2000 active he second yes 2 he3&lt;BR /&gt;am7 4000 2000 active fm first yes 1 fm3&lt;BR /&gt;am8 5000 3000 active he second yes 2 he2&amp;nbsp; ;&lt;BR /&gt;proc print;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;proc sql&lt;/STRONG&gt;;&lt;BR /&gt;create table want as select&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;rep_cat,&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;ifn(lien_position="first", sum(endbal),0) as Lien_1 format=Dollar30.2,&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;ifn(lien_position="second", sum(endbal),0) as Lien_2 format=Dollar30.2,&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;ifn(lien_position="first" or lien_position="second", sum(endbal),0) as Total format=Dollar30.2&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;from have&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;group by rep_cat &amp;nbsp;&amp;nbsp; /* GROUP BY clause needed otherwise the summary function and HAVING clause treat table as one group */&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;having lien_position="first" &amp;nbsp;&amp;nbsp; &amp;nbsp;/* HAVING clause is required below to summarize by participating lien_positions */&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;or lien_position="second"&amp;nbsp;&amp;nbsp; &amp;nbsp;/* SAS is deterministic so the query requires remerge summary stats back with orginial data */&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;BR /&gt;Quit;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; want_final;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; /* first reference (with obs=0) to existing table creats an empty table with required structure */&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;update want (obs=0) /* second reference updates with values and BY ensures only one record per BY value is outputed */&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; want;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;by rep_cat;&lt;BR /&gt;Run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;SAS Output&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/4165iE56CA2F3A2C20101/image-size/original?v=v2&amp;amp;px=-1" alt="Capture.PNG" title="Capture.PNG" border="0" /&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;vs Desired Output (done in pivot table on "have" data using excel)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/4166iB67961EF53AFAFB9/image-size/original?v=v2&amp;amp;px=-1" alt="Capture.PNG" title="Capture.PNG" border="0" /&gt;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jul 2016 15:10:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-having-clause-pivot-table/m-p/286155#M59472</guid>
      <dc:creator>blakezen</dc:creator>
      <dc:date>2016-07-21T15:10:14Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql having clause pivot table</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-having-clause-pivot-table/m-p/286259#M59485</link>
      <description>Don't use SQL to create the actual pivot. SQL is mainly for data manipulation / aggregation,  not advanced report layout.&lt;BR /&gt;Take a look at TABULATE and REPORT  procedures instead.</description>
      <pubDate>Thu, 21 Jul 2016 20:18:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-having-clause-pivot-table/m-p/286259#M59485</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-07-21T20:18:53Z</dc:date>
    </item>
  </channel>
</rss>

