Hi all,
I would like to select the patients with mental health diagnosis using ICD10 code. There were up to 30 diagnosis variables for each patient, I will define the patients having mental health diagnosis if the first letter of any 30 diagnosis variables is 'F', but it seems my code resulted in all missing in mental_flag, why?
array dx(30) diag1-diag30;
do i=1 to 30;
if substr(dx(i),1,1)='F' then mental_flag=1;
else if substr(dx(i),1,1) notin ("F","") then mental_flag=0;
else if substr(dx(i),1,1) in ("") then mental_flag=.;
end;
drop i;
Thanks!
Your code works like this:
If you find a variable whose value starts with "F", the flag gets set to 1, and then the loop continues searching and if the next variable (or any subsequent variable) does not start with "F", then the flag is set back to 0 or missing. You probably want to stop the loop immediately upon finding an "F".
Perhaps easier is to use the WHICHC function and then no looping is needed. https://documentation.sas.com/?cdcId=pgmmvacdc&cdcVersion=9.4&docsetId=lefunctionsref&docsetTarget=p...
@joe66 wrote:
Yes, you are right. how to change my code?
Thanks!
There are examples at the link that I provided.
What does your log say? Can you provide an example of what your data looks like?
Makes it much easier to help you 🙂
id diag1 diag2 diag3 diag4 diag5............................diag30
1 A11 A12 A13 A14 F10 A12
2 B11 B10 B12 B17 B19 B20
3 C01 C12 C11 F02 C15
.
.
.
.
.
.
.
My data looks like the above.
Thanks
If 'F' only occurs at the start of the codes, my limited knowledge of ICD-10 makes me think this might be the case then this may work:
data want; set have; Flag =( index(cat(of diag:),'F')>0); run;
do i=1 to 30;
if substr(dx(i),1,1)='F' then mental_flag=1;
else if substr(dx(i),1,1) notin ("F","") then mental_flag=0;
You reset the flag. If it was already set, so diag1 = F10 and diag2=A30 what happens?
So first diagnosis you set mental_flag=1 and second diagnosis you reset it back to zero. Remove the ELSE statements is all you need to do.
I suspect your log is also flagging NOTIN as a problem as well.
@joe66 wrote:
Hi all,
I would like to select the patients with mental health diagnosis using ICD10 code. There were up to 30 diagnosis variables for each patient, I will define the patients having mental health diagnosis if the first letter of any 30 diagnosis variables is 'F', but it seems my code resulted in all missing in mental_flag, why?
array dx(30) diag1-diag30;
do i=1 to 30;
if substr(dx(i),1,1)='F' then mental_flag=1;
else if substr(dx(i),1,1) notin ("F","") then mental_flag=0;
else if substr(dx(i),1,1) in ("") then mental_flag=.;
end;
drop i;
Thanks!
Just in case you are still looking for another way:
array dx(30) diag1-diag30;
mental_flag=0;
do i=1 to 30 until (mental_flag=1);
if dx(i)=:'F' then mental_flag=1;
end;
drop i;
The search stops, once a code beginning with "F" has been found. Don't forget the colon after the equal sign.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.