<?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 calculate variables inside a group? in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-variables-inside-a-group/m-p/737534#M38734</link>
    <description>&lt;P&gt;You now have a nice dataset with the last 4 quarters for every group, now you just need to run the statistics on it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=extract;
by type phase;
var counter;
output out=want (drop=_type_ _freq_) mean()=mean std()=std;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 28 Apr 2021 10:39:14 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-04-28T10:39:14Z</dc:date>
    <item>
      <title>How to calculate variables inside a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-variables-inside-a-group/m-p/737514#M38728</link>
      <description>&lt;P&gt;For every&amp;nbsp;&lt;STRONG&gt;PHASE&lt;/STRONG&gt;&amp;nbsp;in every&amp;nbsp;&lt;STRONG&gt;TYPE&lt;/STRONG&gt;&amp;nbsp;I'm trying to calculate the&amp;nbsp;&lt;STRONG&gt;AVG&lt;/STRONG&gt;&amp;nbsp;and&amp;nbsp;&lt;STRONG&gt;STD&lt;/STRONG&gt;&amp;nbsp;based on the last &lt;STRONG&gt;4&amp;nbsp;&lt;/STRONG&gt;&lt;STRONG&gt;quarters&lt;/STRONG&gt;. 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 10 11
BB-B 202002 2 20 4
BB-B 202002 3 5 4
BB-B 202003 1 10 1
BB-B 202003 2 20 3
BB-B 202003 3 7 3
BB-B 202004 1 10 1
BB-B 202004 2 20 3
BB-B 202004 3 7 3
BB-B 202101 1 10 1
BB-B 202101 2 20 3
BB-B 202101 3 7 3
AA-C 202001 1 67 6
AA-C 202001 2 38 4
AA-C 202001 3 68 4
AA-C 202002 1 29 6
AA-C 202002 2 38 4
AA-C 202002 3 89 4
AA-C 202003 1 20 6
AA-C 202003 2 39 4
AA-C 202003 3 105 4
AA-C 202004 1 15 6
AA-C 202004 2 36 4
AA-C 202004 3 95 4
AA-C 202101 1 27 6
AA-C 202101 2 45 4
AA-C 202101 3 98 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 or quarter=max(quarter)-2 or quarter=max(quarter)-3
)
group by type,phase;
quit;

&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_1-1619602944112.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/58760i2FBF466C6DDAF91B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Andalusia_1-1619602944112.png" alt="Andalusia_1-1619602944112.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;BR /&gt;&lt;BR /&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;LI-CODE lang="sas"&gt;type   phase    AVG        SDeviation
BB-B   1        (10+10+10+10)/4       (Sdeviation here)
BB-B   2        (20+20+20+20)/4       (Sdeviation here)
BB-B   3        (7+7+7+5)/4           (Sdeviation here)
AA-C   1        (27+15+20+29)/4       (Sdeviation here)
AA-C   2        (45+36+39+38)/4       (Sdeviation here)
AA-C   3        (98+95+105+89)/4      (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;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Apr 2021 09:49:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-variables-inside-a-group/m-p/737514#M38728</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-04-28T09:49:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate variables inside a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-variables-inside-a-group/m-p/737517#M38729</link>
      <description>&lt;P&gt;How is this question different than your earlier question, in which an answer was given to compute the results for the last X quarters?&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735968" target="_blank"&gt;https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-make-calculations-inside-a-group/m-p/735968&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Apr 2021 09:49:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-variables-inside-a-group/m-p/737517#M38729</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-04-28T09:49:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate variables inside a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-variables-inside-a-group/m-p/737519#M38730</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;, that question was only about calculating the last 2 quarters. This one is about calculating the last 4 quarters. So this question also has different sample data.</description>
      <pubDate>Wed, 28 Apr 2021 09:52:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-variables-inside-a-group/m-p/737519#M38730</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-04-28T09:52:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate variables inside a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-variables-inside-a-group/m-p/737523#M38731</link>
      <description>&lt;P&gt;In your other thread, Kurt Bremser explained how to get the results you want from the last X quarters. Just because the data is different isn't a good explanation, because it seems the goal is the same, so it should work regardless of the data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, again I am not sure what is different, and a more detailed explanation would help.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Apr 2021 09:56:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-variables-inside-a-group/m-p/737523#M38731</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-04-28T09:56:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate variables inside a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-variables-inside-a-group/m-p/737529#M38732</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;So I tried Kurts snippet on aboves sample data but I did not got the desired output:&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;Kurt's Snippet:&lt;/STRONG&gt;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;%let n_quarters = 4;

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;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;BR /&gt;&lt;STRONG&gt;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_0-1619605336202.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/58763i92F8DBC2933E4635/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Andalusia_0-1619605336202.png" alt="Andalusia_0-1619605336202.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Apr 2021 10:22:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-variables-inside-a-group/m-p/737529#M38732</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-04-28T10:22:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate variables inside a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-variables-inside-a-group/m-p/737531#M38733</link>
      <description>&lt;P&gt;And then all you have to do is compute means and standard deviations by TYPE and PHASE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=extract nway;
    class type phase;
    var counter;
    output out=want mean=count_mean std=count_std;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Apr 2021 10:33:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-variables-inside-a-group/m-p/737531#M38733</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-04-28T10:33:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate variables inside a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-variables-inside-a-group/m-p/737534#M38734</link>
      <description>&lt;P&gt;You now have a nice dataset with the last 4 quarters for every group, now you just need to run the statistics on it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=extract;
by type phase;
var counter;
output out=want (drop=_type_ _freq_) mean()=mean std()=std;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Apr 2021 10:39:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-variables-inside-a-group/m-p/737534#M38734</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-04-28T10:39:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate variables inside a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-variables-inside-a-group/m-p/737539#M38735</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;Thank you both. That was indeed the last step I missed!&lt;/P&gt;</description>
      <pubDate>Wed, 28 Apr 2021 11:09:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-variables-inside-a-group/m-p/737539#M38735</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-04-28T11:09:31Z</dc:date>
    </item>
  </channel>
</rss>

