<?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 group by is not working in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-group-by-is-not-working/m-p/916132#M360908</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, I need all the variables in my output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;vnreddy&lt;/P&gt;</description>
    <pubDate>Wed, 14 Feb 2024 18:02:25 GMT</pubDate>
    <dc:creator>vnreddy</dc:creator>
    <dc:date>2024-02-14T18:02:25Z</dc:date>
    <item>
      <title>Proc SQL group by is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-group-by-is-not-working/m-p/916123#M360904</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Can someone help me with below issue.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When i use sum and group by in proc sql with or without distinct, i am not getting unique records.&lt;/P&gt;
&lt;P&gt;As you can see from below output image, i should only get 3 rows in my output. How can i get rid of 2nd or 3rd row from my output.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input code $1-3 tcode $5-8 order_date :date9. invoice_date :date9. invoice_code $30-33 customer $35-39 Cust_code $41-44
P_code $46-50 P_name $52-60 H_code $62-65 T_code $67-72 Plant $74-77 Cat_code $79-81 Cat_desc $83 Item_code $85-87
Item_desc $89-98 Distance 100-102 Qty 104-107 HPrice 109-112 Rev 114-119;
format order_date date9. invoice_date date9.;
;
datalines;
101 1019 21NOV2023 25NOV2023 2626 B11AC B100 52486 New_B11AC EAST GNA1UC 100A 200 H 380 HPayDry    9.3    0 0.00      0
101 1019 21NOV2023 25NOV2023 2626 B11AC B100 52486 New_B11AC EAST GNA1UC 100A 200 H 370 HChargeDry 9.3 2.14 0.00  94.25
101 1019 21NOV2023 25NOV2023 2626 B11AC B100 52486 New_B11AC EAST GNA1UC 100A 200 H 370 HChargeDry 9.3 2.14 0.00 113.36
101 1019 21NOV2023 25NOV2023 2626 B11AC B100 52486 New_B11AC EAST GNA1UC 100A 114 S 520 0/2_catP3  9.3 2.14 4.35 169.38
; 

proc sql;
create table want (drop=Qty Rev) as
select*,
sum(Qty) as P_Qty,
sum(Rev) as Revenue
from have
group by cat_code, Cat_desc, item_code,item_desc
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;current output what i am getting&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="vnreddy_1-1707932613603.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/93722i471C7C1A63FD6A97/image-size/medium?v=v2&amp;amp;px=400" role="button" title="vnreddy_1-1707932613603.png" alt="vnreddy_1-1707932613603.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Expected output when i use sum and group by i should get only 3 rows as per the requirement. Row 2 &amp;amp; 3 are same, how should i get rid of one row.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sum issue : for sample purpose i have manually shown few records here, when i use the sum function on a large dataset i end getting a sum on complete data rather then group by. Below image shows an issue with sum on revenue and qty when i use a sum function and group by in proc sql. In reality it should only give the sum based on grouping. Below sum is not right, it won't be this high number.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="vnreddy_0-1707933213264.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/93723iE749352E24E87EC9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="vnreddy_0-1707933213264.png" alt="vnreddy_0-1707933213264.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;vnreddy&lt;/P&gt;</description>
      <pubDate>Wed, 14 Feb 2024 17:55:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-group-by-is-not-working/m-p/916123#M360904</guid>
      <dc:creator>vnreddy</dc:creator>
      <dc:date>2024-02-14T17:55:54Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL group by is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-group-by-is-not-working/m-p/916125#M360905</link>
      <description>&lt;P&gt;You asked SAS to return ALL of the variables.&amp;nbsp; So that will require ALL of the observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want just the summary results and the group by variables then you need to only ask for those variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table want as
  select cat_code, Cat_desc, item_code, item_desc
       , sum(Qty) as P_Qty
       , sum(Rev) as Revenue
  from have
  group by cat_code, Cat_desc, item_code,item_desc
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PS Don't hide those continuation commas at the ends of the lines. It is much harder to scan for them there since the right side of the lines is jagged.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Feb 2024 17:56:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-group-by-is-not-working/m-p/916125#M360905</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-02-14T17:56:41Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL group by is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-group-by-is-not-working/m-p/916132#M360908</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, I need all the variables in my output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;vnreddy&lt;/P&gt;</description>
      <pubDate>Wed, 14 Feb 2024 18:02:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-group-by-is-not-working/m-p/916132#M360908</guid>
      <dc:creator>vnreddy</dc:creator>
      <dc:date>2024-02-14T18:02:25Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL group by is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-group-by-is-not-working/m-p/916133#M360909</link>
      <description>&lt;P&gt;Then SAS did what you wanted.&lt;/P&gt;
&lt;P&gt;Try it with SASHELP.CLASS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select *,mean(age) as Gender_Mean
from sashelp.class
group by sex
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As you can see you get ALL of the observations and the new variable has the same value for all observations that share the same values of the GROUP BY variables.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Feb 2024 18:05:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-group-by-is-not-working/m-p/916133#M360909</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-02-14T18:05:07Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL group by is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-group-by-is-not-working/m-p/916135#M360910</link>
      <description>Then you need to summarize/add some logic to have them handled.</description>
      <pubDate>Wed, 14 Feb 2024 18:15:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-group-by-is-not-working/m-p/916135#M360910</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2024-02-14T18:15:55Z</dc:date>
    </item>
  </channel>
</rss>

