<?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: Group and sum observation in SAS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Group-and-sum-observation-in-SAS/m-p/359629#M84577</link>
    <description>Thanks RW9. This was quick. If car 1 and car 2 sales had multiple observations how do I group only the car 3 sales.&lt;BR /&gt;&lt;BR /&gt;data have;&lt;BR /&gt;input Car sales;&lt;BR /&gt;datalines;&lt;BR /&gt;1 3&lt;BR /&gt;1 4&lt;BR /&gt;2 4&lt;BR /&gt;2 4&lt;BR /&gt;3 5&lt;BR /&gt;3 5&lt;BR /&gt;3 5&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;The output should be:&lt;BR /&gt;&lt;BR /&gt;Car sales&lt;BR /&gt;1 3&lt;BR /&gt;1 4&lt;BR /&gt;2 4&lt;BR /&gt;2 4&lt;BR /&gt;3 15&lt;BR /&gt;&lt;BR /&gt;Thank you.</description>
    <pubDate>Thu, 18 May 2017 12:23:33 GMT</pubDate>
    <dc:creator>Tomcaty</dc:creator>
    <dc:date>2017-05-18T12:23:33Z</dc:date>
    <item>
      <title>Group and sum observation in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-and-sum-observation-in-SAS/m-p/359618#M84572</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am new to SAS communities. I have a dataset that needs to be grouped as below. How do I group and sum all the observations in that group using SQL statement. Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;input Car sales;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1 3&lt;/P&gt;&lt;P&gt;2 4&lt;/P&gt;&lt;P&gt;3 5&lt;/P&gt;&lt;P&gt;3 5&lt;/P&gt;&lt;P&gt;3 5&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output I needed was:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Car sales&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; 15&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wanted to group and sum up the sales with 3 cars. How do I do this using PRCO SQL.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Much appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 May 2017 12:02:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-and-sum-observation-in-SAS/m-p/359618#M84572</guid>
      <dc:creator>Tomcaty</dc:creator>
      <dc:date>2017-05-18T12:02:29Z</dc:date>
    </item>
    <item>
      <title>Re: Group and sum observation in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-and-sum-observation-in-SAS/m-p/359619#M84573</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table WANT as
  select CAR,
         sum(SALES) as SALES
  from   HAVE
  group by CAR;
quit;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 May 2017 12:05:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-and-sum-observation-in-SAS/m-p/359619#M84573</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-05-18T12:05:10Z</dc:date>
    </item>
    <item>
      <title>Re: Group and sum observation in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-and-sum-observation-in-SAS/m-p/359622#M84575</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Car sales;
datalines;
1 3
2 4
3 5
3 5
3 5
;

data want(drop = Sales);
	set have;
	by Car;
	if first.Car then Sum_Sales = 0;
	Sum_Sales + Sales;
	if last.Car then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 May 2017 12:12:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-and-sum-observation-in-SAS/m-p/359622#M84575</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-05-18T12:12:15Z</dc:date>
    </item>
    <item>
      <title>Re: Group and sum observation in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-and-sum-observation-in-SAS/m-p/359629#M84577</link>
      <description>Thanks RW9. This was quick. If car 1 and car 2 sales had multiple observations how do I group only the car 3 sales.&lt;BR /&gt;&lt;BR /&gt;data have;&lt;BR /&gt;input Car sales;&lt;BR /&gt;datalines;&lt;BR /&gt;1 3&lt;BR /&gt;1 4&lt;BR /&gt;2 4&lt;BR /&gt;2 4&lt;BR /&gt;3 5&lt;BR /&gt;3 5&lt;BR /&gt;3 5&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;The output should be:&lt;BR /&gt;&lt;BR /&gt;Car sales&lt;BR /&gt;1 3&lt;BR /&gt;1 4&lt;BR /&gt;2 4&lt;BR /&gt;2 4&lt;BR /&gt;3 15&lt;BR /&gt;&lt;BR /&gt;Thank you.</description>
      <pubDate>Thu, 18 May 2017 12:23:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-and-sum-observation-in-SAS/m-p/359629#M84577</guid>
      <dc:creator>Tomcaty</dc:creator>
      <dc:date>2017-05-18T12:23:33Z</dc:date>
    </item>
    <item>
      <title>Re: Group and sum observation in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-and-sum-observation-in-SAS/m-p/359630#M84578</link>
      <description>&lt;P&gt;proc sql;&lt;BR /&gt;&amp;nbsp; create table WANT as&lt;BR /&gt;&amp;nbsp; select CAR,&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sum(SALES) as SALES&lt;BR /&gt;&amp;nbsp; from&amp;nbsp;&amp;nbsp; HAVE&lt;/P&gt;&lt;P&gt;where car=3&amp;nbsp;&amp;nbsp; /*add this where clause*/&lt;BR /&gt;&amp;nbsp; group by CAR;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Thu, 18 May 2017 12:25:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-and-sum-observation-in-SAS/m-p/359630#M84578</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-05-18T12:25:37Z</dc:date>
    </item>
    <item>
      <title>Re: Group and sum observation in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-and-sum-observation-in-SAS/m-p/359631#M84579</link>
      <description>&lt;P&gt;Well, thats a quesiton back to you, how would I, as a computer, know that from this data:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1 3&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1 4&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2 4&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2 4&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;3 5&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;3 5&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;3 5&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I should only group 3? &amp;nbsp;No way of telling is there, so you either need to a) add the information to the data so you can tell the computer what to group, or b) do two separate blocks and join them together:&lt;BR /&gt;For b):&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table WANT as
  select *
  from   HAVE
  where CAR ne 3
  union all
  select CAR,
           sum(SALES) as SALES
  from   (select * from HAVE where CAR=3)
  group by CAR;
quit;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;As you can see, more code due to the fact the data does not tell the story, so not good.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 18 May 2017 12:27:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-and-sum-observation-in-SAS/m-p/359631#M84579</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-05-18T12:27:14Z</dc:date>
    </item>
    <item>
      <title>Re: Group and sum observation in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-and-sum-observation-in-SAS/m-p/359692#M84594</link>
      <description>&lt;P&gt;RW9, I apologize for being vague. Heres the actual dataset I have.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input sales frequency percent;
datalines;
1 240 24.0
2 320 32.0
3 120 12.0
4  80  8.0
5  68  6.8
6  42  4.2
7 130 13.0
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I wanted to bucket all sales greater than or equal to 4. My output should look something like this:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Sales&lt;/TD&gt;&lt;TD&gt;frequency&lt;/TD&gt;&lt;TD&gt;percent&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;240&lt;/TD&gt;&lt;TD&gt;24.0%&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;320&lt;/TD&gt;&lt;TD&gt;32.0%&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;120&lt;/TD&gt;&lt;TD&gt;12.0%&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&amp;gt;=4&lt;/TD&gt;&lt;TD&gt;320&lt;/TD&gt;&lt;TD&gt;32%&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Total&lt;/TD&gt;&lt;TD&gt;1000&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for all your help!&lt;/P&gt;</description>
      <pubDate>Thu, 18 May 2017 15:48:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-and-sum-observation-in-SAS/m-p/359692#M84594</guid>
      <dc:creator>Tomcaty</dc:creator>
      <dc:date>2017-05-18T15:48:59Z</dc:date>
    </item>
    <item>
      <title>Re: Group and sum observation in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-and-sum-observation-in-SAS/m-p/359697#M84595</link>
      <description>&lt;P&gt;Right, so the key here is to create the variable to group by, as we are only using SQL here, we use a subquery which conditionally creates a character variable of sales (as we have non-numeric in it). &amp;nbsp;Then its simply grouping by that:&lt;/P&gt;
&lt;PRE&gt;data have;
  input sales frequency percent;
datalines;
1 240 24.0
2 320 32.0
3 120 12.0
4  80  8.0
5  68  6.8
6  42  4.2
7 130 13.0
;
run;

proc sql;
  create table WANT as
  select  SALES,
          sum(FREQUENCY) as FREQUENCY,
          sum(PERCENT) as PERCENT
  from    (select case when SALES &amp;gt;= 4 then "&amp;gt;= 4" else strip(put(SALES,best.)) end as SALES,
                  FREQUENCY,
                  PERCENT
           from   HAVE)
  group by SALES;
quit;
&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 May 2017 15:56:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-and-sum-observation-in-SAS/m-p/359697#M84595</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-05-18T15:56:39Z</dc:date>
    </item>
    <item>
      <title>Re: Group and sum observation in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-and-sum-observation-in-SAS/m-p/359705#M84598</link>
      <description>Wonderful! this worked. You are really an expert. Many thanks.</description>
      <pubDate>Thu, 18 May 2017 16:02:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-and-sum-observation-in-SAS/m-p/359705#M84598</guid>
      <dc:creator>Tomcaty</dc:creator>
      <dc:date>2017-05-18T16:02:59Z</dc:date>
    </item>
  </channel>
</rss>

