<?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 sum a column in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-a-column/m-p/810295#M319540</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/222217"&gt;@di_niu0&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;A very simple approach to solve your issues is to use Proc SQL. The function count(cost) sums the cost and makes it available in all rows. Internally&amp;nbsp; behind the scene it makes two passes as described as described by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;but a user need not be worried about it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id cost;
datalines;
1 10
2 40
3 50
;
run;
proc sql;
create table want as
select *, sum(cost) as total
from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There will be a message in the log like this indicating that the some was calculated and merged .&lt;/P&gt;
&lt;PRE&gt;NOTE: The query requires remerging summary statistics back with the original data&lt;/PRE&gt;
&lt;P&gt;The output will be what you wanted.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Sajid01_0-1651109146880.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/70924iA520B723CFF73B87/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Sajid01_0-1651109146880.png" alt="Sajid01_0-1651109146880.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 28 Apr 2022 01:28:25 GMT</pubDate>
    <dc:creator>Sajid01</dc:creator>
    <dc:date>2022-04-28T01:28:25Z</dc:date>
    <item>
      <title>How to sum a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-a-column/m-p/810284#M319532</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to sum the cost to get the variable TOTAL (it only has ID and cost in the original table). Please see below. Thank you in advance!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;COST&amp;nbsp; &amp;nbsp; &amp;nbsp; TOTAL&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;40&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;50&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;/P&gt;</description>
      <pubDate>Wed, 27 Apr 2022 22:30:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-a-column/m-p/810284#M319532</guid>
      <dc:creator>di_niu0</dc:creator>
      <dc:date>2022-04-27T22:30:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-a-column/m-p/810285#M319533</link>
      <description>Please use data step if possible.</description>
      <pubDate>Wed, 27 Apr 2022 22:32:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-a-column/m-p/810285#M319533</guid>
      <dc:creator>di_niu0</dc:creator>
      <dc:date>2022-04-27T22:32:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-a-column/m-p/810288#M319535</link>
      <description>&lt;P&gt;If you want to produce this with a data step, you need it to pass through the data set twice:&amp;nbsp; The first time to establish the total, and the second time to reread and output each obs with the total established in the first pass.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So here's the first two statements to do that:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have (in=firstpass)   have (in=secondpass);
...
...
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The SET statement above reads ALL the observations with the FIRSTPASS=1 condition (and SECONDPASS=0), and then rereads them with SECONDPASS=1 and FIRSTPASS=0.&amp;nbsp; So you have dummy variables available to determine whether the observation-hand is from the first pass or the second pass.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So write code after the SET that creates a total during the first pass, and a filter that allows output only during the second pass.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Apr 2022 23:34:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-a-column/m-p/810288#M319535</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-04-27T23:34:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-a-column/m-p/810295#M319540</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/222217"&gt;@di_niu0&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;A very simple approach to solve your issues is to use Proc SQL. The function count(cost) sums the cost and makes it available in all rows. Internally&amp;nbsp; behind the scene it makes two passes as described as described by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;but a user need not be worried about it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id cost;
datalines;
1 10
2 40
3 50
;
run;
proc sql;
create table want as
select *, sum(cost) as total
from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There will be a message in the log like this indicating that the some was calculated and merged .&lt;/P&gt;
&lt;PRE&gt;NOTE: The query requires remerging summary statistics back with the original data&lt;/PRE&gt;
&lt;P&gt;The output will be what you wanted.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Sajid01_0-1651109146880.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/70924iA520B723CFF73B87/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Sajid01_0-1651109146880.png" alt="Sajid01_0-1651109146880.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Apr 2022 01:28:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-a-column/m-p/810295#M319540</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2022-04-28T01:28:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-a-column/m-p/810300#M319544</link>
      <description>&lt;P&gt;Do you really have to sum the costs of different ids? This seems strange.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Apr 2022 05:25:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-a-column/m-p/810300#M319544</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-04-28T05:25:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-a-column/m-p/810329#M319558</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;Please use data step if possible.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For anyone else who doesn't have this unfortunate restriction, use the SQL solution from &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/131732"&gt;@Sajid01&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;For &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/222217"&gt;@di_niu0&lt;/a&gt; who seems to require a data step, why??? In SAS, there are many ways to accomplish something, and sometimes you can have better solutions by removing this restriction.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Apr 2022 10:19:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-a-column/m-p/810329#M319558</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-28T10:19:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-a-column/m-p/810359#M319569</link>
      <description>Thanks!</description>
      <pubDate>Thu, 28 Apr 2022 13:49:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-a-column/m-p/810359#M319569</guid>
      <dc:creator>di_niu0</dc:creator>
      <dc:date>2022-04-28T13:49:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-a-column/m-p/810360#M319570</link>
      <description>It's to incorporate other codes.</description>
      <pubDate>Thu, 28 Apr 2022 13:50:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-a-column/m-p/810360#M319570</guid>
      <dc:creator>di_niu0</dc:creator>
      <dc:date>2022-04-28T13:50:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-a-column/m-p/810365#M319571</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/222217"&gt;@di_niu0&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;It's to incorporate other codes.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;does not answer the question, as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/131732"&gt;@Sajid01&lt;/a&gt;&amp;nbsp;has shown this is easy to program in PROC SQL&lt;/P&gt;</description>
      <pubDate>Thu, 28 Apr 2022 14:01:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-a-column/m-p/810365#M319571</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-28T14:01:21Z</dc:date>
    </item>
  </channel>
</rss>

