BookmarkSubscribeRSS Feed
joe66
Calcite | Level 5

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!

 

 

8 REPLIES 8
PaigeMiller
Diamond | Level 26

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...

--
Paige Miller
joe66
Calcite | Level 5
Yes, you are right. how to change my code?

Thanks!
PaigeMiller
Diamond | Level 26

@joe66 wrote:
Yes, you are right. how to change my code?

Thanks!

There are examples at the link that I provided.

--
Paige Miller
PeterClemmensen
Tourmaline | Level 20

What does your log say? Can you provide an example of what your data looks like?

 

Makes it much easier to help you 🙂

joe66
Calcite | Level 5

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 

ballardw
Super User

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;
Reeza
Super User

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!

 

 


 

Astounding
PROC Star

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1485 views
  • 3 likes
  • 6 in conversation