<?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: sum of sales for each city in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748114#M234917</link>
    <description>&lt;P&gt;There is a very slight optimization that can be done:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data summ;
set sales;
by city;
if first.city
then total_sales = sales;
else total_sales + sales;
if last.city;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;At first.city, there will now be only one assignment instead of two.&lt;/P&gt;</description>
    <pubDate>Tue, 15 Jun 2021 13:15:20 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-06-15T13:15:20Z</dc:date>
    <item>
      <title>sum of sales for each city</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748038#M234891</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sales;
input city $  sales ;
datalines;
mumbai 2500
mumbai 1522
delhi  2000
delhi  3500
kolkatta 7500
kolkatta 8000
;
run;

proc sort data= sales;
by city sales;
run;

data s;
set sales;
by city;
if first.city then sales1=0;
sales+sales;
proc print;
run;





proc sql;
select city,sales ,sum(sales) as total
from sales
group by city,sales;
quit;
  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;where I did wrong both datastep and proc sql&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jun 2021 06:37:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748038#M234891</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2021-06-15T06:37:10Z</dc:date>
    </item>
    <item>
      <title>Re: sum of sales for each city</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748043#M234894</link>
      <description>&lt;P&gt;In your data step, you sum up the wrong variable, and you do not suppress the output for each incoming observation (hint: use a subsetting IF with LAST. You have been given NUMEROUS examples for this in your previous questions).&lt;/P&gt;
&lt;P&gt;In your SQL, you group by sales, so the summary will still happen per each observation, not by city alone. Remove sales from the SELECT and the GROUP BY.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jun 2021 07:04:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748043#M234894</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-06-15T07:04:06Z</dc:date>
    </item>
    <item>
      <title>Re: sum of sales for each city</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748077#M234899</link>
      <description>&lt;P&gt;I would not use a data step (or even proc sql), but proc summary to get the sum for each city.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=sales nway;
  class City;
  var Sales;
  output out=city_sums(drop= _:) sum=;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Jun 2021 09:48:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748077#M234899</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-06-15T09:48:46Z</dc:date>
    </item>
    <item>
      <title>Re: sum of sales for each city</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748078#M234900</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary nway data=sales;
    class city; 
    var sales;
    output out=_sums_ sum=sum_sales;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This would be a great time to start learning the very useful and powerful PROC SUMMARY!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;No sorting needed. No writing your own SUM statements in a DATA step. And if you have another variable that has to be summed, you just add additional variable names into the VAR statement.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jun 2021 10:05:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748078#M234900</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-06-15T10:05:02Z</dc:date>
    </item>
    <item>
      <title>Re: sum of sales for each city</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748105#M234911</link>
      <description>interviewer ask datastep how can you solve it so please correct where i did wrong</description>
      <pubDate>Tue, 15 Jun 2021 12:39:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748105#M234911</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2021-06-15T12:39:54Z</dc:date>
    </item>
    <item>
      <title>Re: sum of sales for each city</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748108#M234913</link>
      <description>&lt;P&gt;I already pointed out your mistake. Correct it.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jun 2021 12:42:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748108#M234913</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-06-15T12:42:26Z</dc:date>
    </item>
    <item>
      <title>Re: sum of sales for each city</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748111#M234914</link>
      <description>&lt;P&gt;You could tell the interviewer that PROC SUMMARY also solves the problem, and since you know how to do it in PROC SUMMARY you don't have to write custom DATA step code to do this.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jun 2021 12:51:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748111#M234914</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-06-15T12:51:22Z</dc:date>
    </item>
    <item>
      <title>Re: sum of sales for each city</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748112#M234915</link>
      <description>data sales;&lt;BR /&gt;input city $  sales ;&lt;BR /&gt;datalines;&lt;BR /&gt;mumbai 2500&lt;BR /&gt;mumbai 1522&lt;BR /&gt;delhi  2000&lt;BR /&gt;delhi  3500&lt;BR /&gt;kolkatta 7500&lt;BR /&gt;kolkatta 8000&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc sort data= sales;&lt;BR /&gt;by city ;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data summ;&lt;BR /&gt;set sales;&lt;BR /&gt;by city;&lt;BR /&gt;if first.city then total_sales=0;&lt;BR /&gt;total_sales+ sales;&lt;BR /&gt;if last.city;&lt;BR /&gt;proc print;&lt;BR /&gt;run;&lt;BR /&gt;I got it</description>
      <pubDate>Tue, 15 Jun 2021 13:02:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748112#M234915</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2021-06-15T13:02:05Z</dc:date>
    </item>
    <item>
      <title>Re: sum of sales for each city</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748113#M234916</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265860"&gt;@BrahmanandaRao&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am posting the updated code for use with data step. (There could be other approaches too).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sales;
input city $  sales ;
datalines;
mumbai 2500
mumbai 1522
delhi  2000
delhi  3500
kolkatta 7500
kolkatta 8000
;
run;

proc sort data= sales;
by city sales;
run;

data s (drop =sales);
set sales;
by city;
if first.city then sales1=0;
sales1+sales;
if last.city then output;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="Consolas, Monaco, Andale Mono, Ubuntu Mono, monospace" color="#000000"&gt;&lt;SPAN style="font-size: 14.4px; white-space: pre; background-color: #f5f2f0;"&gt;Only the value of last.city has to be output as we are interested in the sum.&lt;BR /&gt;I have used Sales1 as the accumulator variable to hold the sum.&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jun 2021 13:12:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748113#M234916</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2021-06-15T13:12:02Z</dc:date>
    </item>
    <item>
      <title>Re: sum of sales for each city</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748114#M234917</link>
      <description>&lt;P&gt;There is a very slight optimization that can be done:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data summ;
set sales;
by city;
if first.city
then total_sales = sales;
else total_sales + sales;
if last.city;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;At first.city, there will now be only one assignment instead of two.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jun 2021 13:15:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748114#M234917</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-06-15T13:15:20Z</dc:date>
    </item>
    <item>
      <title>Re: sum of sales for each city</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748124#M234920</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265860"&gt;@BrahmanandaRao&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;interviewer ask datastep how can you solve it so please correct where i did wrong&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;My personal question for an interviewer when given a question like this would be to clarify exactly why a data step is needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Other approaches such as Proc SQL, Proc Summary/Means, Proc Tabulate and Proc Report will do the same thing, often with less headache depending on the specific sum(s) needed. So there should be a reason to be forced to use a data step solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sometimes an interview question isn't so much can you do exactly what is mentioned but do you know enough to ask why an approach might be needed.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jun 2021 14:02:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748124#M234920</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-06-15T14:02:38Z</dc:date>
    </item>
    <item>
      <title>Re: sum of sales for each city</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748127#M234921</link>
      <description>&lt;P&gt;I actually like this question as an interview question. It quickly demonstrates if a programmer understands a data step and by group functionality. It's definitely not something that's required but there are various reasons to do a running total or lagged data and this demonstrates both ideas.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Its also a very basic level of understanding and can easily verify if a programmers skill is at the level they claim during an interview process.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's also kind of amusing when people don't even consider if their interviewers/testers are on the same forums and see the questions/answers.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jun 2021 14:18:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748127#M234921</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-15T14:18:17Z</dc:date>
    </item>
    <item>
      <title>Re: sum of sales for each city</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748159#M234937</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&amp;nbsp;&lt;A class="trigger-hovercard" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562" target="_blank"&gt;KurtBremser&amp;nbsp;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;if we are using &lt;STRONG&gt;&lt;FONT color="#000000"&gt;sum statement&lt;/FONT&gt;&lt;/STRONG&gt; instead of &lt;STRONG&gt;sum function&lt;/STRONG&gt; we&amp;nbsp; getting problem when missing values are there data&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jun 2021 15:46:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748159#M234937</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2021-06-15T15:46:01Z</dc:date>
    </item>
    <item>
      <title>Re: sum of sales for each city</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748171#M234942</link>
      <description>Your example data does not demonstrate any missing values so it seems like it may not reflect your actual data. If that's the case please update your question with a more accurate data example.</description>
      <pubDate>Tue, 15 Jun 2021 16:39:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748171#M234942</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-15T16:39:53Z</dc:date>
    </item>
    <item>
      <title>Re: sum of sales for each city</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748172#M234943</link>
      <description>&lt;P&gt;I just felt I would mention this: PROC SUMMARY handles missing values appropriately, no extra coding needed.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jun 2021 16:45:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748172#M234943</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-06-15T16:45:17Z</dc:date>
    </item>
    <item>
      <title>Re: sum of sales for each city</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748182#M234949</link>
      <description>&lt;P&gt;Hello All.&lt;BR /&gt;This topic is very interesting and there is&amp;nbsp; a lot for me to learn.&lt;BR /&gt;I have a basic question, Please excuse me for my ignorance.&lt;BR /&gt;If the question has to be answered using data step only, can one use procs's for example proc means or proc sql ?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jun 2021 17:18:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748182#M234949</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2021-06-15T17:18:44Z</dc:date>
    </item>
    <item>
      <title>Re: sum of sales for each city</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748185#M234951</link>
      <description>&lt;P&gt;&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;I think the problem SHOULD be answered by PROC SUMMARY/PROC MEANS or PROC SQL. I don't think people should program their own sums or means or other basic statistics by group in a DATA step. We have seen, in the SAS Communities, over and over again, people (usually with just a little experience in SAS) struggle to do this in a DATA step, and get it wrong, or they write code that doesn't work, or they write code that mis-handles missing values. Why spend the time writing DATA step code, debugging it, verifying it works, when SAS has already done this in PROC SUMMARY (and it works properly with missing values)???&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jun 2021 17:48:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748185#M234951</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-06-15T17:48:02Z</dc:date>
    </item>
    <item>
      <title>Re: sum of sales for each city</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748189#M234954</link>
      <description>"If the question has to be answered using data step only" is a an artificial limit that only exists in non work like situation, such as homework, class exams or tests before a job. And IMO, those situations are typically one where a user should be able to answer the question themselves without significant assistance of external users.</description>
      <pubDate>Tue, 15 Jun 2021 18:16:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748189#M234954</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-15T18:16:51Z</dc:date>
    </item>
    <item>
      <title>Re: sum of sales for each city</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748191#M234955</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265860"&gt;@BrahmanandaRao&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&amp;nbsp;&lt;A class="trigger-hovercard" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562" target="_blank" rel="noopener"&gt;KurtBremser&amp;nbsp;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;if we are using &lt;STRONG&gt;&lt;FONT color="#000000"&gt;sum statement&lt;/FONT&gt;&lt;/STRONG&gt; instead of &lt;STRONG&gt;sum function&lt;/STRONG&gt; we&amp;nbsp; getting problem when missing values are there data&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Maxim 4 (short version): Try It.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jun 2021 18:31:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-sales-for-each-city/m-p/748191#M234955</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-06-15T18:31:04Z</dc:date>
    </item>
  </channel>
</rss>

