My solution would have been shorter:
data snowforecast;
set winter2015_2016;
retain FirstSnow;
by Code;
if first.Code then FirstSnow=Date;
if last.Code;
LastSnow=Date;
WinterLengthWeeks=intck('week', FirstSnow, LastSnow, 'c');
ProjectedFirstSnow=intnx('year', FirstSnow, 1, 'same');
format FirstSnow LastSnow ProjectedFirstSnow date7.;
drop Snow Date;
run;
The code will read all observations, but keep only the value of the first observation in a group in variable firstsnow.
Then, it will only proceed further if the last observation in a group has been read. All following statements (including the implicit OUTPUT at the end of each step iteration) are "jumped over" in all other cases.
This particular form of an IF statement is called "Subsetting IF"; if the condition is not met, the next iteration of the DATA step is immediately started.
... View more