<?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: SQL AVG Function Case when on selective rows in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/SQL-AVG-Function-Case-when-on-selective-rows/m-p/818736#M20381</link>
    <description>&lt;P&gt;Follow up question&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to sum the prior 12 month sales , however there would be scenarios where there are less than 12 months, then I'd like to annualize the sales&amp;nbsp; based on the # of months. I do have a record on how many months they have been on book.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code I have below which does not work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;,sum(case when max(mob)&amp;gt;=12 and yyyymm&amp;gt;="&amp;amp;pre12m." then sum(sales)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;when max(mob)&amp;lt;12 and yyyymm&amp;gt;="&amp;amp;pre12m." then (sum(sales)/max(mob))*12&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else 0 end) as Pre12m_sales_annualized&lt;/P&gt;</description>
    <pubDate>Fri, 17 Jun 2022 00:32:43 GMT</pubDate>
    <dc:creator>superking</dc:creator>
    <dc:date>2022-06-17T00:32:43Z</dc:date>
    <item>
      <title>SQL AVG Function Case when on selective rows</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/SQL-AVG-Function-Case-when-on-selective-rows/m-p/818643#M20378</link>
      <description>&lt;P&gt;The dataset consist of multiple months (24+ months YYYYMM)&lt;/P&gt;&lt;P&gt;Pre6m_sales_avg is averaging on all 24 months&amp;nbsp; or rows of records.&lt;/P&gt;&lt;P&gt;I want to only average based on the prior 6 months of records.&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are some account with that has less than 6 months of records..&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table work.temp2 as&lt;BR /&gt;select&lt;BR /&gt;a.account_id&lt;/P&gt;&lt;P&gt;,sum(case when yyyymm&amp;gt;="&amp;amp;pre6m." then sales else 0 end) as Pre6m_sales_sum&lt;/P&gt;&lt;P&gt;,avg(case when yyyymm&amp;gt;="202101"&amp;nbsp; then sales else 0 end) as Pre6m_sales_avg&lt;/P&gt;&lt;P&gt;from work.temp1 a&lt;BR /&gt;group by 1&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jun 2022 17:31:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/SQL-AVG-Function-Case-when-on-selective-rows/m-p/818643#M20378</guid>
      <dc:creator>superking</dc:creator>
      <dc:date>2022-06-16T17:31:29Z</dc:date>
    </item>
    <item>
      <title>Re: SQL AVG Function Case when on selective rows</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/SQL-AVG-Function-Case-when-on-selective-rows/m-p/818649#M20379</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp2;

array p{0:5} _temporary_;
set temp1; 
by account_id yyyymm;

if first.account_id then do;
 call missing(of p{*}); 
  counter=0;
end;

counter+1;
p{mod(_n_,6)} = sales;

if counter &amp;gt;=6 then do;
    Pre6m_sales_sum= sum(of p{*});
    Pre6m_sales_avg= mean(of p{*});
end;
else do;
   Pre6m_sales_sum=0;
    Pre6m_sales_avg=0;
end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Assuming your data is sorted, I prefer temporary arrays. Otherwise, you do need a self join and a method to handle when you have less than 6 occurrences (ie the first 5 months of your data set). That becomes a few CASE statements in SQL.&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>Thu, 16 Jun 2022 17:42:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/SQL-AVG-Function-Case-when-on-selective-rows/m-p/818649#M20379</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-06-16T17:42:26Z</dc:date>
    </item>
    <item>
      <title>Re: SQL AVG Function Case when on selective rows</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/SQL-AVG-Function-Case-when-on-selective-rows/m-p/818677#M20380</link>
      <description>&lt;P&gt;Zero is just another number... if you want to remove some values from the average calculations, you must replace them with missing values:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table work.temp2 as
select
a.account_id
,sum(case when yyyymm&amp;gt;="&amp;amp;pre6m." then sales else . end) as Pre6m_sales_sum
,avg(case when yyyymm&amp;gt;="202101"  then sales else . end) as Pre6m_sales_avg
from work.temp1 a
group by 1
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Jun 2022 18:58:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/SQL-AVG-Function-Case-when-on-selective-rows/m-p/818677#M20380</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2022-06-16T18:58:10Z</dc:date>
    </item>
    <item>
      <title>Re: SQL AVG Function Case when on selective rows</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/SQL-AVG-Function-Case-when-on-selective-rows/m-p/818736#M20381</link>
      <description>&lt;P&gt;Follow up question&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to sum the prior 12 month sales , however there would be scenarios where there are less than 12 months, then I'd like to annualize the sales&amp;nbsp; based on the # of months. I do have a record on how many months they have been on book.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code I have below which does not work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;,sum(case when max(mob)&amp;gt;=12 and yyyymm&amp;gt;="&amp;amp;pre12m." then sum(sales)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;when max(mob)&amp;lt;12 and yyyymm&amp;gt;="&amp;amp;pre12m." then (sum(sales)/max(mob))*12&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else 0 end) as Pre12m_sales_annualized&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2022 00:32:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/SQL-AVG-Function-Case-when-on-selective-rows/m-p/818736#M20381</guid>
      <dc:creator>superking</dc:creator>
      <dc:date>2022-06-17T00:32:43Z</dc:date>
    </item>
    <item>
      <title>Re: SQL AVG Function Case when on selective rows</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/SQL-AVG-Function-Case-when-on-selective-rows/m-p/818737#M20382</link>
      <description>&lt;P&gt;thank you, this type of syntax is foreign to me...&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2022 00:42:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/SQL-AVG-Function-Case-when-on-selective-rows/m-p/818737#M20382</guid>
      <dc:creator>superking</dc:creator>
      <dc:date>2022-06-17T00:42:47Z</dc:date>
    </item>
    <item>
      <title>Re: SQL AVG Function Case when on selective rows</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/SQL-AVG-Function-Case-when-on-selective-rows/m-p/818738#M20383</link>
      <description>&lt;P&gt;That would be :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;, 12 * avg(case when yyyymm&amp;gt;="&amp;amp;pre12m." then sales else . end) as Pre12m_sales_annualized&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2022 00:59:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/SQL-AVG-Function-Case-when-on-selective-rows/m-p/818738#M20383</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2022-06-17T00:59:41Z</dc:date>
    </item>
    <item>
      <title>Re: SQL AVG Function Case when on selective rows</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/SQL-AVG-Function-Case-when-on-selective-rows/m-p/818740#M20384</link>
      <description>&lt;P&gt;perfect, thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2022 01:38:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/SQL-AVG-Function-Case-when-on-selective-rows/m-p/818740#M20384</guid>
      <dc:creator>superking</dc:creator>
      <dc:date>2022-06-17T01:38:05Z</dc:date>
    </item>
  </channel>
</rss>

