I found another problem for the situation when there is more than one quarter of missing data between valid observations. In this case, the counter did not augment the date variable correctly. the following code works.
data CPI;
infile cards;
input @;
_infile_ = translate(_infile_,'Q',':');
input Date :yyq. State:$1. CPI;
format date yyq.;
cards;
1976:1 CA 100
1976:2 CA 101
1976:3 CA 102
1976:4 CA 103
1977:1 CA 104
1977:3 CA 106
1977:4 CA 107
1976:1 PA 100
1976:4 PA 103
1977:1 PA 104
1977:3 PA 106
1977:4 PA 107
;;;;
run;
proc print;
run;
data CPI;
set CPI end=eof;
by state date;
if not eof then set CPI(firstobs=2 keep=date rename=date=nextQ);
q = ifN(not last.state,intCK('QTR',date,nextQ),1);
do i = 1 to q;
if i=1 then date = intNX('QTR',date,0);
if i>1 then date = intNX('QTR',date,1);
CPI_L1 = lag1(CPI);
if (first.state and i=1) then CPI_L1 = .;
output;
CPI = .;
end;
*drop nextQ q i;
run;