BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
KALLEN
Obsidian | Level 7

How do I count how many consecutive days are 90 degrees or over each August (i.e. 19 consecutive days over 90 in August 1999)?

 

i.e. "What is longest sequence of consecutive days with temperature greater than 90?"

 

 

My code: 

 

data demo5.aug_weather_consec;
    set demo5.aug_weather1;   
	if Max_Temp >= 90 then consec+1;
	else if Max_Temp < 90 then consec=0;
run;

proc means data=demo5.aug_weather_consec max;
	var consec; 
	by date;
run;

Desired result: 

 

August Consecutive Days at/or Above 90
August, YearAugust Consecutive Days at/or Above 90
199918
200016
20019
200227
200315
200413
20056
20067
200726
20086
200923
201023
201116
201216
201313
20147
201530
201612
201717
201819

 

I have attached my dataset.

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19
data aug;
   infile aug;
   input date :date. degrees;
   year = year(date);
   gt90 = degrees ge 90;
   format date date11.;
   run;
proc summary nway data=aug;
   by gt90 notsorted;
   class date;
   format date monyy7.;
   output out=runs(drop=_type_ rename=(_freq_=runlength) where=(gt90 eq 1)) idgroup(out(date)=start) idgroup(last out(date)=end);
   format start: end: date11.;
   run;
proc summary data=runs nway;
   class date;
   output out=maxrun(drop=_:) max(runlength)=;
   run;

Capture.PNG

 

View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26

You haven't actually asked a question.

 

Also, your title says you want consecutive days, but your first sentence doesn't say consecutive. So which is it?

--
Paige Miller
KALLEN
Obsidian | Level 7

I have revised the question.

data_null__
Jade | Level 19

I haven't answered the question you did not ask but is something I want to show you.

This outputs all 90+ days with start and end dates for consecutive runs.  _FREQ_ is the day length of the run.

 

filename aug clipbrd;
data aug;
   infile aug;
   input date :date. degrees;
   gt90 = degrees ge 90;
   format date date11.;
   run;
proc summary nway;
   by gt90 notsorted;
   class date;
   format date monyy.;
   output out=runs(drop=_type_ where=(gt90 eq 1)) idgroup(out(date)=start) idgroup(last out(date)=end);
   format start: end: date11.;
   run;
proc print;
   run;

 

Capture.PNG

ballardw
Super User

From your example data and what I think is your expected result the question you are asking is "what is longest sequence of consecutive days with temperature greater than 90". Is that  a correct interpretation?

KALLEN
Obsidian | Level 7

Yes, thank you.

data_null__
Jade | Level 19
data aug;
   infile aug;
   input date :date. degrees;
   year = year(date);
   gt90 = degrees ge 90;
   format date date11.;
   run;
proc summary nway data=aug;
   by gt90 notsorted;
   class date;
   format date monyy7.;
   output out=runs(drop=_type_ rename=(_freq_=runlength) where=(gt90 eq 1)) idgroup(out(date)=start) idgroup(last out(date)=end);
   format start: end: date11.;
   run;
proc summary data=runs nway;
   class date;
   output out=maxrun(drop=_:) max(runlength)=;
   run;

Capture.PNG

 

KALLEN
Obsidian | Level 7

This code and output look great. However, I am having trouble importing the data aug. When I run the first block of code, I get the error message below.

data aug;
   infile aug;
   input date :date. degrees;
   year = year(date);
   gt90 = degrees ge 90;
   format date date11.;
   run;

ERROR: I/O failure.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.AUG may be incomplete. When this step was stopped there were 0 observations and 4 variables.
WARNING: Data set WORK.AUG was not replaced because this step was stopped.

 

What could I be doing wrong when importing the data or refer to the dataset in the data step?

data_null__
Jade | Level 19

@KALLEN wrote:

This code and output look great. However, I am having trouble importing the data aug. When I run the first block of code, I get the error message below.

data aug;
   infile aug;
   input date :date. degrees;
   year = year(date);
   gt90 = degrees ge 90;
   format date date11.;
   run;

ERROR: I/O failure.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.AUG may be incomplete. When this step was stopped there were 0 observations and 4 variables.
WARNING: Data set WORK.AUG was not replaced because this step was stopped.

 

What could I be doing wrong when importing the data or refer to the dataset in the data step?


You still IMPORT your data the same way as you have done.  You just need to create the indicator and then run the PROC SUMMARY steps.

KALLEN
Obsidian | Level 7

Great, this worked for me - just had to alter the name of the dataset.

 

Thanks again.

KALLEN
Obsidian | Level 7

I have a similar problem, but now I need to view the longest sequence of consecutive days with temperature greater than 90° for each period Jan-Aug 1999 - 2018 (YTD).

 

Below is similar code I used from the last post, but now I need to change proc summary such that the data from Jan - Aug for runlength are added together for each year: 

 

 

/* create indicator variable for Max Temp >= 90 (gt90)  */
data ytd_weather1;
   set demo5.ytd_weather;
   gt90 = Max_Temp ge 90;
   format date date11.;
   run;

  /* create start and end dates for the beginning and end of each month */
proc summary nway data=ytd_weather1;
   by gt90 notsorted;
   class date;
   format date monyy7.;
   output out=runs(drop=_type_ rename=(_freq_=runlength) where=(gt90 eq 1)) idgroup(out(date)=start) idgroup(last out(date)=end);
   format start: end: date11.;
   run;

/* run the proc summary to view the longest sequence of consecutive days with temperature greater than 90 for each YTD AUG 1999 - 2018 */ 
proc summary data=runs nway;
   class date;
   output out=maxrun(drop=_:) max(runlength)=;
   run;

current output:

 

Daterunlength
Apr19992
May19995
Jun199910
Jul19999
Aug199918
Apr20005
May20009
Jun20008
Jul200031
Aug200016
Apr20011
May20019
Jun200113
Jul200131
Aug20019
Apr20022
May20024
Jun200224
Jul200210
Aug200228
May20038
Jun200324
Jul200328
Aug200315
May200411
Jun200421
Jul200423
Aug200413
May20057
Jun200526
Jul200526
Aug20056
Apr20062
May200614
Jun200625
Jul200619
Aug20067
May20075
Jun200718
Jul20077
Aug200727
Apr20081
May20087
Jun200823
Jul20087
Aug20086
May200912
Jun20098
Jul200931
Aug200923
May20106
Jun201028
Jul201013
Aug201023
Apr20111
May20117
Jun201130
Jul201128
Aug201117
Mar20121
Apr20126
May201215
Jun201230
Jul201215
Aug201216
Apr20132
May20137
Jun201330
Jul201312
Aug201313
May20146
Jun201430
Jul201415
Aug20147
May20153
Jun201530
Jul201531
Aug201531
May20166
Jun201625
Jul201630
Aug201612
Mar20171
Apr20172
May20174
Jun201728
Jul201715
Aug201717
Apr20182
May201814
Jun201815
Jul201821
Aug201819

 

 

 

desired output:

Daterunlength
199944
200069
200163
200268
200375
200468
200565

etc to 2018

 

Thanks.

KALLEN
Obsidian | Level 7

I had to delete obs where runlength=1 and then add the proc timeseries step to convert monthly to annual data:

/* drop any obs where runlength=1 because that is not a consecutive day >=90 */
data maxrun;
set maxrun;
if runlength=1 then delete;
run;

/*convert monthly data to yearly data by adding sum of runlength */
proc timeseries data=maxrun out=runs_ytd;
id date interval=year accumulate=total;
var runlength;
run;

This gave me the expected output.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 11 replies
  • 2910 views
  • 5 likes
  • 4 in conversation