<?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: Summarizing and grouping data in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883231#M39198</link>
    <description>&lt;P&gt;But if you want to calculate total_sales for each quarter (like num_months), then PROC MEANS does it in one step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=blib.order_qtrsum nway;
class customer_id order_qtr;
var sales;
output out=qtrcustomers (drop=_TYPE_ rename=(_FREQ_=num_months)) sum()=total_sales;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you run out of memory (unlikely), sort first and use BY instead of CLASS.&lt;/P&gt;</description>
    <pubDate>Sun, 02 Jul 2023 10:51:17 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2023-07-02T10:51:17Z</dc:date>
    <item>
      <title>Summarizing and grouping data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883201#M39182</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Can anyone please help me with the code.I have also attached the dataset.I need to create variable num_months that counts total months within each quarter that customer had an order.I also need to create variable total_sales&amp;nbsp; that should contain totalsales for each quarter within each customer_id .&lt;/P&gt;&lt;PRE&gt;proc sort data=blib.order_qtrsum out=s;
by customer_id order_qtr;
run;
data qtrcustomers;
set s;
retain num_months;
if first.order_month then num_months=1;
else num_months=num_months+1;
if last.order_month then output ;
run;
proc print;
run;&lt;/PRE&gt;</description>
      <pubDate>Sat, 01 Jul 2023 16:10:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883201#M39182</guid>
      <dc:creator>dona17</dc:creator>
      <dc:date>2023-07-01T16:10:59Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing and grouping data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883202#M39183</link>
      <description>&lt;P&gt;You need a BY statement to create the FIRST. and LAST. variables. And you need to use the correct variable for the determination of the quarter:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data qtrcustomers;
set s;
by customer_id order_qtr;
retain num_months;
if first.order_qtr
then num_months = 1;
else num_months + 1;
if last.order_qtr;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 01 Jul 2023 16:21:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883202#M39183</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-07-01T16:21:48Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing and grouping data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883205#M39184</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why are you doing this with a data step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would use PROC MEANS or PROC SUMMARY or PROC TABULATE or PROC REPORT&amp;nbsp;&lt;BR /&gt;or the&amp;nbsp;SIMPLE.SUMMARY action in CASL (CAS Language SAS VIYA).&lt;BR /&gt;Two other actions that you can use: AGGREGATION.AGGREGATE and DATAPREPROCESS.RUSTATS. &lt;BR /&gt;&lt;BR /&gt;Perfectly fine to do it with a data step of course&lt;/P&gt;
&lt;P&gt;but procedures are often easier to use than a data step (data step requires some programming knowledge).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BR,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jul 2023 17:11:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883205#M39184</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2023-07-01T17:11:32Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing and grouping data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883206#M39185</link>
      <description>Hi,&lt;BR /&gt;Thanks its is working . But I am still struggling with the totalsales variable</description>
      <pubDate>Sat, 01 Jul 2023 17:14:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883206#M39185</guid>
      <dc:creator>dona17</dc:creator>
      <dc:date>2023-07-01T17:14:51Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing and grouping data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883207#M39186</link>
      <description>&lt;PRE&gt;proc sort data=blib.order_qtrsum out=s;
by customer_id order_qtr;
run;
data qtrcustomers;
set s;
by customer_id order_qtr;
retain num_months;
if first.order_qtr then num_months=1;
else num_months+1;
if last.order_qtr  ;
retain total_sales;
if first.customer_id then total_sales=sale_amt;
else total_sales+sale_amt;
if last.customer_id;
run;
proc print;
run;&lt;/PRE&gt;</description>
      <pubDate>Sat, 01 Jul 2023 17:16:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883207#M39186</guid>
      <dc:creator>dona17</dc:creator>
      <dc:date>2023-07-01T17:16:10Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing and grouping data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883208#M39187</link>
      <description>&lt;P&gt;Thank you , I need to do it via data step, but I will try as you suggested.&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jul 2023 17:18:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883208#M39187</guid>
      <dc:creator>dona17</dc:creator>
      <dc:date>2023-07-01T17:18:10Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing and grouping data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883210#M39189</link>
      <description>&lt;P&gt;Keep in mind that I cannot see your dataset. I am posting this from my tablet, where SAS is not available.&lt;/P&gt;
&lt;P&gt;By posting data in code (DATA step with DATALINES), you can avoid this.&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jul 2023 17:50:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883210#M39189</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-07-01T17:50:14Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing and grouping data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883213#M39191</link>
      <description>&lt;P&gt;There is no need to use the RETAIN statement for total_sales if you are using a SUM statement. With a SUM statement (total_sales + sale_amt), there's an implicit RETAIN.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* with retain */
retain total_sales;
if first.customer_id then total_sales=0;
total_sales = (total_sales + sale_amt);
if last.customer_id then output;

/* without retain stmt (but with implicit retain) */
if first.customer_id then total_sales=0;
total_sales + sale_amt;
if last.customer_id then output;

/* output should be the same twice (not tested) */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Koen&lt;/P&gt;
&lt;DIV id="ConnectiveDocSignExtentionInstalled" data-extension-version="1.0.4"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Sat, 01 Jul 2023 19:01:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883213#M39191</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2023-07-01T19:01:52Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing and grouping data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883214#M39192</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data qtrcustomers;
set s;
by customer_id order_qtr;
retain num_months;
if first.order_qtr then num_months=1;
else num_months+1;
if last.order_qtr  ; /* this subsetting IF stops the data step iteration when the condition is false */
retain total_sales;
if first.customer_id then total_sales=sale_amt;
else total_sales+sale_amt;
if last.customer_id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;With two different scopes, you should create two datasets, one for customers, the other for customers and quarters:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data
  qtrcustomers (keep=customer_id order_qtr num_months)
  customers (keep=customer_id total_sales)
;
set s;
by customer_id order_qtr;
if first.order_qtr
then num_months = 1;
else num_months + 1;
if last.order_qtr  then output qtrcustomers;
if first.customer_id
then total_sales = sale_amt;
else total_sales + sale_amt;
if last.customer_id then output customers;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 01 Jul 2023 20:27:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883214#M39192</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-07-01T20:27:44Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing and grouping data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883224#M39196</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/60547"&gt;@sbxkoenk&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why are you doing this with a data step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would use PROC MEANS or PROC SUMMARY or PROC TABULATE or PROC REPORT&amp;nbsp;&lt;BR /&gt;or the&amp;nbsp;SIMPLE.SUMMARY action in CASL (CAS Language SAS VIYA).&lt;BR /&gt;Two other actions that you can use: AGGREGATION.AGGREGATE and DATAPREPROCESS.RUSTATS. &lt;BR /&gt;&lt;BR /&gt;Perfectly fine to do it with a data step of course&lt;/P&gt;
&lt;P&gt;but procedures are often easier to use than a data step (data step requires some programming knowledge).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BR,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Lots of wisdom there from Koen, which I think &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/444673"&gt;@dona17&lt;/a&gt; should pay attention to. In particular, Koen's recommendation to use one of many SAS PROCs is the best approach forward. We have seen time after time in this forum people (usually beginners or near-beginners) trying to write their own data step code to perform "summarizing and grouping", and struggle mightily to do this and sometimes even get the results wrong (and not even realize it). Data steps are not a good tool to perform "summarizing and grouping"! (although it is certainly possible to use data steps to perform "summarizing and grouping") SAS has already done the hard work to write this code to do "summarizing and grouping" for us, then SAS has carefully debugged it and this code has been proven in approximately 12 bazillion real world applications. In &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/444673"&gt;@dona17&lt;/a&gt; 's &lt;A href="https://communities.sas.com/t5/New-SAS-User/summarizing-with-conditional-output/m-p/883209#M39188" target="_self"&gt;other thread&lt;/A&gt;, I show how this is done in PROC SUMMARY, it's very simple, and everyone can and will have extremely high confidence that the calculations are done properly. &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/444673"&gt;@dona17&lt;/a&gt;: please take the wisdom offered here and use PROC SUMMARY (or other SAS PROCs which do similar things).&lt;/P&gt;</description>
      <pubDate>Sun, 02 Jul 2023 09:24:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883224#M39196</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-07-02T09:24:03Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing and grouping data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883231#M39198</link>
      <description>&lt;P&gt;But if you want to calculate total_sales for each quarter (like num_months), then PROC MEANS does it in one step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=blib.order_qtrsum nway;
class customer_id order_qtr;
var sales;
output out=qtrcustomers (drop=_TYPE_ rename=(_FREQ_=num_months)) sum()=total_sales;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you run out of memory (unlikely), sort first and use BY instead of CLASS.&lt;/P&gt;</description>
      <pubDate>Sun, 02 Jul 2023 10:51:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883231#M39198</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-07-02T10:51:17Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing and grouping data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883245#M39199</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Thank you for your valuable suggestions. Noted!&lt;/P&gt;</description>
      <pubDate>Sun, 02 Jul 2023 15:07:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883245#M39199</guid>
      <dc:creator>archita</dc:creator>
      <dc:date>2023-07-02T15:07:22Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing and grouping data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883246#M39200</link>
      <description>&lt;P&gt;Thanks,it is, working.&lt;/P&gt;</description>
      <pubDate>Sun, 02 Jul 2023 15:08:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883246#M39200</guid>
      <dc:creator>archita</dc:creator>
      <dc:date>2023-07-02T15:08:54Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing and grouping data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883247#M39201</link>
      <description>&lt;P&gt;Thank you, its working.&lt;/P&gt;</description>
      <pubDate>Sun, 02 Jul 2023 15:11:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883247#M39201</guid>
      <dc:creator>archita</dc:creator>
      <dc:date>2023-07-02T15:11:20Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing and grouping data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883248#M39202</link>
      <description>&lt;P&gt;thanks,it works.&lt;/P&gt;</description>
      <pubDate>Sun, 02 Jul 2023 15:12:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Summarizing-and-grouping-data/m-p/883248#M39202</guid>
      <dc:creator>archita</dc:creator>
      <dc:date>2023-07-02T15:12:21Z</dc:date>
    </item>
  </channel>
</rss>

