You've now clarified your object. For any pair of consecutive observations within an ID, you want to assign flag=1 for both of those observations if they have at least 2 variables with consecutive values of 1.
This is a single step solution that, for each ID, reads the series of observations twice. In the first pass, each record F=1,2,3, ... is compared to the prior record and a temporary variable consec_ones{F} counts the number of vars with a value of 1 in the current and prior record.
The second pass, rereads the series, and for each record (S=1,2,3) and if the current record has consec_ones>1 or the next record has consec_ones > 1 then set the flag:
data have;
input id $ year month var1-var7 ;
datalines;
1 2012 7 0 0 0 0 1 1 1
1 2012 8 0 0 0 0 0 1 1
1 2012 9 1 0 0 0 0 0 0
1 2012 10 0 0 1 0 0 0 0
1 2012 11 0 1 0 1 0 1 0
1 2012 12 0 0 0 0 0 0 1
1 2013 1 0 0 1 1 1 0 0
2 2012 6 0 0 1 1 1 0 0
2 2012 7 0 0 0 0 0 0 0
2 2012 8 0 0 0 0 0 0 0
2 2012 9 0 0 0 0 0 0 0
2 2012 10 0 0 0 0 0 0 0
2 2012 11 0 0 0 0 0 0 0
2 2012 12 0 0 0 1 0 1 0
2 2013 1 0 0 1 1 1 0 0
2 2013 3 1 1 0 0 0 0 0
3 2012 7 0 0 0 0 1 1 1
3 2012 8 0 0 0 0 0 0 0
3 2012 9 0 0 0 0 0 0 0
3 2012 10 0 0 1 0 0 0 0
4 2012 11 0 1 0 0 0 1 0
4 2012 12 0 0 0 0 0 0 0
4 2013 1 1 0 0 0 0 1 0
4 2013 4 0 0 1 1 0 0 0
run;
data want (drop=v f s);
set have (in=firstpass)
have (in=secondpass);
by id;
array consec_ones{30} _temporary_;
array var{*} var: ;
if first.id then call missing(F,S,of consec_ones{*});
if firstpass then do;
F+1;
consec_ones{F}=0;
do v=1 to dim(var);
if dif(var{v})=0 and var{v}=1 then consec_ones{F}+1;
end;
end;
if first.id then consec_ones{1}=0;
if secondpass;
S+1;
flag= max(consec_ones{S},consec_ones{S+1})>1;
run;
Edit: I added the if first.id then consec_ones{1}=0; statement to eliminate artificial creation of consec_one{1} from comparing the beginning of one ID to its predecessor at the end of the previous ID.
Notes:
Set the size of the array consec_ones to at least 1 greater than the largest number of observations expected for any ID. I used 30, meaning I expected no more that 29 records for any ID.
F=1,2,3,... for record sequence within the first pass through an ID. S=1,2,3 for the same during the second pass.
The DIF(x) function is equivalent to X-lag(X). So if DIF(x)=0 and the current value of X=1, then you've revealed consecutive 1's.
The major "trick" here is to interleave dataset HAVE with itself - the pair of statements SET HAVE (in=firstpass) HAVE (in=secondpass); BY ID; .
... View more