@Manikanta wrote:
2. Can we always count of the first instance of a patient ID to be a visit type of SCR? If not, can the data be sorted in that fashion?
Ans: Patient ID is sorted based on the date ,So SCR can occur at any instance .
Please add that case to your example data to explain what you expect as result.
In the meantime i created something really ugly. I am sure that the code will be easier to maintain by using two dow-loops - the first to find the scr-observation for a patient and the second to fill minimum and visit_min.
data want;
set one;
by pt;
length
minimum 8 visit_min $ 3
bakMinimum scrSum 8 bakVisit $ 3
rc 8
;
retain bak: scrSum;
drop bak: scrSum rc;
if _n_ = 1 then do;
declare hash h(dataset: 'work.one(keep= pt sum visit rename=(sum=minimum) where=(visit = "scr"))');
h.defineKey('pt');
h.defineData('minimum');
h.defineDone();
end;
if first.pt then do;
bakMinimum = sum;
if visit = "scr" then do;
scrSum = sum;
bakVisit = "scr";
call missing(minimum, visit_min);
end;
else do;
rc = h.find();
visit_min = "scr";
scrSum = Minimum;
bakMinimum = min(sum, minimum);
bakVisit = ifc(sum < minimum, visit, "scr");
end;
end;
else do;
if sum >= bakMinimum then do;
minimum = bakMinimum;
visit_min = bakVisit;
end;
else do;
minimum = scrSum;
visit_min = "scr";
bakMinimum = sum;
bakVisit = visit;
end;
if visit = "scr" then do;
call missing(minimum, visit_min);
end;
end;
run;
... View more