Add a counter that starts with every permno:
%macro test;
data want;
set have;
by permno;
if first.permno
then count = 1;
else count + 1;
%do j = 1 to &varnum.;
%do i = 1 %to 12;
if fyr = &i. and count >= &i. + 4 then &&var&j. = lag%evalf(&i.+3)(&&var&j.);
%end;
%end;
run;
%mend;
... View more