<?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: Creating new aggregated variables for base table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-aggregated-variables-for-base-table/m-p/438396#M109302</link>
    <description>&lt;P&gt;The means you could get using PROC MEANS or SQL. But when you also want last minus first for another variable, you may as well do the whole thing in a datastep, e.g.:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data want;
  Sum_var1=0;&lt;BR /&gt;  Count_var1=0;
  do until(last.customer);
    set have;
    by customer;&lt;BR /&gt;    if not missing(Variable1) then do;
      Sum_var1=Sum_var1+Variable1;&lt;BR /&gt;      Count_var1=Count_var1+1;&lt;BR /&gt;      end;&lt;BR /&gt;    if first.customer then &lt;BR /&gt;      First_var2=Variable2;
    end;
  Diff_var2=Variable2-First_var2; /* last value minus first value */&lt;BR /&gt;  if Count_var1 then&lt;BR /&gt;    Mean_var1=Sum_var1/Count_var1;&lt;BR /&gt;  keep customer Mean_var1 Diff_var2;
run;  &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The data should be sorted by customer and month, of course.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It can also be done in SQL, though:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table want as select
    a.*,
    mean(a.Variable1) as mean_var1,
    Last.Variable2-First.variable2 as Diff_var2
  from
    have a,
    have first,
    have last
  where first.customer=a.customer
    and last.customer=a.customer
  group by a.customer
  having first.month=min(a.month)
     and last.month=max(a.month)&lt;BR /&gt;  order by a.customer, a.month
  ;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here, you can easily get all the original rows without remerging with the original data afterwards. If you&amp;nbsp;only want the two summary&amp;nbsp;rows, you should&amp;nbsp;add a DISTINCT after SELECT, replace&amp;nbsp;a.* with a.customer, and drop a.month from the ORDER BY clause.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 19 Feb 2018 13:40:10 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2018-02-19T13:40:10Z</dc:date>
    <item>
      <title>Creating new aggregated variables for base table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-aggregated-variables-for-base-table/m-p/437168#M108826</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am pretty new to SAS, and are having some problems understanding how to create&amp;nbsp;a base table.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;At the moment I have the following table:&lt;BR /&gt;&lt;BR /&gt;Customer:&amp;nbsp; &amp;nbsp; &amp;nbsp; Month:&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Variable1:&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Variable2:&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.........&amp;nbsp; &amp;nbsp;VariableN&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 56&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 78&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 71&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 81&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 70&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 78&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 100&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;102&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 98&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;88&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 89&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 81&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;What I want is this:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Customer:&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Avg.Variable 1:&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Change.Variable2:&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; (mean of variable 1 for&amp;nbsp;all months)&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;(last month - first month)&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;(mean of variable 1 for&amp;nbsp;all months)&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;(last month - first month)&amp;nbsp;&lt;/SPAN&gt;&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;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Hope it is clear ish. Any suggestions?&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Feb 2018 16:01:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-aggregated-variables-for-base-table/m-p/437168#M108826</guid>
      <dc:creator>AntrOsl</dc:creator>
      <dc:date>2018-02-14T16:01:14Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new aggregated variables for base table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-aggregated-variables-for-base-table/m-p/438396#M109302</link>
      <description>&lt;P&gt;The means you could get using PROC MEANS or SQL. But when you also want last minus first for another variable, you may as well do the whole thing in a datastep, e.g.:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data want;
  Sum_var1=0;&lt;BR /&gt;  Count_var1=0;
  do until(last.customer);
    set have;
    by customer;&lt;BR /&gt;    if not missing(Variable1) then do;
      Sum_var1=Sum_var1+Variable1;&lt;BR /&gt;      Count_var1=Count_var1+1;&lt;BR /&gt;      end;&lt;BR /&gt;    if first.customer then &lt;BR /&gt;      First_var2=Variable2;
    end;
  Diff_var2=Variable2-First_var2; /* last value minus first value */&lt;BR /&gt;  if Count_var1 then&lt;BR /&gt;    Mean_var1=Sum_var1/Count_var1;&lt;BR /&gt;  keep customer Mean_var1 Diff_var2;
run;  &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The data should be sorted by customer and month, of course.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It can also be done in SQL, though:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table want as select
    a.*,
    mean(a.Variable1) as mean_var1,
    Last.Variable2-First.variable2 as Diff_var2
  from
    have a,
    have first,
    have last
  where first.customer=a.customer
    and last.customer=a.customer
  group by a.customer
  having first.month=min(a.month)
     and last.month=max(a.month)&lt;BR /&gt;  order by a.customer, a.month
  ;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here, you can easily get all the original rows without remerging with the original data afterwards. If you&amp;nbsp;only want the two summary&amp;nbsp;rows, you should&amp;nbsp;add a DISTINCT after SELECT, replace&amp;nbsp;a.* with a.customer, and drop a.month from the ORDER BY clause.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2018 13:40:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-aggregated-variables-for-base-table/m-p/438396#M109302</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-02-19T13:40:10Z</dc:date>
    </item>
  </channel>
</rss>

