You can
Concatenate all the 1's and 0's in the YEAR vars into a single character variable (_strng).
Trim leading and trailing 0's from _strng, so that all remaining zeroes must be between 1's.
Then
Treating 1's as word separators, count the number of "words" (NGAPS)
Get total number of zeroes (compress out the 1's, get the length -->TOTAL_GAP_LENGTH).
Calculate the average gap length.
data my_data;
input KEY YEAR97 YEAR98 YEAR99 YEAR00 YEAR01 YEAR02 YEAR03 YEAR04 YEAR05 YEAR06 YEAR07 YEAR08 YEAR09;
datalines;
1 0 0 1 0 0 1 0 1 0 0 1 0 0
2 1 1 0 1 1 0 1 0 1 1 0 1 1
3 0 0 0 0 1 0 0 0 1 0 0 1 0
4 1 1 1 1 1 1 1 1 1 1 1 1 1
;
data want (drop=_:);
set my_data;
length _strng $14; *Assign a length at least one byte longer than N of YEAR variables *;
_strng=cats(of year:);
_strng=substr(_strng,findc(_strng,'1')); *trim leading zeroes;
_last1=findc(_strng,'1',-14); *Find RIGHTMOST '1', note negative 3rd parameter;
_strng=substr(_strng,1,_last1); *trim trailing zeroes*;
ngaps=countw(_strng,'1')-1;
total_gap_length=lengthn(compress(_strng,'1'));
if ngaps>0 then average_gap_length=total_gap_length/ngaps;
run;
... View more