Here is one way that works with the example data to identify first/subsequent by topic of interest
proc sort data=have;
by id diag_dt dx1-dx3;
run;
/* the sort and transpose by multiple variables is because of
/* multiple rows of data with the same date*/
/* and need to filter the output to get one ICD value per observation*/
proc transpose data=have out=trans ( where=(not missing(ICD1)) drop=_name_ dx1-dx3)
prefix=ICD;
by id diag_dt dx1-dx3;
var dx1-dx3;
run;
proc format library=work;
value $icd
'433', '434', '436', 'I63', 'I693', 'G459' = "Ischemic Stroke"
'430', '431', '4320', '4321', '4329', 'I60', 'I61', 'I62' = "Hemorrhagic Stroke"
'42731', 'I480', 'I481', 'I482', 'I489' = "AFIB"
'345', '78039', '6494', 'G40'= "Seizure"
other='N/A'
;
run;
data helpful;
set trans;
ICD_text = put(icd1,$icd.);
run;
proc sort data=helpful;
by id ICD_text diag_dt;
run;
data need;
set helpful;
by id ICD_text diag_dt;
if first.ICD_text then do;
select (Icd_text);
when ("Ischemic Stroke") First_IS=1;
when ("Hemorrhagic Stroke") First_HS=1;
when ("AFIB") First_AFIB=1;
when ("Seizure") First_Seizure=1;
otherwise;
end;
end;
else do;
select (Icd_text);
when ("Ischemic Stroke") Recurrent_IS=1;
when ("Hemorrhagic Stroke") Recurrent_HS=1;
when ("AFIB") Recurrent_AFIB=1;
when ("Seizure") Recurrent_Seizure=1;
otherwise;
end;
end;
run;
Most of the code is to get single observation per date/topic to identify "first".
But I still think the description of what you want is not clear.
Note that the format is assigning N/A to anything that you didn't mention was of interest. The values are still in the data but if they are to be used for anything likely more details are needed.
... View more