Search code examples
sas

Trying to create a flag for data that has the same numerical format for a 15 month period over 96 months


I have a numerical dataset with 96 consecutive months and I need to generate a variable called flag for each numerical ID(this variable is distinct), with 1 if they have 3 consecutive 'zeros' followed by 9 consecutive 'non zeros' and another 3 consecutive 'zeros'. Example below

0,0,0,100,200,40,50,40,300,500,50,30,0,0,0

if any ID's follow the format above at any time during the 96 month period I would like a variable called flag to equal 1 and 0 if it doesnt follow that format.

I have started with the code below

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

;


Solution

  • Really hard code:

    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;