@RandyStan:
If the general pattern of your input data set is exactly as you've shown - in other words, the data variations whose possibility @Tom has sagely spelled out do not apply, you can simplify your program logic by eliminating the duplicates by DATE (again, @Tom's suggestion), creating the required variables for the unduplicated dates, and merging the results back with the original data. It can be done as a multi-step process creating a separate data set at every step. Below, it's done in a single step via a hash table made use of two things at once: (a) auto-eliminating the duplicates and (b) flagging the required items which are then merged with the original data.
data have ;
input date :mmddyy10. vara varb ;
format date yymmdd10. ;
cards;
6/20/2019 . .
6/20/2019 . .
6/20/2019 . .
6/20/2019 . .
6/21/2019 . .
6/21/2019 . .
6/21/2019 . .
6/22/2019 . 1
6/22/2019 . 1
6/22/2019 . 1
6/22/2019 . 1
6/23/2019 1 .
6/23/2019 1 .
6/23/2019 1 .
6/24/2019 . .
6/24/2019 . .
6/24/2019 . .
6/24/2019 . .
6/26/2019 . .
6/26/2019 . .
6/27/2019 . .
6/27/2019 . .
6/27/2019 . .
6/28/2019 . .
;
run ;
data want (drop = _:) ;
if _n_ = 1 then do ;
dcl hash h (ordered:"a") ;
h.definekey ("date") ;
h.definedata ("date", "vara", "varb", "vara_bf", "varb_bf") ;
h.definedone () ;
dcl hiter hi ("h") ;
do until (z) ;
set have end = z ;
h.ref() ;
end ;
do _q = 1 by 1 while (hi.next() = 0) ; link fill ; end ;
do _q = 1 by 1 while (hi.prev() = 0) ; link fill ; end ;
end ;
set have ;
h.find() ;
return ;
fill: if _q = 1 then call missing (_qa, _qb) ;
if vara then _qa = _q ;
if varb then _qb = _q ;
if _qa then if _q <= _qa + 2 then vara_bf = 1 ;
if _qb then if _q <= _qb + 2 then varb_bf = 1 ;
h.replace() ;
return ;
run ;
Kind regards
Paul D.
... View more