<?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 the average and standard deviation of the last 2 values in SAS? in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-the-average-and-standard-deviation-of-the-last/m-p/735994#M38649</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;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; in my real dataset I have much more points. Just a question. What if I want to calculate the AVG and STD of based on the last 4 quarters instead of 2?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The Lag approach I suggested for two cases can be extended back to 100 records. However looking back that way adds in potential issues with looking back into a previous phase or type.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are adding in some complications that raise questions of 1) will there always going to be at least 4 quarters? 2) if there are fewer than 4 quarters but you want 4 what to do, 3) Are your "4 quarters" consecutive, if not how to treat gaps?&lt;/P&gt;
&lt;P&gt;Also is "last" actually best description? Quarters usually relate to calendar dates and do you mean "after a given date" instead? (This is much easier to do once actual dates are supplied in data).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Make example data that demonstrates more of the actual data and replicating and problem cases such a fewer "quarters", missing quarters if they ever occur in the data (you have looked haven't you).&lt;/P&gt;
&lt;P&gt;Then show the desired values for that example data.&lt;/P&gt;
&lt;P&gt;Your "example" data might imply that 4 quarters means a calendar year. In which case construct a calendar year variable and use proc means or summary with type, year and phase as class variables and request the statistics for the variable as a VAR variable.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;data need;
   set have;
   year = int(quarter/100);
run;

proc summary data=need nway;
   class type year phase;
   var counter;
   output out=work.summary mean= std= /autoname;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your last 4 quarters crosses a calendar year boundary then you need to show example data and possibly provide a cleaner description.&lt;/P&gt;
&lt;P&gt;BTW, consider making your quarter variable a date value because then SAS provides a number of tools that are easier to work with than pseudo-date values that need parsing for every step.&lt;/P&gt;</description>
    <pubDate>Wed, 21 Apr 2021 14:38:20 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-04-21T14:38:20Z</dc:date>
    <item>
      <title>How to calculate the average and standard deviation of the last 2 values in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-the-average-and-standard-deviation-of-the-last/m-p/735594#M38624</link>
      <description>&lt;P&gt;I have the following dataset:&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 202002 1 79 11
BB-B 202003 1 17 1
BB-B 202004 1 80 1
BB-B 202001 2 91 2
BB-B 202002 2 20 3
BB-B 202003 2 2 3
BB-B 202004 2 85 3
AA-C 202001 1 67 6
AA-C 202002 1 23 6
AA-C 202003 1 7 6
AA-C 202004 1 94 4
AA-C 202001 2 49 4
AA-C 202002 2 39 4
AA-C 202003 2 9 4
AA-C 202004 2 98 1
;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;For every PHASE in every TYPE I want to calculate the AVERAGE and STANDARDDEVIATION of the last &lt;STRONG&gt;2&lt;/STRONG&gt; QUARTERS.&lt;BR /&gt;&lt;STRONG&gt;My desired output is:&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;type   phase    AVG        SDeviation
BB-B   1        (17+80)/2  (Sdeviation here)
BB-B   2        (85+2)/2   (Sdeviation here)
AA-C   1        (94+7)/2   (Sdeviation here)
AA-C   2        (98+9)/2   (Sdeviation here)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Apr 2021 14:56:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-the-average-and-standard-deviation-of-the-last/m-p/735594#M38624</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-04-20T14:56:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the average and standard deviation of the last 2 values in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-the-average-and-standard-deviation-of-the-last/m-p/735601#M38627</link>
      <description>You want the standard deviation of two data points?</description>
      <pubDate>Tue, 20 Apr 2021 15:06:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-the-average-and-standard-deviation-of-the-last/m-p/735601#M38627</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-04-20T15:06:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the average and standard deviation of the last 2 values in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-the-average-and-standard-deviation-of-the-last/m-p/735607#M38629</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;
&lt;P&gt;I have the following dataset:&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 202002 1 79 11
BB-B 202003 1 17 1
BB-B 202004 1 80 1
BB-B 202001 2 91 2
BB-B 202002 2 20 3
BB-B 202003 2 2 3
BB-B 202004 2 85 3
AA-C 202001 1 67 6
AA-C 202002 1 23 6
AA-C 202003 1 7 6
AA-C 202004 1 94 4
AA-C 202001 2 49 4
AA-C 202002 2 39 4
AA-C 202003 2 9 4
AA-C 202004 2 98 1
;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For every PHASE in every TYPE I want to calculate the AVERAGE and STANDARDDEVIATION of the last &lt;STRONG&gt;2&lt;/STRONG&gt; QUARTERS.&lt;BR /&gt;&lt;STRONG&gt;My desired output is:&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;type   phase    AVG        SDeviation
BB-B   1        (17+80)/2  (Sdeviation here)
BB-B   2        (85+2)/2   (Sdeviation here)
AA-C   1        (94+7)/2   (Sdeviation here)
AA-C   2        (98+9)/2   (Sdeviation here)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This seems to do what is requested for the given data but two points for a standard deviation is a bit odd.&lt;/P&gt;
&lt;PRE&gt;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;/PRE&gt;</description>
      <pubDate>Tue, 20 Apr 2021 15:18:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-the-average-and-standard-deviation-of-the-last/m-p/735607#M38629</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-04-20T15:18:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the average and standard deviation of the last 2 values in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-the-average-and-standard-deviation-of-the-last/m-p/735837#M38632</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; in my real dataset I have much more points. Just a question. What if I want to calculate the AVG and STD of based on the last 4 quarters instead of 2?</description>
      <pubDate>Wed, 21 Apr 2021 06:54:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-the-average-and-standard-deviation-of-the-last/m-p/735837#M38632</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-04-21T06:54:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the average and standard deviation of the last 2 values in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-the-average-and-standard-deviation-of-the-last/m-p/735859#M38634</link>
      <description>&lt;P&gt;So you have&amp;nbsp;&lt;EM&gt;multiple&lt;/EM&gt; observations per type and quarter?&lt;/P&gt;
&lt;P&gt;Please take care to set up your example data sufficiently so that it really illustrates your issue.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Apr 2021 08:12:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-the-average-and-standard-deviation-of-the-last/m-p/735859#M38634</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-04-21T08:12:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the average and standard deviation of the last 2 values in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-the-average-and-standard-deviation-of-the-last/m-p/735860#M38635</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; Check out my latest question, its almost the same as this one but with more sufficient example data.</description>
      <pubDate>Wed, 21 Apr 2021 08:14:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-the-average-and-standard-deviation-of-the-last/m-p/735860#M38635</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-04-21T08:14:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the average and standard deviation of the last 2 values in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-the-average-and-standard-deviation-of-the-last/m-p/735947#M38641</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 13:05:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-the-average-and-standard-deviation-of-the-last/m-p/735947#M38641</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-04-21T13:05:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the average and standard deviation of the last 2 values in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-the-average-and-standard-deviation-of-the-last/m-p/735979#M38647</link>
      <description>&lt;P&gt;Do you have a SAS/ETS license? Check using the following code:&lt;BR /&gt;&lt;BR /&gt;proc product_status;run;&lt;BR /&gt;&lt;BR /&gt;Check your log for SAS/ETS.&lt;BR /&gt;&lt;BR /&gt;Check the log for SAS/ETS listed.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Apr 2021 15:19:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-the-average-and-standard-deviation-of-the-last/m-p/735979#M38647</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-04-21T15:19:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the average and standard deviation of the last 2 values in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-the-average-and-standard-deviation-of-the-last/m-p/735994#M38649</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;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; in my real dataset I have much more points. Just a question. What if I want to calculate the AVG and STD of based on the last 4 quarters instead of 2?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The Lag approach I suggested for two cases can be extended back to 100 records. However looking back that way adds in potential issues with looking back into a previous phase or type.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are adding in some complications that raise questions of 1) will there always going to be at least 4 quarters? 2) if there are fewer than 4 quarters but you want 4 what to do, 3) Are your "4 quarters" consecutive, if not how to treat gaps?&lt;/P&gt;
&lt;P&gt;Also is "last" actually best description? Quarters usually relate to calendar dates and do you mean "after a given date" instead? (This is much easier to do once actual dates are supplied in data).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Make example data that demonstrates more of the actual data and replicating and problem cases such a fewer "quarters", missing quarters if they ever occur in the data (you have looked haven't you).&lt;/P&gt;
&lt;P&gt;Then show the desired values for that example data.&lt;/P&gt;
&lt;P&gt;Your "example" data might imply that 4 quarters means a calendar year. In which case construct a calendar year variable and use proc means or summary with type, year and phase as class variables and request the statistics for the variable as a VAR variable.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;data need;
   set have;
   year = int(quarter/100);
run;

proc summary data=need nway;
   class type year phase;
   var counter;
   output out=work.summary mean= std= /autoname;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your last 4 quarters crosses a calendar year boundary then you need to show example data and possibly provide a cleaner description.&lt;/P&gt;
&lt;P&gt;BTW, consider making your quarter variable a date value because then SAS provides a number of tools that are easier to work with than pseudo-date values that need parsing for every step.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Apr 2021 14:38:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-calculate-the-average-and-standard-deviation-of-the-last/m-p/735994#M38649</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-04-21T14:38:20Z</dc:date>
    </item>
  </channel>
</rss>

