DATA Step, Macro, Functions and more

Flag first diagnosis beginning with "S06"

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 126
Accepted Solution

Flag first diagnosis beginning with "S06"

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


Accepted Solutions
Solution
‎06-16-2016 05:17 PM
Super User
Posts: 5,509

Re: Flag first diagnosis beginning with "S06"

[ Edited ]

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. 

View solution in original post


All Replies
Respected Advisor
Posts: 4,925

Re: Flag first diagnosis beginning with "S06"

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
Frequent Contributor
Posts: 126

Re: Flag first diagnosis beginning with "S06"

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

Super User
Posts: 19,815

Re: Flag first diagnosis beginning with "S06"

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. 

Solution
‎06-16-2016 05:17 PM
Super User
Posts: 5,509

Re: Flag first diagnosis beginning with "S06"

[ Edited ]

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. 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 2535 views
  • 1 like
  • 4 in conversation