<?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 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-values-w-SQL/m-p/250120#M268714</link>
    <description>This not ideal task for SQL, much simpler using a data step.&lt;BR /&gt;But I don't think that storing cumulative values is a great idea. More of a task for a report. But that's perhaps the use of this?</description>
    <pubDate>Mon, 15 Feb 2016 16:15:45 GMT</pubDate>
    <dc:creator>LinusH</dc:creator>
    <dc:date>2016-02-15T16:15:45Z</dc:date>
    <item>
      <title>Calculate cumulative values w SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-values-w-SQL/m-p/250113#M268713</link>
      <description>&lt;P&gt;store &amp;nbsp;sales&lt;/P&gt;
&lt;P&gt;1 100&lt;/P&gt;
&lt;P&gt;1 110&lt;/P&gt;
&lt;P&gt;1 200&lt;/P&gt;
&lt;P&gt;2 200&lt;/P&gt;
&lt;P&gt;2 300&lt;/P&gt;
&lt;P&gt;2 100&lt;/P&gt;
&lt;P&gt;i want cumulative sales of store with proc sql&amp;nbsp;&lt;/P&gt;
&lt;P&gt;store sales&lt;/P&gt;
&lt;P&gt;1 100&lt;/P&gt;
&lt;P&gt;1 210&lt;/P&gt;
&lt;P&gt;1 410&lt;/P&gt;
&lt;P&gt;2 200&lt;/P&gt;
&lt;P&gt;2 500&lt;/P&gt;
&lt;P&gt;2 600&lt;/P&gt;
&lt;P&gt;how cant i get this result using proc sql?&lt;/P&gt;</description>
      <pubDate>Mon, 15 Feb 2016 17:04:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-values-w-SQL/m-p/250113#M268713</guid>
      <dc:creator>pawandh</dc:creator>
      <dc:date>2016-02-15T17:04:28Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-values-w-SQL/m-p/250120#M268714</link>
      <description>This not ideal task for SQL, much simpler using a data step.&lt;BR /&gt;But I don't think that storing cumulative values is a great idea. More of a task for a report. But that's perhaps the use of this?</description>
      <pubDate>Mon, 15 Feb 2016 16:15:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-values-w-SQL/m-p/250120#M268714</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-02-15T16:15:45Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-values-w-SQL/m-p/250121#M268715</link>
      <description>&lt;P&gt;Generally speaking, its better to create a new variable with calculations rather than overriding the original data. &amp;nbsp;Also there is no order givein in that data you provide, i.e. how do you know that 100 comes before 110 and 200? &amp;nbsp; The calculation changes depending on observation order in the data - which is not a good idea as the calculation performed again may not get the same response, say next time you look at the data it looks like:&lt;/P&gt;
&lt;P&gt;1 100&lt;/P&gt;
&lt;P&gt;1 105&lt;/P&gt;
&lt;P&gt;1 110&lt;/P&gt;
&lt;P&gt;1 200&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then your cumulative does not match, observation position is not a good grouping.&lt;/P&gt;
&lt;P&gt;As for your calculation, as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13674"&gt;@LinusH﻿&lt;/a&gt;&amp;nbsp;has said, your better off in a datastep, why do you not want to use that?&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  retain cum_sum;
  by store;
  if first.store then cum_sum=sales;
  else cum_sum=sum(cum_sales,sales);
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Feb 2016 16:22:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-values-w-SQL/m-p/250121#M268715</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-02-15T16:22:59Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-values-w-SQL/m-p/250122#M268716</link>
      <description>&lt;P&gt;I agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13674"&gt;@LinusH﻿&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;&amp;nbsp;that the data step is much more appropriate for this task, but if you really want to use PROC SQL, try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table temp as
select *, monotonic() as n
from have
order by store, n;

create table want as
select a.store, sum(b.sales) as sales
from temp a join temp b
on a.store=b.store &amp;amp; b.n&amp;lt;=a.n
group by a.store, a.n
order by store, a.n;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Edit: Given your strong preference for PROC SQL, you probably have a "database mindset." In this case, however, you will have a record identifier in your &lt;EM&gt;real&lt;/EM&gt; data. So, the creation of table TEMP (using the undocumented MONOTONIC() function, which I personally would avoid for production purposes) would be unnecessary, because something like variable N would exist already in your HAVE dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit 2: Added the ORDER BY clause to the first CREATE statement, so as to make it less probable that PROC SQL permutes observations.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Feb 2016 16:39:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-values-w-SQL/m-p/250122#M268716</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-02-15T16:39:33Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate cumulative values w SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-values-w-SQL/m-p/250125#M268717</link>
      <description>&lt;P&gt;I'm changing the subject to better describe the question.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Feb 2016 17:05:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-values-w-SQL/m-p/250125#M268717</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-02-15T17:05:04Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-values-w-SQL/m-p/250379#M268718</link>
      <description>&lt;P&gt;if instead of sum(b.sales), i took (a.sales) then y my output is different.And "a.n&amp;lt;=b.n" how is this working.&lt;/P&gt;&lt;P&gt;Please explain how the query is working&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;select a.store,sum(a.sales)as sale from test a,test b where a.store=b.store and a.n&amp;lt;=b.n group by b.store,b.n;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;store sale&lt;/P&gt;&lt;P&gt;1 100&lt;/P&gt;&lt;P&gt;1 210&lt;/P&gt;&lt;P&gt;1 210&lt;/P&gt;&lt;P&gt;1 410&lt;/P&gt;&lt;P&gt;1 410&lt;/P&gt;&lt;P&gt;1 410&lt;/P&gt;&lt;P&gt;2 200&lt;/P&gt;&lt;P&gt;2 410&lt;/P&gt;&lt;P&gt;2 410&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;getting this output from the above query.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please explain&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2016 16:58:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-values-w-SQL/m-p/250379#M268718</guid>
      <dc:creator>pawandh</dc:creator>
      <dc:date>2016-02-16T16:58:58Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-values-w-SQL/m-p/250400#M268719</link>
      <description>&lt;P&gt;I think, the last two 410s should read 500 in your "output."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That said, your code is almost correct: If you select &lt;STRONG&gt;b&lt;/STRONG&gt;.store instead of &lt;STRONG&gt;a&lt;/STRONG&gt;.store, the unwanted duplicate records will not occur.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The reason is that, although by your WHERE condition a.store and b.store contain equal values, PROC SQL regards a.store and b.store as two separate columns. The GROUP BY clause requests a consolidation of the "b.store-b.n BY groups" within the subset of the Cartesian product which was created by selecting from "test a, test b."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a consequence, selecting &lt;STRONG&gt;b&lt;/STRONG&gt;.store means selecting the single distinct value that b.store has in the respective BY&amp;nbsp;group. Selecting &lt;STRONG&gt;a&lt;/STRONG&gt;.store, however, is interpreted as the request to combine the aggregated values from the BY groups with values (namely of a.store) from original data (in the Cartesian product subset). This is documented in the log by the note:&lt;/P&gt;
&lt;PRE&gt;NOTE: The query requires remerging summary statistics back with the original data.&lt;/PRE&gt;
&lt;P&gt;So, the nicely cumulated sales&amp;nbsp;values (100, 210, 410, ...) are matched to the &lt;STRONG&gt;a&lt;/STRONG&gt;.store values from the BY groups: The first value occurs once, because for b.n=1 there is only one a.n value satisfying the WHERE condition a.n&amp;lt;=b.n (namely a.n=1). (Hence, the said BY group consists of a single observation.)&amp;nbsp;&lt;SPAN&gt;The second&amp;nbsp;value occurs twice, because for b.n=2 there are two a.n values satisfying a.n&amp;lt;=b.n: a.n=1 and a.n=2. (BY group has two obs.) &amp;nbsp;And so on.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2016 17:42:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-values-w-SQL/m-p/250400#M268719</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-02-16T17:42:47Z</dc:date>
    </item>
  </channel>
</rss>

