## How to count consecutive days?

Solved
Occasional Contributor
Posts: 15

# How to count consecutive days?

[ Edited ]

Hi,  I am pretty new to SAS programming and teaching myself out of necessity. I am trying to figure out the consecutive days that mean daily water temperature remained below a threshold temperature (8 degrees Celsius).

Here is an example of what my data looks like:

Minimum  Maximum   Mean  Count   Station Date

4.1            10.6            7.9         1        20160102

4.2             9.1             7.6         1        20160103

4.0             8.9             7.3         1        20160104

4.0             9.0             7.4         1        20160105

3.9             8.9             7.0         1        20160106

4.1             9.2             7.9         1        20160107

4.4            10.4            7.8         1        20160108

For station date, the date is set up as yyyymmdd.  This can be changed if need be, this is just how we input dates in our database.  Minimum, maximum, and mean all refer to water temperature

I've already separated all of the mean water temperature data below 8 o C, and all I want are the maximum consecutive days for each year  (meaning the maximum number of CONSECUTIVE days that the temperature was below 8 o C.  My year data range from 1991-2017.

I've tried out some code, but nothing has been very successful yet.  I tried modifying this tutorial :http://support.sas.com/kb/33/838.html, but I'm having trouble discerning where parts of the data example come from.

This is the data I want:

Year        Maximum Consecutive Days

1991        20

1992        34

1993        42

1994        26

(for example).  What kind of procedure can I use to achieve this?

Accepted Solutions
Solution
‎02-05-2018 01:49 PM
Posts: 2,989

## Re: How to count consecutive days?

Posted in reply to jswinford

Assuming your dates are actual SAS dates and not character strings:

``````data want;
set have;    year=year(station_date);
if month(station_date)=1 and day(station_date)=1 then consec=0;
if mean<8 then consec+1;
else if mean>=8 then consec=0;
run;``````

Then you can use PROC SUMMARY to obtain the max values of variable CONSEC in each year;

--
Paige Miller

All Replies
Solution
‎02-05-2018 01:49 PM
Posts: 2,989

## Re: How to count consecutive days?

Posted in reply to jswinford

Assuming your dates are actual SAS dates and not character strings:

``````data want;
set have;    year=year(station_date);
if month(station_date)=1 and day(station_date)=1 then consec=0;
if mean<8 then consec+1;
else if mean>=8 then consec=0;
run;``````

Then you can use PROC SUMMARY to obtain the max values of variable CONSEC in each year;

--
Paige Miller
Super User
Posts: 23,683

## Re: How to count consecutive days?

Posted in reply to jswinford

Are you doing a degree day type calculation?

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
• 2 replies
• 368 views
• 0 likes
• 3 in conversation