Ah I see @NickS2 I think still hung over from last night. My apologies. Try this
data want;
do until(last.id);
set have;
by id ;
trt_flag=' ';
if visit=1 and ENROL_FLAG = "Y" then do;
trt_flag="Y";
_n_=0;
end;
else if _n_ and ENROL_FLAG = "Y" and DAY_DIFF>180 then trt_flag="Y";
if not _d and trt_flag="Y" then _d=date;
if _d and date>=_d then output;
end;
drop _d;
run;
This is exactly what I wanted. Thank you so much, you are a life saver.
please try the below code
proc sort data=have;
by id drug date;
run;
data want;
set have;
by id drug date;
if first.drug and ENROL_FLAG='Y' then TRT_FLAG=ENROL_FLAG;
if not first.drug and ENROL_FLAG='Y' and DAY_DIFF>180 then TRT_FLAG=ENROL_FLAG;
run;
Thanks Jagdish,
But youe code is giving me the same result which I am getting.
Shouldn't the visit for patient 2 on 26JUN2016 (drug B) be flagged as TRT_FLAG = Y? Your desired output lists it as blank. The first criteria is not satisfied (since the patient already has had drug B), but the second is (ENROL_FLAG = Y and DAY_DIFF > 180).
@mklangley No the second record for the patient ID 2 should be not get flagged because the first record already met the criteria.
I think the proper logic would look like this:
proc sort data=have; by id drug date; run;
data want;
retain flagged;
set have; by id drug;
if first.drug then flagged = 0;
if not flagged then if ENROL_FLAG="Y" or DAY_DIFF > 180 then do;
flagged = 1;
TRT_FLAG = "Y";
end;
drop flagged;
run;
proc sort data=want; by id date drug; run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.