DATA Step, Macro, Functions and more

RECODING: Prioritizing conflicting values within the same variable

Accepted Solution Solved
Reply
Contributor
Posts: 45
Accepted Solution

RECODING: Prioritizing conflicting values within the same variable

Hello everyone!

I need some help prioritizing some recoding on my dataset. To explain the situation, allow me to showcase my dataset and code:

 

Patient ID            PRINDIAG                 DIAG1   DIAG2     DIAG3   DIAG4     DIAG5         ALLDIAG                            BH

1                              11                           12           13           13           15           16           11 12 13 13 15 16                .

2                              25                           21           13           15           16           20           25 21 13 15 16 20               1

3                              13                           12           17           18           20           10           13 12 17 18 20 10               1

4                              12                           12           15           .               .               .            12 12 15                               .

5                              31                           12           17           13            .               .            31 12 17 13                          2

6                              14                           25           11           19           30                          14 25 11 19 30                     2

 

I got this by using the following code:

 

DATA &LIB..FINAL;

                SET &LIB..FINAL;

                                ARRAY ACSC{*} PRINDIAG DIAG1-DIAG5;

                                DO I=1 TO 6;

                                ALLDIAG = CATX (‘  ‘, OF ACSC{*});

                                                IF ACSC{I} IN (20, 25) THEN BH = 1;

                                                ELSE IF ACSC{I} IN (30,31) THEN BH = 2;

                                DROP I;

                                END;

RUN;

 

I'm emphasizing row Patient "6" because that’s the one I need help with. For the recoding, I put all the diagnoses into one variable called "ALLDIAG". Based on that variable, I recode the information for variable “BH”. If you look at my program code, Patient “6” has diagnoses that includes both values for “BH”, and what I want SAS to do is to recode for the first qualified value read in “ALLDIAG”. However, what my current code does is provide the “BH” value for the last qualified value read in “ALLDIAG”. So for Patient “6”, values “25” and “30” are valid and “BH” can be recoded as “1” or “2”. “BH” does not allow for two values to be listed and so it chooses only one value. Unfortunately, what SAS does is choose the last qualified value read and recodes “BH” based on that value (in this case, “30” to recode to “2” in “BH”). What I want is for Patient “6” to have a “BH” value of “1” (for “25”) since the first diagnoses are more important.

 

Does anyone know how to instruct SAS to prioritize the diagnoses on the left above the ones on the right? The order matters and when there are conflicting values, the diagnosis farthest left should take priority.  To put it in another way, the diagnosis that comes first when reading left to right should take prioritiy. For Patient “6”, a diagnosis of “25” is more important than a diagnosis of “30”, so “BH” should be a “1” and not a “2”.

 

I am using SAS 9.2.

 

Thanks for the help!


Accepted Solutions
Solution
‎07-05-2016 02:52 PM
Super User
Posts: 10,466

Re: RECODING: Prioritizing conflicting values within the same variable

If the rule is always the lower indexed array variable (more formal "Left") then you can use Leave to quite the Do loop.

 

DATA &LIB..FINAL;
   SET &LIB..FINAL;
   ARRAY ACSC{*} PRINDIAG DIAG1-DIAG5;
   ALLDIAG = CATX (‘  ‘, OF ACSC{*});
   DO I=1 TO 6;
   IF ACSC{I} IN (20, 25) THEN do;
      BH = 1;
      leave;
   end;
   ELSE IF ACSC{I} IN (30,31) THEN do;
      BH = 2;
      leave;
   end;
   DROP I;
   END;
RUN;

I moved the ALLDIAg assignment as doing it within the loop is just duplicating the same assignment multiple times.

 

If you have more complicated priorities you may want to investigate use of WHICHN or WHICHC functions to play with orders of appearance within the array.

View solution in original post


All Replies
Solution
‎07-05-2016 02:52 PM
Super User
Posts: 10,466

Re: RECODING: Prioritizing conflicting values within the same variable

If the rule is always the lower indexed array variable (more formal "Left") then you can use Leave to quite the Do loop.

 

DATA &LIB..FINAL;
   SET &LIB..FINAL;
   ARRAY ACSC{*} PRINDIAG DIAG1-DIAG5;
   ALLDIAG = CATX (‘  ‘, OF ACSC{*});
   DO I=1 TO 6;
   IF ACSC{I} IN (20, 25) THEN do;
      BH = 1;
      leave;
   end;
   ELSE IF ACSC{I} IN (30,31) THEN do;
      BH = 2;
      leave;
   end;
   DROP I;
   END;
RUN;

I moved the ALLDIAg assignment as doing it within the loop is just duplicating the same assignment multiple times.

 

If you have more complicated priorities you may want to investigate use of WHICHN or WHICHC functions to play with orders of appearance within the array.

Contributor
Posts: 45

Re: RECODING: Prioritizing conflicting values within the same variable

Your solution worked, thank you so much! Smiley Very Happy

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 177 views
  • 0 likes
  • 2 in conversation