1
votes

(Note: this is a homework assignment for which I have the correct answer, but I am unsatisfied with the results.)

A little context with regards to the data. Information on each of four drugs was recorded for a patient's coming in for a clinic visit: a code of 0 indicates the drug is not being taken, a code of 1 indicates the drug is being started, a code of 2 indicates that drug is being discontinued. For each visit, a patient ID, visit date, and the status codes of the four drugs are recorded.

My goal is to only use PROC SORT and DATA _NULL_ procedures to produce a report. I have had success using a PUT statement in the DATA _NULL_ step to create a list of individuals who have used all prescription drugs, but how would I go about producing a report showing the total number of individuals who have used all prescription drugs?

Current Output

Patient 01 has taken 4 different drugs
Patient 03 has taken 4 different drugs
Patient 04 has taken 4 different drugs

Desired Output

There are 3 patients who have taken 4 different drugs.

Code

DATA drug;
    Input ID : $2. Visit : MMDDYY8. rx_1-rx_4;
Datalines;
04 01/28/95 1 0 1 1
03 02/28/95 2 0 2 1
05 05/20/95 0 0 0 0
01 03/27/95 1 0 0 1
03 03/02/95 0 0 0 1
01 04/04/95 0 1 1 1
02 04/29/95 0 1 1 1
02 05/04/95 0 0 1 0
03 02/26/95 1 0 1 1
03 03/06/95 0 1 0 1
04 01/25/95 0 0 1 0
05 03/01/95 0 0 0 0
02 04/30/95 0 2 1 2
01 03/31/95 2 1 0 1
03 03/07/95 0 1 0 2
04 02/01/95 1 1 1 1
05 04/18/95 0 0 0 0
01 04/09/95 0 2 1 2
;
PROC SORT Data = Drug;
    By id visit;
RUN;
%let num_rx_var = %eval(4); 
DATA _null_;
    File Print;
        Set drug;
        By id;
    Array
        rx_indicator{&num_rx_var} rx_1-rx_4;
    Array
        takes_rx_{&num_rx_var} (&num_rx_var*0);
    Array
        has_taken_rx_{&num_rx_var} (&num_rx_var*0);
    Do i=1 To &num_rx_var;
        takes_rx_{i} = 0;
    End;
    Do i=1 To &num_rx_var;
        takes_rx_{i} = takes_rx_{i} or (rx_indicator{i}>0);
        sum_dif_rx_visit = sum(of takes_rx_{*});
    End;
    If first.id Then
        Do i=1 To &num_rx_var; 
            has_taken_rx_{i} = 0;
        End;
        Do i=1 To &num_rx_var;
            has_taken_rx_{i} = has_taken_rx_{i} or (rx_indicator{i}>0);
        End;
    If last.id Then Do;
        num_diff_rx = sum(of has_taken_rx_{*});
    End;    
        all_drugs = count(num_diff_rx, &num_rx_var);
    If all_drugs = 1 Then Put "Patient " id "has taken " num_diff_rx "different drugs" ;
RUN;
1

1 Answers

1
votes

Here's how I'd do it, trying to stick to mostly how you're doing things.

You've got a lot of extra stuff in there you don't really need. I don't know why you're using COUNT (a character function) for example towards the end. Also not sure why you're using %eval() in the macro variable - that's not doing anything. No idea what takes array is doing - is that something else?

Overall though your approach is pretty sound. If your data were better structured this would be very easy with an update statement, if 0 was instead .. I'm unsure of the importance of 2 here, you seem to disregard it; it does make sense that a drug that is a 2 must also be at some point a 1, but who knows.

The general approach to find out how many total patients take all drugs is to add a counter variable in the last if (where you verify that all drugs are taken) and then use that in the output. x+1; creates a counter, and automatically retains it.

%let num_rx_var = 4; 
DATA test;
    File Print;
    Set drug end=eof;                    *note small change here - sets `eof=1` on last row.;
    By id;
    array rx rx_1-rx_&num_rx_var;
    array rx_flag[&num_rx_var] _temporary_;  
                                         *Temporary array variables are automatically retained;
    if first.id then 
        call missing(of rx_flag[*]);     *clear the flags for a new ID;
    do _i = 1 to dim(rx);                *use your OR construct, that is fine;   
        rx_flag[_i] = rx_flag[_i] or rx[_i];
    end;
    if last.id then do;                  
        sum_drugs = sum(of rx_flag[*]);  *check if the sum of the flags equals number of flags;
                                         *there is a more fun way to do this using `whichn`;
        if sum_drugs = dim(rx_flag) then do;
            num_total +1;                *increment our counter;
            put "Patient " id "has taken "  sum_drugs "different drugs.";
        end;
    end;
    if eof then                         /* Use our counter only on the last row of the dataset */
        put num_total "Patients have taken &num_rx_Var. different drugs.";
RUN;