<?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 Average of every n rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Average-of-every-n-rows/m-p/495094#M130601</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data set that has a measure of the temperature every 10 minutes (variable = temp) for a month. I would like to get an average of every 6 rows combined so I can calculate the average temperature&amp;nbsp;per hour.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;An example of the first bit of my data is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="sasSource"&gt;data WORK.WEATHER;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;infile datalines dsd truncover;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;input date: DDMMYY10. time:TIME. temp:32.;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;format date DDMMYY10. time TIME.;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;datalines;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 7:00:00 18.461&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 7:10:00 18.557&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 7:20:00 18.675&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 7:30:00 18.557&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 7:40:00 18.461&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 7:50:00 18.319&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 8:00:00 18.39&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 8:10:00 18.652&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 8:20:00 19.246&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 8:30:00 19.46&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 8:40:00 19.793&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 8:50:00 20.293&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;I would like it to look like this:&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&lt;SPAN&gt;04/08/2018 7:00:00 18.505&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&lt;SPAN&gt;04/08/2018 8:00:00&amp;nbsp;19.306&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;Thank you!&lt;/DIV&gt;</description>
    <pubDate>Thu, 13 Sep 2018 00:10:09 GMT</pubDate>
    <dc:creator>pvm90</dc:creator>
    <dc:date>2018-09-13T00:10:09Z</dc:date>
    <item>
      <title>Average of every n rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-of-every-n-rows/m-p/495094#M130601</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data set that has a measure of the temperature every 10 minutes (variable = temp) for a month. I would like to get an average of every 6 rows combined so I can calculate the average temperature&amp;nbsp;per hour.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;An example of the first bit of my data is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="sasSource"&gt;data WORK.WEATHER;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;infile datalines dsd truncover;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;input date: DDMMYY10. time:TIME. temp:32.;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;format date DDMMYY10. time TIME.;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;datalines;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 7:00:00 18.461&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 7:10:00 18.557&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 7:20:00 18.675&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 7:30:00 18.557&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 7:40:00 18.461&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 7:50:00 18.319&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 8:00:00 18.39&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 8:10:00 18.652&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 8:20:00 19.246&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 8:30:00 19.46&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 8:40:00 19.793&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;04/08/2018 8:50:00 20.293&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;I would like it to look like this:&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&lt;SPAN&gt;04/08/2018 7:00:00 18.505&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&lt;SPAN&gt;04/08/2018 8:00:00&amp;nbsp;19.306&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;Thank you!&lt;/DIV&gt;</description>
      <pubDate>Thu, 13 Sep 2018 00:10:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-of-every-n-rows/m-p/495094#M130601</guid>
      <dc:creator>pvm90</dc:creator>
      <dc:date>2018-09-13T00:10:09Z</dc:date>
    </item>
    <item>
      <title>Re: Average of every n rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-of-every-n-rows/m-p/495099#M130604</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data WORK.WEATHER;
infile datalines  truncover;
input date: DDMMYY10. time:TIME. temp:32.;
format date DDMMYY10. time TIME.;
datalines;
04/08/2018 7:00:00 18.461
04/08/2018 7:10:00 18.557
04/08/2018 7:20:00 18.675
04/08/2018 7:30:00 18.557
04/08/2018 7:40:00 18.461
04/08/2018 7:50:00 18.319
04/08/2018 8:00:00 18.39
04/08/2018 8:10:00 18.652
04/08/2018 8:20:00 19.246
04/08/2018 8:30:00 19.46
04/08/2018 8:40:00 19.793
04/08/2018 8:50:00 20.293
;

data w;
set weather;
if minute(time)=0 then grp+1;
run;

proc sql;
create table want(drop=grp) as
select date, time, mean(temp) as avg_temp
from w
group by date,grp
having time=min(time);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Sep 2018 00:19:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-of-every-n-rows/m-p/495099#M130604</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-09-13T00:19:58Z</dc:date>
    </item>
    <item>
      <title>Re: Average of every n rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-of-every-n-rows/m-p/495103#M130606</link>
      <description>&lt;P&gt;Thank you so much! That is perfect.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Sep 2018 00:23:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-of-every-n-rows/m-p/495103#M130606</guid>
      <dc:creator>pvm90</dc:creator>
      <dc:date>2018-09-13T00:23:37Z</dc:date>
    </item>
    <item>
      <title>Re: Average of every n rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-of-every-n-rows/m-p/495116#M130612</link>
      <description>&lt;P&gt;I'd suggest a modification. Use the HOUR() function on the time variable and then you can have the hour as well with your summaries.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
set have;
hour = hour(time);
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Sep 2018 00:59:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-of-every-n-rows/m-p/495116#M130612</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-09-13T00:59:41Z</dc:date>
    </item>
    <item>
      <title>Re: Average of every n rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-of-every-n-rows/m-p/495436#M130754</link>
      <description>&lt;P&gt;Another approach using a DATETIME value and formats:&lt;/P&gt;
&lt;PRE&gt;data WORK.WEATHER;
   infile datalines  truncover;
   input date: DDMMYY10. time:TIME. temp:32.;
   format date DDMMYY10. time TIME.;
   dt = dhms(date,0,0,time);
   format dt datetime18.;
datalines;
04/08/2018 7:00:00 18.461
04/08/2018 7:10:00 18.557
04/08/2018 7:20:00 18.675
04/08/2018 7:30:00 18.557
04/08/2018 7:40:00 18.461
04/08/2018 7:50:00 18.319
04/08/2018 8:00:00 18.39
04/08/2018 8:10:00 18.652
04/08/2018 8:20:00 19.246
04/08/2018 8:30:00 19.46
04/08/2018 8:40:00 19.793
04/08/2018 8:50:00 20.293
;

proc summary data=work.weather nway;
   class dt;
   format dt datetime10.;
   var temp;
   output out=work.weathermean (drop=_:) mean=;
run;&lt;/PRE&gt;
&lt;P&gt;The output data set will have the first datetime value that rounds to an hour by using the datetime10 format. Change the format in other displays if you want to see it appear differently.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or no added variable just a different format for the Time variable:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc summary data=work.weather nway;
   class date time;
   format time time2.;
   var temp;
   output out=work.weathermean2 (drop=_:) mean=;
run;&lt;/PRE&gt;
&lt;P&gt;Again, you can change the time variable format in other displays to get back to an hh:mm:ss appearance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Either of these will also work if you have more or fewer records within the hour.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Sep 2018 17:57:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-of-every-n-rows/m-p/495436#M130754</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-09-13T17:57:25Z</dc:date>
    </item>
  </channel>
</rss>

