BookmarkSubscribeRSS Feed
Emma22
Fluorite | Level 6

Hello SAS experts:

 

Can you give me a suggestion on how I can do the following chronologically (based on the Rx_date)?  The sample data is provided below. Thanks a lot in advance! 

 

Step1: Count the number of switches among the drugs for each patient ; for examples, Pat_ID 201, should have switch count =8;Pat_ID 202 would have switch count=6 , and Pat_ID 203 would have switch count=0;

 

Step2: For patients never switched, flag them as the drug they were on; For example, Pat_ID 203, would have a  Uflag='Stable' & DrugStable='DrugB'; Pat_ID 201 & 202 would have  Uflag='Switcher' & DrugSwitch='DrugC, DrugA,DrugC, DrugA, DrugDrugC, DrugB, DrugC';

 

Step 3. Output the last RX_Date record for each patient with the flags in Step 1 & 2 and retain the first Rx_Date for all  patients; while creating switch date fields to keep the switch dates for each switcher such as switch1, switch2, switch3,....;

 

Step 4: Count the number of 'Stable' patients for each drug;

 


data have;
input PAT_ID RX_DT : ddmmyy10. RX_Name $;
format RX_DT ddmmyy10.;
datalines;;
201 18/07/2013 DrugA
201 27/12/2013 DrugA
201 13/06/2013 DrugC
201 19/08/2013 DrugC
201 01/05/2014 DrugC
201 14/10/2013 DrugC
201 20/02/2014 DrugC
201 27/03/2014 DrugA
201 16/09/2013 DrugB
201 18/12/2013 DrugC
202 16/04/2014 DrugC
202 16/01/2014 DrugC
202 02/10/2013 DrugC
202 15/07/2013 DrugC
202 13/05/2013 DrugC
202 11/03/2014 DrugC
202 11/11/2013 DrugB
202 23/09/2013 DrugA
202 05/08/2013 DrugA
202 24/06/2013 DrugA
203 11/12/2014 DrugB
203 11/09/2015 DrugB
203 23/09/2016 DrugB
203 05/08/2017 DrugB
203 24/06/2013 DrugB
;
run;

proc sort data=have;
by pat_id rx_dt;
quit;

 

 

 

 

7 REPLIES 7
ballardw
Super User

Please provide a clear and complete definition of exactly what makes a "switch".

 

One concern when discussing medication is that people are often on multiple medications and I don't see anything in that example data that allows telling when different medication is just that, different for a different diagnosis. So "switch" is not at all clear to me.

So you have to walk us through exactly what steps you are using to get a count of 8 for Pat_id = 201. If I count "change" of drug I only see 4. So there must be some other rules that you have not provided.

 

Exactly how to expect to use a variable like this later : DrugSwitch='DrugC, DrugA,DrugC, DrugA, DrugDrugC, DrugB, DrugC'

 

And Step 3 you need to provide an actual example of the data set you expect as I don't follow that at all.

Emma22
Fluorite | Level 6

Thank you for your questions. I would like to define a 'switch' as any change different from the original drug. For example, consider pat_id201, who started with DrugC, then switched to Drug A, and later reverted to DrugC. In this case, I would define that patient 201 had 2 switches during the period from June 2013 through Aug 2013.
My aim is to maintain the history of movements among drugs and allow users to define a time period based on RX_DT to analyze the number of switches per patient and counts of patients for each drug within the drug group based on a user-defined period.

 

For simplicity, all drugs in the provided sample data belong to the same drug group, and there is no need to consider other diagnosis-related variables. Thanks a lot! 

mkeintz
PROC Star

This looks like homework.

 

What have you tried so far?  If you show your code, and the results produced, we can advise.  We can help you own the task,  instead of the opposite.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Tom
Super User Tom
Super User

Sort the data by PAT_ID and RX_DT but process the data by PAT_ID and RX_NAME.  You will need to add the NOTSORTED keyword to the BY statement.  Then just count the number of FIRST.RX_NAME records.

proc sort data=have;
  by pat_id px_dt;
run;

data want;
  set have ;
  by pat_id rx_name notsorted;
  if first.pat_id then switch=0;
  switch+first.rx_name;
  if last.pat_id;
  keep pat_id switch;
run;

Resutl:

Obs    PAT_ID    switch

 1       201        5
 2       202        3
 3       203        1
Emma22
Fluorite | Level 6
Thank you for your help, Tom. The result won't show the proper results for the switch count unless we add and "else" before "switch+first.rx_name;"

Thanks, Tom
Tom
Super User Tom
Super User

Do you mean that you want subjects that only took one medication to coded as SWITCH=0 instead of SWITCH=1?

Emma22
Fluorite | Level 6

Yes, that's my hope at the end. I want to be able to flag patients as switchers and non-switcher (stable) at a given date. For example,  on July,1, 2013. 

 

At the beginning, I was just hoping to count how many times each patient switched and then sum up the total number of switches by pat_id before I use proc freq to get the distribution of the patients among drugs at a given date; 

 

Thank you very much again! 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 7 replies
  • 777 views
  • 1 like
  • 4 in conversation