<?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: Summing problem in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Summing-problem/m-p/73110#M15742</link>
    <description>&amp;gt;if a customer purchase at company A, how much ... they spend there.&lt;BR /&gt;
&lt;BR /&gt;
This is not what you are showing: &lt;BR /&gt;
A 4+6+5(R1+R5+R10) &lt;BR /&gt;
is for all customers.&lt;BR /&gt;
&lt;BR /&gt;
In any case, here is how to obtain the same numbers you are showing.&lt;BR /&gt;
Efficient is not how I would describe it, but it works.&lt;BR /&gt;
Loading the table in memory speeds things up when making the successive passes (You might want to drop unnecessary columns if you have many).&lt;BR /&gt;
If the amount of memory you have is too small, the code will still work, but an index on CID and maybe another on COMPANY will help speed.&lt;BR /&gt;
[pre]&lt;BR /&gt;
data T;                                              * sample data;               &lt;BR /&gt;
 input R CID COMPANY $1. AMT ;&lt;BR /&gt;
 cards;&lt;BR /&gt;
1 111 A 4&lt;BR /&gt;
2 111 D 2&lt;BR /&gt;
3 111 G 3&lt;BR /&gt;
4 111 X 4&lt;BR /&gt;
5 112 A 6&lt;BR /&gt;
6 112 D 2&lt;BR /&gt;
7 112 G 8&lt;BR /&gt;
8 113 G 4&lt;BR /&gt;
9 113 X 2&lt;BR /&gt;
10 114 A 5&lt;BR /&gt;
11 115 X 5&lt;BR /&gt;
run;&lt;BR /&gt;
data FINAL;                                          * prepare output table;&lt;BR /&gt;
  delete;&lt;BR /&gt;
run;&lt;BR /&gt;
sasfile T load;                                      * load data in memory;&lt;BR /&gt;
proc sort data=T(keep=COMPANY) out=COLIST nodupkey;  * get list of companies;&lt;BR /&gt;
  by COMPANY;&lt;BR /&gt;
run;&lt;BR /&gt;
%macro t;&lt;BR /&gt;
  %let dsid=%sysfunc(open(COLIST));&lt;BR /&gt;
  %let rc=%sysfunc(fetch(&amp;amp;dsid));&lt;BR /&gt;
  %do %while(&amp;amp;rc=0);                                 * loop thru companies;&lt;BR /&gt;
    %let coname=%sysfunc(getvarc(&amp;amp;dsid,1));&lt;BR /&gt;
    proc sql;                                        * derive figures for 1 company;   &lt;BR /&gt;
      create table CO_&amp;amp;coname as &lt;BR /&gt;
      select sum(AMT) as &amp;amp;coname ,COMPANY &lt;BR /&gt;
      from T&lt;BR /&gt;
      where CID in (select unique CID from T where COMPANY="&amp;amp;coname")&lt;BR /&gt;
      group by COMPANY;&lt;BR /&gt;
    quit;&lt;BR /&gt;
    proc transpose data=CO_&amp;amp;coname out=CO_&amp;amp;coname;   * put figures in 1 row;&lt;BR /&gt;
      id COMPANY ; &lt;BR /&gt;
    run;&lt;BR /&gt;
    data FINAL;                                      * add to output table;&lt;BR /&gt;
      set FINAL CO_&amp;amp;coname;&lt;BR /&gt;
    run;&lt;BR /&gt;
    %let rc=%sysfunc(fetch(&amp;amp;dsid));&lt;BR /&gt;
  %end;&lt;BR /&gt;
  %let rc=%sysfunc(close(&amp;amp;dsid));&lt;BR /&gt;
%mend;%t&lt;BR /&gt;
sasfile T close;                                     * unload data from memory;</description>
    <pubDate>Thu, 24 Sep 2009 22:04:00 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2009-09-24T22:04:00Z</dc:date>
    <item>
      <title>Summing problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-problem/m-p/73109#M15741</link>
      <description>Hi I been stuck on this problem for a while now and hoping someone could&lt;BR /&gt;
shed a light how to program this in an efficient manner my data set is&lt;BR /&gt;
approx 41 million records.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
HAVE:&lt;BR /&gt;
&lt;BR /&gt;
Row(R) CID COMPANY Amt&lt;BR /&gt;
1          111 A                4&lt;BR /&gt;
2          111 D                2&lt;BR /&gt;
3          111 G               3&lt;BR /&gt;
4          111 X                4&lt;BR /&gt;
5          112 A               6&lt;BR /&gt;
6          112 D               2&lt;BR /&gt;
7          112 G               8&lt;BR /&gt;
8          113 G               4&lt;BR /&gt;
9          113 X               2&lt;BR /&gt;
10        114 A               5&lt;BR /&gt;
11        115 X               5&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
WANT:&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
     A   D    G    X&lt;BR /&gt;
A  15   4    11   4&lt;BR /&gt;
D  10   4    11   4&lt;BR /&gt;
G  10   4    15   6&lt;BR /&gt;
X    4   2     7   11&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
With the sum is breakdown as follow&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
    A                            D                   G                              X&lt;BR /&gt;
A 4+6+5(R1+R5+R10)  2+2(R2+R6)   3+8 (R3+R7)              4(R4)&lt;BR /&gt;
D 4+6(R1+R5)             2+2(R2+R6)   3+8 (R3+R7)              4(R4)&lt;BR /&gt;
G 4+6(R1+R5)             2+2(R2+R6)   3+8+4(R3+R7+R8)     4+2(R4+R9)&lt;BR /&gt;
X 4(R1)                       4(R2)             3+4(R3+R8)               4+2+5(R4+R9+R11)&lt;BR /&gt;
&lt;BR /&gt;
So essentially what I need is if a customer purchase at company A, how&lt;BR /&gt;
much is the total amount they spend there.&lt;BR /&gt;
&lt;BR /&gt;
And we also want to know how much they are spending elsewhere, so for&lt;BR /&gt;
example for the first row, all customers spending at company A how much&lt;BR /&gt;
they are spending at company D, G &amp;amp; X.&lt;BR /&gt;
&lt;BR /&gt;
Many Thanks in advance.</description>
      <pubDate>Thu, 24 Sep 2009 13:16:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-problem/m-p/73109#M15741</guid>
      <dc:creator>psquares</dc:creator>
      <dc:date>2009-09-24T13:16:42Z</dc:date>
    </item>
    <item>
      <title>Re: Summing problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-problem/m-p/73110#M15742</link>
      <description>&amp;gt;if a customer purchase at company A, how much ... they spend there.&lt;BR /&gt;
&lt;BR /&gt;
This is not what you are showing: &lt;BR /&gt;
A 4+6+5(R1+R5+R10) &lt;BR /&gt;
is for all customers.&lt;BR /&gt;
&lt;BR /&gt;
In any case, here is how to obtain the same numbers you are showing.&lt;BR /&gt;
Efficient is not how I would describe it, but it works.&lt;BR /&gt;
Loading the table in memory speeds things up when making the successive passes (You might want to drop unnecessary columns if you have many).&lt;BR /&gt;
If the amount of memory you have is too small, the code will still work, but an index on CID and maybe another on COMPANY will help speed.&lt;BR /&gt;
[pre]&lt;BR /&gt;
data T;                                              * sample data;               &lt;BR /&gt;
 input R CID COMPANY $1. AMT ;&lt;BR /&gt;
 cards;&lt;BR /&gt;
1 111 A 4&lt;BR /&gt;
2 111 D 2&lt;BR /&gt;
3 111 G 3&lt;BR /&gt;
4 111 X 4&lt;BR /&gt;
5 112 A 6&lt;BR /&gt;
6 112 D 2&lt;BR /&gt;
7 112 G 8&lt;BR /&gt;
8 113 G 4&lt;BR /&gt;
9 113 X 2&lt;BR /&gt;
10 114 A 5&lt;BR /&gt;
11 115 X 5&lt;BR /&gt;
run;&lt;BR /&gt;
data FINAL;                                          * prepare output table;&lt;BR /&gt;
  delete;&lt;BR /&gt;
run;&lt;BR /&gt;
sasfile T load;                                      * load data in memory;&lt;BR /&gt;
proc sort data=T(keep=COMPANY) out=COLIST nodupkey;  * get list of companies;&lt;BR /&gt;
  by COMPANY;&lt;BR /&gt;
run;&lt;BR /&gt;
%macro t;&lt;BR /&gt;
  %let dsid=%sysfunc(open(COLIST));&lt;BR /&gt;
  %let rc=%sysfunc(fetch(&amp;amp;dsid));&lt;BR /&gt;
  %do %while(&amp;amp;rc=0);                                 * loop thru companies;&lt;BR /&gt;
    %let coname=%sysfunc(getvarc(&amp;amp;dsid,1));&lt;BR /&gt;
    proc sql;                                        * derive figures for 1 company;   &lt;BR /&gt;
      create table CO_&amp;amp;coname as &lt;BR /&gt;
      select sum(AMT) as &amp;amp;coname ,COMPANY &lt;BR /&gt;
      from T&lt;BR /&gt;
      where CID in (select unique CID from T where COMPANY="&amp;amp;coname")&lt;BR /&gt;
      group by COMPANY;&lt;BR /&gt;
    quit;&lt;BR /&gt;
    proc transpose data=CO_&amp;amp;coname out=CO_&amp;amp;coname;   * put figures in 1 row;&lt;BR /&gt;
      id COMPANY ; &lt;BR /&gt;
    run;&lt;BR /&gt;
    data FINAL;                                      * add to output table;&lt;BR /&gt;
      set FINAL CO_&amp;amp;coname;&lt;BR /&gt;
    run;&lt;BR /&gt;
    %let rc=%sysfunc(fetch(&amp;amp;dsid));&lt;BR /&gt;
  %end;&lt;BR /&gt;
  %let rc=%sysfunc(close(&amp;amp;dsid));&lt;BR /&gt;
%mend;%t&lt;BR /&gt;
sasfile T close;                                     * unload data from memory;</description>
      <pubDate>Thu, 24 Sep 2009 22:04:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-problem/m-p/73110#M15742</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2009-09-24T22:04:00Z</dc:date>
    </item>
    <item>
      <title>Re: Summing problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-problem/m-p/73111#M15743</link>
      <description>Thank you Chris

Message was edited by: psquares</description>
      <pubDate>Fri, 25 Sep 2009 02:59:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-problem/m-p/73111#M15743</guid>
      <dc:creator>psquares</dc:creator>
      <dc:date>2009-09-25T02:59:45Z</dc:date>
    </item>
  </channel>
</rss>

