我有一个连续 96 个月的数字数据集,我需要为每个数字 ID 生成一个名为 flag 的变量(该变量是不同的),如果它们有 3 个连续的“零”,后跟 9 个连续的“非零”,则为 1 3 个连续的“零”。下面的例子
0,0,0,100,200,40,50,40,300,500,50,30,0,0,0
如果任何 ID 在 96 个月期间的任何时间遵循上述格式,我希望名为 flag 的变量等于 1 和 0(如果它不遵循该格式)。
我从下面的代码开始
data want;
set have;
array pays(*) monthly_pay_201501--monthly_pay_202212;
array months(*) month1-month96;
* populate with first day of each month extracted from var name;
do _i = 1 to 96;
months(_i) = input(scan(vname(pays(_i)), -1, '_'), yymmn6.);
end;
* ensure the dates are properly bounded;
format strt_dt end_dt date9.;
strt_dt = intnx('month', max(startdate, '1jan2015'd), 0);
end_dt = intnx('month', min(enddate, '31dec2022'd), 0);
* find the index of months array that matches the dates;
strt_indx = whichn(strt_dt, of months(*));
end_indx = whichn(end_dt, of months(*));
* validate the indices accurately reflect the true var;
strt_vname = vname(pays(strt_indx));
end_vname = vname(pays(end_indx));
do _i = strt_indx to end_indx;
#think i need to do some sort if statement here ?
end;
drop _: monthly_pay: month:;
run
;
真正的硬代码:
flag=0;
if end_indx-strt_indx>=15 then do _i = strt_indx to end_indx-14 until(flag=1);
if pays[_i]=0
and pays[_i+1]=0
and pays[_i+2]=0
and pays[_i+3]^=0
and pays[_i+4]^=0
and pays[_i+5]^=0
and pays[_i+6]^=0
and pays[_i+7]^=0
and pays[_i+8]^=0
and pays[_i+9]^=0
and pays[_i+10]^=0
and pays[_i+11]^=0
and pays[_i+12]=0
and pays[_i+13]=0
and pays[_i+14]=0
then flag=1;
end;