<?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 make calculations inside a group? in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735967#M38645</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;I used below code but it still only grabs the last 2 quarters instead of the last 4:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select  type,phase,mean(counter) as mean,std(counter) as std
from (
select * from outputdatasetname group by type having quarter=max(quarter) or quarter=max(quarter)-1 or quarter=max(quarter)-2 or quarter=max(quarter)-3
)
group by type,phase;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 21 Apr 2021 13:50:43 GMT</pubDate>
    <dc:creator>Andalusia</dc:creator>
    <dc:date>2021-04-21T13:50:43Z</dc:date>
    <item>
      <title>How to make calculations inside a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735854#M38633</link>
      <description>&lt;P&gt;For every &lt;STRONG&gt;PHASE&lt;/STRONG&gt;&amp;nbsp;in every &lt;STRONG&gt;TYPE&lt;/STRONG&gt; I'm trying to calculate the &lt;STRONG&gt;AVG&lt;/STRONG&gt; and &lt;STRONG&gt;STD&lt;/STRONG&gt;&amp;nbsp;based on the last &lt;STRONG&gt;2&lt;/STRONG&gt; quarters. This is my current code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input type $ quarter phase counter percent;
  datalines;
BB-B 202001 1 94 13
BB-B 202001 2 91 2
BB-B 202001 3 50 2
BB-B 202002 1 79 11
BB-B 202002 2 20 4
BB-B 202002 3 88 4
BB-B 202003 1 17 1
BB-B 202003 2 2 3
BB-B 202003 3 100 3
AA-C 202001 1 67 6
AA-C 202001 2 49 4
AA-C 202001 3 68 4
AA-C 202002 1 23 6
AA-C 202002 2 39 4
AA-C 202003 3 98 4
AA-C 202003 1 7 6
AA-C 202003 2 9 4
AA-C 202003 3 48 4
;
data want;
   set have;
   by  type phase notsorted;
   lcounter=lag(counter);
   if last.phase then do
      avg=mean(lcounter,counter);
      std=std(lcounter,counter);
      output;
   end;
   keep type phase avg std;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;This is my current output:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Andalusia_2-1618992124613.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/58426iF413C9ABE810288F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Andalusia_2-1618992124613.png" alt="Andalusia_2-1618992124613.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;This is my desired output:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;type   phase    AVG        SDeviation
BB-B   1        (79+17)/2  (Sdeviation here)
BB-B   2        (20+2)/2   (Sdeviation here)
BB-B   3        (88/100)/2 (Sdeviation here)
AA-C   1        (23+7)/2   (Sdeviation here)
AA-C   2        (39+9)/2   (Sdeviation here)
AA-C   3        (98+48)/2  (Sdeviation here)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Apr 2021 08:06:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735854#M38633</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-04-21T08:06:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to make calculations inside a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735890#M38637</link>
      <description>&lt;P&gt;Your program is written to expect all the records for a given type/phase to be consecutive (even if not in ascending or descending sort order).&amp;nbsp; But your data is not so grouped.&amp;nbsp; &amp;nbsp;Sort your data by type/phase first.&amp;nbsp; Then run your code.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Apr 2021 09:52:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735890#M38637</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-04-21T09:52:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to make calculations inside a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735893#M38638</link>
      <description>Already done that, but that did not give the correct output.&lt;BR /&gt;</description>
      <pubDate>Wed, 21 Apr 2021 09:56:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735893#M38638</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-04-21T09:56:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to make calculations inside a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735938#M38639</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input type $ quarter phase counter percent;
  datalines;
BB-B 202001 1 94 13
BB-B 202001 2 91 2
BB-B 202001 3 50 2
BB-B 202002 1 79 11
BB-B 202002 2 20 4
BB-B 202002 3 88 4
BB-B 202003 1 17 1
BB-B 202003 2 2 3
BB-B 202003 3 100 3
AA-C 202001 1 67 6
AA-C 202001 2 49 4
AA-C 202001 3 68 4
AA-C 202002 1 23 6
AA-C 202002 2 39 4
AA-C 202003 3 98 4
AA-C 202003 1 7 6
AA-C 202003 2 9 4
AA-C 202003 3 48 4
;

proc sql;
create table want as
select  type,phase,mean(counter) as mean,std(counter) as std
from (
select * from have group by type having quarter=max(quarter) or quarter=max(quarter)-1
)
group by type,phase;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Apr 2021 12:33:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735938#M38639</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-04-21T12:33:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to make calculations inside a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735941#M38640</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;. Thank you. What do I need to change in order to do it for the last 4 quarters instead of the last 2?</description>
      <pubDate>Wed, 21 Apr 2021 12:48:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735941#M38640</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-04-21T12:48:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to make calculations inside a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735958#M38643</link>
      <description>Chang the following&lt;BR /&gt; quarter=max(quarter) or quarter=max(quarter)-1&lt;BR /&gt;&lt;BR /&gt;into&lt;BR /&gt; quarter=max(quarter) or quarter=max(quarter)-1 or  quarter=max(quarter)-2 or quarter=max(quarter)-3</description>
      <pubDate>Wed, 21 Apr 2021 13:30:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735958#M38643</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-04-21T13:30:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to make calculations inside a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735964#M38644</link>
      <description>&lt;P&gt;Do yourself a favor from now on ... don't try to write your own mean and standard deviation (and median and minimum and maximum and a whole lot of other things) code in a DATA step (except for extremely unusual circumstances, which this is not). SAS has already programmed these calculations, tested it, debugged it, and proved that they work in a gazillion real world situations. You are essentially re-inventing the wheel. You (or your company or university) is paying for all this work that SAS has already done, so use that work SAS has already done.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use PROC SUMMARY or PROC SQL (I strongly prefer PROC SUMMARY)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have(where=(quarter&amp;gt;=202002)) nway;
    class type phase;
    var counter;
    output out=want mean=avg stddev=std;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Apr 2021 13:45:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735964#M38644</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-04-21T13:45:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to make calculations inside a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735967#M38645</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;I used below code but it still only grabs the last 2 quarters instead of the last 4:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select  type,phase,mean(counter) as mean,std(counter) as std
from (
select * from outputdatasetname group by type having quarter=max(quarter) or quarter=max(quarter)-1 or quarter=max(quarter)-2 or quarter=max(quarter)-3
)
group by type,phase;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Apr 2021 13:50:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735967#M38645</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-04-21T13:50:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to make calculations inside a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735968#M38646</link>
      <description>What if I want to do it for the last 4 quarters instead of the last 2 as I have right now?&lt;BR /&gt;</description>
      <pubDate>Wed, 21 Apr 2021 13:48:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735968#M38646</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-04-21T13:48:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to make calculations inside a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/736341#M38672</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/358343"&gt;@Andalusia&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;What if I want to do it for the last 4 quarters instead of the last 2 as I have right now?&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Extract the last X observations for every type/phase group first, then run PROC SUMMARY:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let n_quarters = 2;

proc sort data=have;
by type phase quarter;
run;

data extract;
counta = 0;
do until (last.phase);
  set have;
  by type phase;
  counta + 1;
end;
countb = 0;
do until (last.phase);
  set have;
  by type phase;
  countb + 1;
  if countb gt (counta - &amp;amp;n_quarters.) then output;
end;
drop counta countb;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is a slight expansion of the solution I provided &lt;A href="https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-latest-and-second-latest-value-in-a-group/m-p/735517/highlight/true#M38606" target="_blank" rel="noopener"&gt;here&lt;/A&gt;.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 08:22:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/736341#M38672</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-04-22T08:22:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to make calculations inside a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/736374#M38673</link>
      <description>Post some sample data. So I can replicate your question.</description>
      <pubDate>Thu, 22 Apr 2021 11:39:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/736374#M38673</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-04-22T11:39:09Z</dc:date>
    </item>
  </channel>
</rss>

