<?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 How do I calculate cumulative average for past 30 mins in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/How-do-I-calculate-cumulative-average-for-past-30-mins/m-p/26829#M6156</link>
    <description>Hello,&lt;BR /&gt;
  I have a dataset with variable TICODE(code of the product),TRADETSTMP(time recorded) ,UNIQ_KEY(unique number for the record) and RETURN(value for that unique key)..I need to develop a code using Base SAS to generate average of RETURN of all observations in last 30 mins (moving average)..I do have a code which is doing the work for me but am not sure if its the ideal or optimised code...I request u to kindly suggest me a better more optimised code for the same&lt;BR /&gt;
Here is my code :-&lt;BR /&gt;
&lt;BR /&gt;
libname ts 'd:\Priyank\SASDatasets';&lt;BR /&gt;
data ts.samp;&lt;BR /&gt;
	set ts.gb0030738610;&lt;BR /&gt;
	where ticode = "GB0030738610";&lt;BR /&gt;
	if return=. then return=0;&lt;BR /&gt;
	rename uniq_key=uniq_key1;&lt;BR /&gt;
run;&lt;BR /&gt;
data ts.samp1;&lt;BR /&gt;
set ts.gb0030738610;&lt;BR /&gt;
where ticode = "GB0030738610";&lt;BR /&gt;
if lag(TRADETSTMP) ^=. then do;&lt;BR /&gt;
	do i=1800 to 0 by -1;&lt;BR /&gt;
		dt=intnx('second',TRADETSTMP,-i,'same');&lt;BR /&gt;
		output;&lt;BR /&gt;
	end;&lt;BR /&gt;
end;&lt;BR /&gt;
else output;&lt;BR /&gt;
format dt datetime.;&lt;BR /&gt;
drop return;&lt;BR /&gt;
run;&lt;BR /&gt;
proc sql;&lt;BR /&gt;
create table ts.samp2 as&lt;BR /&gt;
select a.* , b.return, b.uniq_key1 from ts.samp1 as a left join ts.samp as b on a.ticode=b.ticode and a.dt=b.TRADETSTMP&lt;BR /&gt;
order by TICODE,TRADETSTMP,DATE,TIME,Price,UNIQ_KEy;&lt;BR /&gt;
quit;&lt;BR /&gt;
data ts.samp2;&lt;BR /&gt;
set ts.samp2;&lt;BR /&gt;
if dt=. and i=. then return=0;&lt;BR /&gt;
if ((dt=. and i=. and UNIQ_KEy1=.) or UNIQ_KEy1^=. )and uniq_key1 &amp;lt;= uniq_key then output;&lt;BR /&gt;
run;&lt;BR /&gt;
data ts.samp3;&lt;BR /&gt;
set ts.samp2;&lt;BR /&gt;
ct=1;&lt;BR /&gt;
if return ^=. then output;&lt;BR /&gt;
/*drop dt i;*/&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
create table ts.samp4 as&lt;BR /&gt;
select TICODE,TRADETSTMP,DATE,TIME,Price,UNIQ_KEy, sum(ct) as ct, sum(return)/sum(ct) as avg  format=8.5&lt;BR /&gt;
from ts.samp3&lt;BR /&gt;
group by TICODE,TRADETSTMP,DATE,TIME,Price,UNIQ_KEy ;&lt;BR /&gt;
quit;</description>
    <pubDate>Tue, 25 May 2010 05:47:10 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2010-05-25T05:47:10Z</dc:date>
    <item>
      <title>How do I calculate cumulative average for past 30 mins</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-do-I-calculate-cumulative-average-for-past-30-mins/m-p/26829#M6156</link>
      <description>Hello,&lt;BR /&gt;
  I have a dataset with variable TICODE(code of the product),TRADETSTMP(time recorded) ,UNIQ_KEY(unique number for the record) and RETURN(value for that unique key)..I need to develop a code using Base SAS to generate average of RETURN of all observations in last 30 mins (moving average)..I do have a code which is doing the work for me but am not sure if its the ideal or optimised code...I request u to kindly suggest me a better more optimised code for the same&lt;BR /&gt;
Here is my code :-&lt;BR /&gt;
&lt;BR /&gt;
libname ts 'd:\Priyank\SASDatasets';&lt;BR /&gt;
data ts.samp;&lt;BR /&gt;
	set ts.gb0030738610;&lt;BR /&gt;
	where ticode = "GB0030738610";&lt;BR /&gt;
	if return=. then return=0;&lt;BR /&gt;
	rename uniq_key=uniq_key1;&lt;BR /&gt;
run;&lt;BR /&gt;
data ts.samp1;&lt;BR /&gt;
set ts.gb0030738610;&lt;BR /&gt;
where ticode = "GB0030738610";&lt;BR /&gt;
if lag(TRADETSTMP) ^=. then do;&lt;BR /&gt;
	do i=1800 to 0 by -1;&lt;BR /&gt;
		dt=intnx('second',TRADETSTMP,-i,'same');&lt;BR /&gt;
		output;&lt;BR /&gt;
	end;&lt;BR /&gt;
end;&lt;BR /&gt;
else output;&lt;BR /&gt;
format dt datetime.;&lt;BR /&gt;
drop return;&lt;BR /&gt;
run;&lt;BR /&gt;
proc sql;&lt;BR /&gt;
create table ts.samp2 as&lt;BR /&gt;
select a.* , b.return, b.uniq_key1 from ts.samp1 as a left join ts.samp as b on a.ticode=b.ticode and a.dt=b.TRADETSTMP&lt;BR /&gt;
order by TICODE,TRADETSTMP,DATE,TIME,Price,UNIQ_KEy;&lt;BR /&gt;
quit;&lt;BR /&gt;
data ts.samp2;&lt;BR /&gt;
set ts.samp2;&lt;BR /&gt;
if dt=. and i=. then return=0;&lt;BR /&gt;
if ((dt=. and i=. and UNIQ_KEy1=.) or UNIQ_KEy1^=. )and uniq_key1 &amp;lt;= uniq_key then output;&lt;BR /&gt;
run;&lt;BR /&gt;
data ts.samp3;&lt;BR /&gt;
set ts.samp2;&lt;BR /&gt;
ct=1;&lt;BR /&gt;
if return ^=. then output;&lt;BR /&gt;
/*drop dt i;*/&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
create table ts.samp4 as&lt;BR /&gt;
select TICODE,TRADETSTMP,DATE,TIME,Price,UNIQ_KEy, sum(ct) as ct, sum(return)/sum(ct) as avg  format=8.5&lt;BR /&gt;
from ts.samp3&lt;BR /&gt;
group by TICODE,TRADETSTMP,DATE,TIME,Price,UNIQ_KEy ;&lt;BR /&gt;
quit;</description>
      <pubDate>Tue, 25 May 2010 05:47:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-do-I-calculate-cumulative-average-for-past-30-mins/m-p/26829#M6156</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-05-25T05:47:10Z</dc:date>
    </item>
    <item>
      <title>Re: How do I calculate cumulative average for past 30 mins</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-do-I-calculate-cumulative-average-for-past-30-mins/m-p/26830#M6157</link>
      <description>It seems that your code does more calculations than the text describes. &lt;BR /&gt;
It's hard to give optimization tips when you don't know all the rules.&lt;BR /&gt;
&lt;BR /&gt;
/Linus</description>
      <pubDate>Tue, 25 May 2010 10:34:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-do-I-calculate-cumulative-average-for-past-30-mins/m-p/26830#M6157</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2010-05-25T10:34:30Z</dc:date>
    </item>
  </channel>
</rss>

