<?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: Aggregation without creating the new dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872963#M344910</link>
    <description>&lt;P&gt;Your step&amp;nbsp;&lt;U&gt;does&lt;/U&gt; create a new dataset. During execution, the file want.sas7bdat is read, and the results are written to want.sas7bdat.lck. When the step finishes successfully, want.sas7bdat is deleted, and want.sas7bdat.lck renamed to want.sas7bdat.&lt;/P&gt;
&lt;P&gt;Anytime a new variable is added, or observations are inserted (not appended at the "end"), the whole dataset must be rewritten.&lt;/P&gt;</description>
    <pubDate>Sat, 29 Apr 2023 05:54:22 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2023-04-29T05:54:22Z</dc:date>
    <item>
      <title>Aggregation without creating the new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872886#M344871</link>
      <description>&lt;P&gt;I've the following code which creates WANT dataset. Now I want to update the WANT dataset by doing sum(sales) and group by of other fields. I can do this in proc sql with create table statement but I don't want to create any&amp;nbsp; new dataset and instead I want to update the existing WANT dataset with the aggregation as told above. Any help?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data work.want;
set work.have;     
      upn = material;
      country = propcase(country_name);
      mon_intrvl = put(date, yymmd7.);
      sales_date = intnx("month",date,0,"b");
      sales = delivery_qty;
  run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 28 Apr 2023 17:58:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872886#M344871</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2023-04-28T17:58:50Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregation without creating the new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872911#M344881</link>
      <description>&lt;P&gt;Artificial restriction like "I don't want to create any new dataset " don't lend themselves to clean easy to understand code.&lt;/P&gt;
&lt;P&gt;Plus, without an example data set of what this is supposed to look like after the "update" I am not sure exactly what you are expecting as a result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"Group by" isn't really a data step concept either. So an example gets more important.&lt;/P&gt;
&lt;P&gt;Typically I would look at using something like Proc Summary to create the summaries and then merge back to this data.&lt;/P&gt;
&lt;P&gt;If you can do it in SQL then why not?&lt;/P&gt;
&lt;P&gt;In SAS terms any time you add a variable you are not actually updating a data set, you are creating a new set that may just happen to have the same name. That is what your example data step code does as well.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2023 20:04:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872911#M344881</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-04-28T20:04:10Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregation without creating the new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872912#M344882</link>
      <description>I don't know how to do it in sql to update and aggregate the WANT dataset&lt;BR /&gt;in one step.&lt;BR /&gt;&lt;BR /&gt;It would be nice if you can point me the document where I can refer to&lt;BR /&gt;update and aggregate the table in proc summary and data step&lt;BR /&gt;</description>
      <pubDate>Fri, 28 Apr 2023 20:16:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872912#M344882</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2023-04-28T20:16:00Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregation without creating the new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872913#M344883</link>
      <description>&lt;P&gt;No such thing as updating an existing data set and at the same time creating a new column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; said, you can create a brand new data set that has the changes you want and has the same name as the previous data set (highly not recommended, it is a poor programming practice, but yes you can do that).&lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2023 20:21:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872913#M344883</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-04-28T20:21:06Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregation without creating the new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872914#M344884</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select *, material as upn, propcase(country_name) as country,
         put(date, yymmd7.) as mon_intrvl, intnx('month', date, 0, 'b') as sales_date,
         delivery_qty as sales,  
        sum(delivery_qty) as total
from have
group by col1, col2, col3;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is there a reason something like above wouldn't work?&lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2023 20:24:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872914#M344884</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-04-28T20:24:11Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregation without creating the new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872918#M344888</link>
      <description>There were some attrib statements and format statements between data and&lt;BR /&gt;set lines from the initial post which I have not shown. In that case how to&lt;BR /&gt;tweak your proc sql?&lt;BR /&gt;&lt;BR /&gt;If I want to handle this in data step I wish to know how to do it given&lt;BR /&gt;that we have to 'group by' few fields after sum.&lt;BR /&gt;&lt;BR /&gt;Whether your proc sql works if 'from' and 'create table' table name is same?&lt;BR /&gt;</description>
      <pubDate>Fri, 28 Apr 2023 20:37:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872918#M344888</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2023-04-28T20:37:00Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregation without creating the new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872925#M344895</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;There were some attrib statements and format statements between data and&lt;BR /&gt;set lines from the initial post which I have not shown. In that case how to&lt;BR /&gt;tweak your proc sql?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Same way you would specify the attributes in any SQL, via &lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/sqlproc/n09x2o5dobk74an1mupf5f3y8emt.htm" target="_self"&gt;column modifiers&lt;/A&gt;.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select put(....) as new_variable format = $8. length = $8. &lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;BR /&gt;If I want to handle this in data step I wish to know how to do it given&lt;BR /&gt;that we have to 'group by' few fields after sum.&lt;BR /&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It depends on the specifics. You have a few ways of doing this but all essentially recreate the table behind the scenes anyway. An exception to this may be if you're using a DB to store the data and writing directly to it. The code doesn't show the re-creation but it happens.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The SQL supports the group by without summarizing the data as long as you have some fields selected that are unique. It will not summarize the data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some options:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;DoW loop to summarize (still has two passes of data)&lt;/LI&gt;
&lt;LI&gt;Add empty column, summarize data via Summary/SQL/Means and "update" table&lt;/LI&gt;
&lt;/UL&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;Whether your proc sql works if 'from' and 'create table' table name is same?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, but it warns you this isn't a good idea and can lead to problems, which is why it's not recommended by anyone here. Just because you can, doesn't mean you should type deal.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class;
set sashelp.class;
run;

proc sql;
create table class as 
select *, put(age, 8.) as age_char format=$8.,
         mean(weight) as avg_weight_by_gender
from class
group by sex, age;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2023 20:54:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872925#M344895</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-04-28T20:54:03Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregation without creating the new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872963#M344910</link>
      <description>&lt;P&gt;Your step&amp;nbsp;&lt;U&gt;does&lt;/U&gt; create a new dataset. During execution, the file want.sas7bdat is read, and the results are written to want.sas7bdat.lck. When the step finishes successfully, want.sas7bdat is deleted, and want.sas7bdat.lck renamed to want.sas7bdat.&lt;/P&gt;
&lt;P&gt;Anytime a new variable is added, or observations are inserted (not appended at the "end"), the whole dataset must be rewritten.&lt;/P&gt;</description>
      <pubDate>Sat, 29 Apr 2023 05:54:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872963#M344910</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-04-29T05:54:22Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregation without creating the new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872964#M344911</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I don't know how to do it in sql to update and aggregate the WANT dataset&lt;BR /&gt;in one step.&lt;BR /&gt;&lt;BR /&gt;It would be nice if you can point me the document where I can refer to&lt;BR /&gt;update and aggregate the table in proc summary and data step&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Using SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table class as
  select *, sum(weight) as weightsum
  from sashelp.class
  group by sex
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PROC SUMMARY:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=sashelp.class nway;
class sex;
var weight;
output out=class_sum sum(weight)=weightsum;
run;

data class;
set sashelp.class;
if _n_ = 1
then do;
  length weightsum 8;
  declare hash s (dataset:"class_sum");
  s.definekey("sex");
  s.defineedata("weightsum");
  s.definedone();
  weightsum = .;
end;
rc = s.find();
drop rc;
run;

proc delete data=class_sum;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;While the SQL will re-order the data, the second method will keep the original order.&lt;/P&gt;</description>
      <pubDate>Sat, 29 Apr 2023 06:04:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872964#M344911</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-04-29T06:04:31Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregation without creating the new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872965#M344912</link>
      <description>Good to know about this method. Is it possible to achieve this with only&lt;BR /&gt;data step without using any procs? Just curious how to handle it only in&lt;BR /&gt;data step when we want to group by multiple variables.&lt;BR /&gt;</description>
      <pubDate>Sat, 29 Apr 2023 06:09:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872965#M344912</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2023-04-29T06:09:00Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregation without creating the new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872966#M344913</link>
      <description>&lt;P&gt;Since all grouping implies some kind of sorting, you either need a preceding sorting step, or your dataset has to fit into available memory, so you can use a hash object. Be aware that the OUTPUT() method will write the new dataset in order of the key variable(s).&lt;/P&gt;
&lt;P&gt;Either &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13569"&gt;@DonH&lt;/a&gt;&amp;nbsp;or &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;might be able to show us a way how to use a hash object to create a sum by one variable, but order by another.&lt;/P&gt;</description>
      <pubDate>Sat, 29 Apr 2023 06:39:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872966#M344913</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-04-29T06:39:38Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregation without creating the new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872990#M344930</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For generating aggregation within the Data Step, I would recommend looking into&lt;/P&gt;
&lt;P&gt;- The &lt;A title="DOW Loop" href="https://www.lexjansen.com/pharmasug/2014/BB/PharmaSUG-2014-BB02.pdf" target="_self"&gt;DOW Loop&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Additional papers can be found &lt;A title="more DOW loop papers" href="https://www.lexjansen.com/search/searchresults.php?q=DOW%20LOOP" target="_blank" rel="noopener"&gt;here&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- Using&amp;nbsp;&lt;A title="Data Aggregation Using the SAS® Hash Object" href="https://support.sas.com/resources/papers/proceedings15/2000-2015.pdf" target="_blank" rel="noopener"&gt;Data Aggregation Using the SAS® Hash Object&lt;/A&gt; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps&lt;/P&gt;</description>
      <pubDate>Sat, 29 Apr 2023 16:07:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/872990#M344930</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2023-04-29T16:07:49Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregation without creating the new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/873097#M344975</link>
      <description>&lt;P&gt;I have to respectfully disagree with&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Since all grouping implies some kind of sorting, you either need a preceding sorting step, or your dataset has to fit into available memory&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;The hash object does not require the source data to fit into memory. It can (but need not) require the summary table to fit into memory.&amp;nbsp; The book and articles that were noted by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;earlier provide details on memory management issues.&lt;BR /&gt;&lt;BR /&gt;Sorting is not required for the hash object solutions described in&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;'s comments.&lt;BR /&gt;&lt;BR /&gt;I assume on your point about grouping by one variable but ordering by another, you are talking about the case where the order variable is not a grouping variable. If you can clarify I can perhaps provide an example. Regardless, unless the summary table is large, I don't see why a sort after the aggregation is a big deal&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 01 May 2023 01:20:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregation-without-creating-the-new-dataset/m-p/873097#M344975</guid>
      <dc:creator>DonH</dc:creator>
      <dc:date>2023-05-01T01:20:55Z</dc:date>
    </item>
  </channel>
</rss>

