<?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: Trouble with Group By, when row is missing in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Trouble-with-Group-By-when-row-is-missing/m-p/759545#M240038</link>
    <description>Actually, you CAN use aggregation on character fields in SQL but not if you're aggregating by that variable as well. So do you actually need to aggregate by FIELD? How do you know what value it should be?</description>
    <pubDate>Wed, 04 Aug 2021 22:30:54 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-08-04T22:30:54Z</dc:date>
    <item>
      <title>Trouble with Group By, when row is missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-with-Group-By-when-row-is-missing/m-p/759530#M240032</link>
      <description>&lt;P&gt;Good afternoon !&amp;nbsp;&lt;/P&gt;&lt;P&gt;Currently got stuck with Proc SQL code. When I use group by operation, some missing rows are spoiling the output result.&lt;/P&gt;&lt;P&gt;According to SQL logic, I should use aggregation function (e.g. sum/avg) to collapse rows, but there is no aggregation function to exclude missing row from a group.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;P&gt;Input data&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Customer&lt;/TD&gt;&lt;TD&gt;Date&lt;/TD&gt;&lt;TD&gt;Sum&lt;/TD&gt;&lt;TD&gt;Field&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ID1&lt;/TD&gt;&lt;TD&gt;C1&lt;/TD&gt;&lt;TD&gt;05.05.2021&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;AAA&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ID2&lt;/TD&gt;&lt;TD&gt;C1&lt;/TD&gt;&lt;TD&gt;05.05.2021&lt;/TD&gt;&lt;TD&gt;16&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ID3&lt;/TD&gt;&lt;TD&gt;C1&lt;/TD&gt;&lt;TD&gt;05.05.2021&lt;/TD&gt;&lt;TD&gt;21&lt;/TD&gt;&lt;TD&gt;AAA&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ID4&lt;/TD&gt;&lt;TD&gt;C2&lt;/TD&gt;&lt;TD&gt;21.05.2021&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;BBB&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I use this code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint; 
  create table output as 
    select Customer, Date, Field
      ,sum(SUM) as Sum 
      ,count(*) as count
  from work.data_test
  group by Customer, Date, Field
;quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;And get this:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Customer&lt;/TD&gt;&lt;TD&gt;Date&lt;/TD&gt;&lt;TD&gt;SUM&lt;/TD&gt;&lt;TD&gt;Field&lt;/TD&gt;&lt;TD&gt;count&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C1&lt;/TD&gt;&lt;TD&gt;05.05.2021&lt;/TD&gt;&lt;TD&gt;36&lt;/TD&gt;&lt;TD&gt;AAA&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C1&lt;/TD&gt;&lt;TD&gt;05.05.2021&lt;/TD&gt;&lt;TD&gt;16&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C2&lt;/TD&gt;&lt;TD&gt;21.05.2021&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;BBB&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Instead I need to get this:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Customer&lt;/TD&gt;&lt;TD&gt;Date&lt;/TD&gt;&lt;TD&gt;Sum&lt;/TD&gt;&lt;TD&gt;Field&lt;/TD&gt;&lt;TD&gt;count&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C1&lt;/TD&gt;&lt;TD&gt;05.05.2021&lt;/TD&gt;&lt;TD&gt;52&lt;/TD&gt;&lt;TD&gt;AAA&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C2&lt;/TD&gt;&lt;TD&gt;21.05.2021&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;BBB&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any possible solution ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Aug 2021 21:37:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-with-Group-By-when-row-is-missing/m-p/759530#M240032</guid>
      <dc:creator>Banker337</dc:creator>
      <dc:date>2021-08-04T21:37:58Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with Group By, when row is missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-with-Group-By-when-row-is-missing/m-p/759532#M240034</link>
      <description>&lt;P&gt;The missing value for FIELD is of course a valid level, just like 'AAA' or 'BBB'.&lt;/P&gt;
&lt;P&gt;If you don't want this, you have to impute the missing values.&lt;/P&gt;
&lt;P&gt;You can for example do a Last Observation Carry Forward (LOCF = Forward Filling) to impute the missing value with a non-missing level.&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Wed, 04 Aug 2021 21:42:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-with-Group-By-when-row-is-missing/m-p/759532#M240034</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-08-04T21:42:21Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with Group By, when row is missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-with-Group-By-when-row-is-missing/m-p/759545#M240038</link>
      <description>Actually, you CAN use aggregation on character fields in SQL but not if you're aggregating by that variable as well. So do you actually need to aggregate by FIELD? How do you know what value it should be?</description>
      <pubDate>Wed, 04 Aug 2021 22:30:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-with-Group-By-when-row-is-missing/m-p/759545#M240038</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-08-04T22:30:54Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with Group By, when row is missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-with-Group-By-when-row-is-missing/m-p/759746#M240153</link>
      <description>proc sql noprint; &lt;BR /&gt;  create table output as &lt;BR /&gt;    select Customer, Date, max(Field) as Field&lt;BR /&gt;      ,sum(SUM) as Sum &lt;BR /&gt;      ,count(*) as count&lt;BR /&gt;  from work.data_test&lt;BR /&gt;  group by Customer, Date&lt;BR /&gt;;quit;</description>
      <pubDate>Thu, 05 Aug 2021 15:55:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-with-Group-By-when-row-is-missing/m-p/759746#M240153</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-08-05T15:55:51Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with Group By, when row is missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-with-Group-By-when-row-is-missing/m-p/759748#M240155</link>
      <description>&lt;P&gt;What is the logic that says the missing FIELD should be replace by AAA in your example?&lt;/P&gt;
&lt;P&gt;Why not include it as FIELD='BBB' or 'CCC'?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Aug 2021 16:03:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-with-Group-By-when-row-is-missing/m-p/759748#M240155</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-08-05T16:03:34Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with Group By, when row is missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-with-Group-By-when-row-is-missing/m-p/760078#M240302</link>
      <description>Same customer and date</description>
      <pubDate>Fri, 06 Aug 2021 19:47:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-with-Group-By-when-row-is-missing/m-p/760078#M240302</guid>
      <dc:creator>Banker337</dc:creator>
      <dc:date>2021-08-06T19:47:07Z</dc:date>
    </item>
  </channel>
</rss>

