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

Hi everyone, 

 

I have a dataset with a similar structure as the one shown below:

 

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 DrugB
202 11/11/2013 DrugB
202 23/09/2013 DrugA
202 05/08/2013 DrugA
202 24/06/2013 DrugA
;

 

I would like to count the number of switches per patient from DrugA -> DrugB or from DrugB -> DrugA.

For example, both patients have one swhitch.

 

Thank you very much for your help in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

I like the use of the where and first.id techniques. In the interest of compactness, I think it makes sense to reduce the amount of code to one data step:

 

data want (keep=pat_id nswitch);

  set have;

  where drug in ('A','B');

  by pat_id drug notsorted;

  if first.pat_id then nswitch=0;

  else if first.drug then nswitch+1;

  if last.pat_id;

run;

 

 

m

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

--------------------------

View solution in original post

3 REPLIES 3
ballardw
Super User

This seems to work for your example data:

data have;
   input PAT_ID RX_DT : ddmmyy10. RX_Name $;
   format RX_DT ddmmyy10.;
   ld = lag(rx_name);
   switch = (ld='DrugA' and Rx_name='DrugB') or (ld='DrugB' and Rx_name='DrugA') ;
   drop ld;
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 DrugB
202 11/11/2013 DrugB
202 23/09/2013 DrugA
202 05/08/2013 DrugA
202 24/06/2013 DrugA
;
run;

 

May get cumbersome to use this approach for more specific comparisons though.

 

And I hope your real data has very consistent spelling.

Ksharp
Super User
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 DrugB
202 11/11/2013 DrugB
202 23/09/2013 DrugA
202 05/08/2013 DrugA
202 24/06/2013 DrugA
;
run;
data temp;
 set have(where=(RX_Name in ('DrugA' 'DrugB')));
 by PAT_ID RX_Name notsorted;
 if first.RX_Name;
run;
proc sql;
select PAT_ID,int(count(*)/2) as n_switch
 from temp
  group by PAT_ID ;
quit;
mkeintz
PROC Star

I like the use of the where and first.id techniques. In the interest of compactness, I think it makes sense to reduce the amount of code to one data step:

 

data want (keep=pat_id nswitch);

  set have;

  where drug in ('A','B');

  by pat_id drug notsorted;

  if first.pat_id then nswitch=0;

  else if first.drug then nswitch+1;

  if last.pat_id;

run;

 

 

m

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

--------------------------

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
  • 3 replies
  • 1025 views
  • 0 likes
  • 4 in conversation