<?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: How to perform SUM for Distinct values in the data. in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/701000#M37691</link>
    <description>&lt;P&gt;This creates your expected result:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input day $ record_id $ calls;
datalines;
Sun abc1  10
Sun abc1  10
Sun abc1  10
Sun abc2  5
Sun abc2  5
Sun abc2  5
Sun abc3  12
Sun abc4  1
Sun abc4  1
Sun abc4  1
;

proc sql;
create table want as
  select
    day,
    count(distinct record_id) as total_records,
    sum(distinct calls) as total_calls
  from have
  group by day
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 23 Nov 2020 18:36:20 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-11-23T18:36:20Z</dc:date>
    <item>
      <title>How to perform SUM for Distinct values in the data.</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/700999#M37690</link>
      <description>&lt;P&gt;Hi SAS Experts,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a data set as shown below:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="324"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="108"&gt;Day&lt;/TD&gt;
&lt;TD width="108"&gt;Record ID&lt;/TD&gt;
&lt;TD width="108"&gt;Calls&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&g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&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="324"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="108"&gt;Day&lt;/TD&gt;
&lt;TD width="108"&gt;Total Records on Sunday&lt;/TD&gt;
&lt;TD width="108"&gt;Total calls&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Sun&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;28&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;Total Calls is sum of distinct values for the calls which is 10+5+12+1 = 28.&lt;/P&gt;
&lt;P&gt;I am trying to perform sum for the distinct values in a summary format.&amp;nbsp;I know you can sort the data by record Id first and then perform sum, but I do have other variables that need from the data set, which restricts to follow this process.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In advance, thank you for your time.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Nov 2020 18:30:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/700999#M37690</guid>
      <dc:creator>PrudhviB</dc:creator>
      <dc:date>2020-11-23T18:30:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform SUM for Distinct values in the data.</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/701000#M37691</link>
      <description>&lt;P&gt;This creates your expected result:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input day $ record_id $ calls;
datalines;
Sun abc1  10
Sun abc1  10
Sun abc1  10
Sun abc2  5
Sun abc2  5
Sun abc2  5
Sun abc3  12
Sun abc4  1
Sun abc4  1
Sun abc4  1
;

proc sql;
create table want as
  select
    day,
    count(distinct record_id) as total_records,
    sum(distinct calls) as total_calls
  from have
  group by day
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Nov 2020 18:36:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/701000#M37691</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-23T18:36:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform SUM for Distinct values in the data.</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/701001#M37692</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input Day $	Record_ID $	Calls;
cards;
Sun	abc1	10
Sun	abc1	10
Sun	abc1	10
Sun	abc2	5
Sun	abc2	5
Sun	abc2	5
Sun	abc3	12
Sun	abc4	1
Sun	abc4	1
Sun	abc4	1
;

data want;
 set have;
 by day Record_ID notsorted;
 if first.day then call missing(Total_Records_on_Sunday,Total_calls);
 if first.Record_ID then do;
   Total_Records_on_Sunday+1;
   Total_calls+calls;
 end;
 if last.day;
 keep day Total_Records_on_Sunday Total_calls;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Nov 2020 18:50:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/701001#M37692</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-11-23T18:50:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform SUM for Distinct values in the data.</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/701014#M37694</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp; this solution returning same value if i have more records with same calls for example for the below code total calls are same even with the new records.&amp;nbsp;data have;&lt;BR /&gt;input day $ record_id $ calls;&lt;BR /&gt;datalines;&lt;BR /&gt;Sun abc1 10&lt;BR /&gt;Sun abc1 10&lt;BR /&gt;Sun abc1 10&lt;BR /&gt;Sun abc2 5&lt;BR /&gt;Sun abc2 5&lt;BR /&gt;Sun abc2 5&lt;BR /&gt;Sun abc3 12&lt;BR /&gt;Sun abc4 1&lt;BR /&gt;Sun abc4 1&lt;BR /&gt;Sun abc4 1&lt;BR /&gt;Sun abc1 10&lt;BR /&gt;Sun abc1 10&lt;BR /&gt;Sun abc6 10&lt;BR /&gt;Sun abc6 10&lt;BR /&gt;Sun abc7 10&lt;BR /&gt;Sun abc8 10&lt;BR /&gt;;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Nov 2020 19:10:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/701014#M37694</guid>
      <dc:creator>PrudhviB</dc:creator>
      <dc:date>2020-11-23T19:10:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform SUM for Distinct values in the data.</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/701015#M37695</link>
      <description>this seems to work, if my day is actual date? like 10/30/2020 will this code still work ? &lt;BR /&gt;&lt;BR /&gt;I am coding on EG environment, is there a better way i can do it on SAS EG ?</description>
      <pubDate>Mon, 23 Nov 2020 19:14:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/701015#M37695</guid>
      <dc:creator>PrudhviB</dc:creator>
      <dc:date>2020-11-23T19:14:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform SUM for Distinct values in the data.</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/701017#M37696</link>
      <description>&lt;P&gt;&lt;EM&gt;"this seems to work, if my day is actual date? like 10/30/2020 will this code still work ?&amp;nbsp;&lt;/EM&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;EM&gt;I am coding on EG environment, is there a better way i can do it on SAS EG ?"&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/279632"&gt;@PrudhviB&lt;/a&gt;&amp;nbsp; Whether your day is actual date or any other value, it shouldn't matter as long as the values can form a by group(or group by). For example, if you have 10 transactions on a given date, you would expect to have 10 records for the same date. Therefore, it should still work fine.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;In EG, if you are using &lt;EM&gt;&lt;STRONG&gt;query builder&lt;/STRONG&gt;&lt;/EM&gt;, your best bet would be is to use a SQL solution. However SAS programming is the same regardless of the SAS client you are using, be it PC SAS, SAS EG, Studio etc.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I would suggest to please test the solution on your data and see if it really works. If it doesn't, it's garbage. And if it does, use it. Sound good?&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Nov 2020 19:22:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/701017#M37696</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-11-23T19:22:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform SUM for Distinct values in the data.</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/701024#M37697</link>
      <description>&lt;P&gt;Please supply example data that reflects your&amp;nbsp;&lt;EM&gt;real&lt;/EM&gt; situation, and show the results you want out of that particular data.&lt;/P&gt;
&lt;P&gt;My code solves the question as stated in the original post, it's not the magic "solve the moving goalpost problem" code.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Nov 2020 19:36:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/701024#M37697</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-23T19:36:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform SUM for Distinct values in the data.</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/701037#M37698</link>
      <description>Thank you for your response. My apologies, I should have shared the total dataset.</description>
      <pubDate>Mon, 23 Nov 2020 20:28:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/701037#M37698</guid>
      <dc:creator>PrudhviB</dc:creator>
      <dc:date>2020-11-23T20:28:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform SUM for Distinct values in the data.</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/701038#M37699</link>
      <description>Thank you, it did work for my data.</description>
      <pubDate>Mon, 23 Nov 2020 20:29:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/701038#M37699</guid>
      <dc:creator>PrudhviB</dc:creator>
      <dc:date>2020-11-23T20:29:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform SUM for Distinct values in the data.</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/701165#M37703</link>
      <description>&lt;P&gt;My 2 cents&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Day $	Record_ID $	Calls;
cards;
Sun abc1 10
Sun abc1 10
Sun abc1 10
Sun abc2 5
Sun abc2 5
Sun abc2 5
Sun abc3 12
Sun abc4 1
Sun abc4 1
Sun abc4 1
Mon abc1 10
Mon abc1 10
Mon abc1 10
Mon abc2 5
Mon abc2 5
Mon abc2 5
Mon abc3 12
Mon abc4 1
Mon abc4 1
Mon abc4 1
Tue abc1 10
Tue abc1 10
Tue abc1 10
Tue abc2 5
Tue abc2 5
Tue abc2 5
Tue abc3 12
Tue abc4 1
Tue abc4 1
Tue abc4 1
;

data _null_;
   dcl hash h (ordered: "A");
   h.definekey ("Day");
   h.definedata ("Day", "rec", "tot");
   h.definedone ();

   dcl hash u ();
   u.definekey ("Day", "Calls");
   u.definedone ();

   do until (z);
      set have end = z;
      if h.find() ne 0 then call missing (rec, tot);
      if u.check() ne 0 then do;
         tot = sum (tot, Calls);
         rec = sum (rec, 1);
         u.add();
      end;
      h.replace();
   end;

   h.output (dataset: "want");
   stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 24 Nov 2020 08:19:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-perform-SUM-for-Distinct-values-in-the-data/m-p/701165#M37703</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-11-24T08:19:11Z</dc:date>
    </item>
  </channel>
</rss>

