BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
TXSASneophyte
Obsidian | Level 7

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

2 REPLIES 2
ballardw
Super User

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.

TXSASneophyte
Obsidian | Level 7

Your solution worked, thank you so much! 😄

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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