I have a table with columns for person_id, Encounter_Date, Diagnosis1-10, and DxCode. How can i flag the first DxCode that starts with "S06" for each separate person_id and Encounter_Date?
Thanks,
Brian
Here's what I think you are looking for:
proc sort data=have; by person_id Encounter_Date; run;
data want;
set have;
by person_id Encounter_Date;
if first.Encounter_Date then do;
flag=0;
S06_found=0;
end;
retain flag S06_found;
if DxCode =: 'S06' then do;
S06_found + 1;
if S06_found=1 then flag=1;
else flag=0;
end;
else flag=0;
drop S06_found;
run;
It gets a little tricky because there can be multiple, different Dx Codes that begin with S06 on the same date.
If you don't mind sorting your data:
proc sort data=have; by person_id Encounter_Date DxCode; run;
data want;
set have;
by person_id Encounter_Date DxCode;
flag = first.DxCode and DxCode =: "S06";
run;
PG,
Thanks for the reply. I was able to get it to work with some minor tweaks (needed because i didn't explain my problem well enough).
Brian
Starts with makes it a bit more difficult.
I think you'll have to create an array of the diags and loop over them unfortunately. If it was full code I would use WHICHC Function.
Here's what I think you are looking for:
proc sort data=have; by person_id Encounter_Date; run;
data want;
set have;
by person_id Encounter_Date;
if first.Encounter_Date then do;
flag=0;
S06_found=0;
end;
retain flag S06_found;
if DxCode =: 'S06' then do;
S06_found + 1;
if S06_found=1 then flag=1;
else flag=0;
end;
else flag=0;
drop S06_found;
run;
It gets a little tricky because there can be multiple, different Dx Codes that begin with S06 on the same date.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.