@mkeintz Thanks! Your code actually worked. I ran each line seperately and noticed that I need to change wgt2 into wgt1 in the line: wgt2=coalesce(weight,wgt2). When I did that I got what I wanted. For the future reference, I used your following code: data want ; merge have (keep=id dateoftest diagnosis rename=(dateoftest=diag_date) where=(diagnosis=1)) have; by id; set have (firstobs=2 keep=id dateoftest rename=(id=nxt_id dateoftest=nxt_date)); retain wgt1 wgt2 .; if first.id then call missing(of wgt:); if intnx('month',diag_date,-4,'sameday') <= dateoftest <= intnx('month',diag_date,-2,'sameday'); if wgt1=. then wgt1=weight; else wgt2=coalesce(weight,wgt1); * if intck('month',nxt_date,diag_date,'c')>2 **See editted note below**; if nmiss(wgt1,wgt2)=0 then wgt_change_ratio= wgt2/wgt1-1; if wgt_change_ratio ^=. and wgt_change_ratio <= -0.1 then flag = 1; else flag = 0; run; That was super helpful. Thank you so much for your help @mkeintz. Best, Sandyzman1
... View more