Calcite | Level 5

## How to calculate averages between dates

Dear sas users!

I have a question regarding calculating averages between dates in my dataset.

My dataset is as followed:

whereby datum= date, tijd=time, bedrijf=company, temp=temperature, luchtvochtigheid=humidity, CO2=carbon dioxide and NH3= ammonia

I have data from 12 weeks whereby every 10 minutes data from senors is logged. I have this for in total 10 companies. Now I want to calculate a 2 week average value for the 4 variables (humidity, temperature, CO2 and NH3) per company. Time is not important in this case.

so for each company I want an avarage value for every 2 weeks (thus 6 average values per company) (this is because clinical analysis to the research object were done were I want to connect climate data to).

I am not sure how to do this. Does anyone maybe have an idea?

Kind regards,

Danniek

2 REPLIES 2
Diamond | Level 26

## Re: How to calculate averages between dates

Use custom formats to identify the two week time periods, and then use PROC SUMMARY to compute the means.

EXAMPLE:

``````/* UNTESTED CODE */

proc format;
value twoweek '06MAY2022'd-'19MAY2022'd='06MAY2022'
'20MAY2022'd-'02JUN2022'd='20MAY2022' /* etc. */
;
run;

proc summary data=have way;
class bedrijf datum;
format datum twoweek.;
var temp luchtvochtigheid co2 nh3;
output out=stats mean=;
run;``````

NOTE: We cannot write code to test against data in screen captures. If you want tested code, then you need to provide the data as working SAS data step code (instructions), and not in any other format.

--
Paige Miller
Super User

## Re: How to calculate averages between dates

I have worked with data logger data similar to this. I would suggest a short investigation of your values around midnight/date change. You may have some cases where the "time" component indicates it is one of before/after midnight that doesn't quite agree with the "date" value depending on exactly how you want to consider date for the calculations.

In my case my data loggers were collecting a string of "hourly" values but had 25 hours per day: 00 to 24. The kicker was that the hour 24 values of day 1 were not the same as the hour 00 values for day 2. So some adjustments for some statistics that were really expecting 24 hourly measures had to be made.

Discussion stats
• 2 replies
• 507 views
• 2 likes
• 3 in conversation