Hi, Diane:
I understood where you were going. It is a problem that requires some advanced programming skills -- because you are not only concerned on the effect of A with the other RX ...but in B with all the others ...and C with all the others, etc.
So you need to first find out the start of RX and runout date of RX for each RX and PTID.
But, let's say that you could pin down a where clause that would help you find the information -- given your original data, for example....this WHERE clause would show what you want, assuming you knew (or could program a way for SAS to tell you the start and stop RX dates for PT 1, RX A, PT2, RX A, etc:
[pre]
proc freq data=rxdata nlevels;
title 'Proc Freq for dates of PTID/RXNAME Combo';
title2 "ptid = 1 for rxname = A, the";
title3 "start/runout dates were: 01Jan2008 and 05Apr2008";
where ptid = 1 and
rxdate between "01Jan2008 "d and "05Apr2008"d;
tables rxname;
run;
[/pre]
Now, this PROC FREQ and the NLEVELS will show you for THIS patient, for RX=A start and end dates, the number of interactions.
I'd end up using a big macro program, probably with a macro do loop to solve this. But, let me attempt to show this for 1 patient and 1 RX, how I'd approach the problem.
First, you have to "collapse" the data so you have the start date and runout date of each RX for each patient. I'd use a DATA Step program:
[pre]
** Make some data and calculate the ending date;
** or date that someone will run out of their RX;
data rxdata;
infile datalines;
input ptid rxname $
rxdate : mmddyy10. day_supply;
runout = rxdate + day_supply;
return;
datalines;
1 A 1/1/2008 30
1 A 2/5/2008 60
1 B 2/11/2008 30
1 C 3/1/2008 90
1 B 3/09/2008 30
1 D 4/1/2008 30
1 E 4/1/2008 30
2 A 2/1/2008 30
2 A 3/5/2008 30
2 B 3/11/2008 30
2 B 4/1/2008 30
;
run;
** Sort the data;
proc sort data=rxdata;
by ptid rxname rxdate runout;
run;
** Print the data;
ods listing;
ods noptitle;
options nocenter formchar='|-+-+++++-+';
proc print data=rxdata;
format rxdate mmddyy10. runout mmddyy10.;
title 'what the data looks like';
run;
** Now, capture the first_rxdate and the last_rxdate for each;
** patient and rxname. First_rxdate comes from the rxdate value;
** and Last_rxdate comes from the RUNOUT var that was calculated;
** above. This program will create one obs for every;
** PTID/RXNAME combo.;
data newrxdata (keep=ptid rxname first_rxdate last_rxdate);
set rxdata;
retain first_rxdate last_rxdate;
by ptid rxname;
retain first_rxdate last_rxdate;
if first.rxname then first_rxdate = rxdate;
if last.rxname then do;
last_rxdate = runout;
output;
first_rxdate = .;
last_rxdate = .;
end;
run;
** Print the new data to test;
proc print data=newrxdata;
format first_rxdate mmddyy10. last_rxdate mmddyy10.;
title 'Capture start and end dates for each RX';
run;
[/pre]
And, the output from the first PROC PRINT would be:
[pre]
what the data looks like
day_
Obs ptid rxname rxdate supply runout
1 1 A 01/01/2008 30 01/31/2008
2 1 A 02/05/2008 60 04/05/2008
3 1 B 02/11/2008 30 03/12/2008
4 1 B 03/09/2008 30 04/08/2008
5 1 C 03/01/2008 90 05/30/2008
6 1 D 04/01/2008 30 05/01/2008
7 1 E 04/01/2008 30 05/01/2008
8 2 A 02/01/2008 30 03/02/2008
9 2 A 03/05/2008 30 04/04/2008
10 2 B 03/11/2008 30 04/10/2008
11 2 B 04/01/2008 30 05/01/2008
[/pre]
and the output from the second proc print would be:
[pre]
Capture start and end dates for each RX 16:06 Monday, June 16, 2008 22
first_ last_
Obs ptid rxname rxdate rxdate
1 1 A 01/01/2008 04/05/2008
2 1 B 02/11/2008 04/08/2008
3 1 C 03/01/2008 05/30/2008
4 1 D 04/01/2008 05/01/2008
5 1 E 04/01/2008 05/01/2008
6 2 A 02/01/2008 04/04/2008
7 2 B 03/11/2008 05/01/2008
[/pre]
Now, I want to make 2 macro variables...one for my PTID of interest and a second one for my RX of interest:
[pre]
** Now, pick a patient ID and an RX;
** that you want to test.;
%let wantpt = 1;
%let wantrx = A;
[/pre]
Next, I want to build a query to go in and find the first_rxdate and the last_rxdate for that patient/RX combo. The result of that query will be 2 new macro variables: &rxstart and &rxstop ...but formatted as DATE9. so I can use them in a subsequent WHERE statement:
[pre]
** Get the starting and ending dates for;
** that single patient and RX;
proc sql;
select first_rxdate format=date9., last_rxdate format=date9.
into :rxstart, :rxend
from newrxdata
where ptid = &wantpt and
rxname = "&wantrx";
quit;
%put For pt &wantpt and RX= &wantrx;
%put The relevant dates are: &rxstart and &rxend;
[/pre]
And the results of the %PUT are:
[pre]
1089
1090 %put For pt &wantpt and RX= &wantrx;
For pt 1 and RX= A
1091 %put The relevant dates are: &rxstart and &rxend;
The relevant dates are: 01JAN2008 and 05APR2008
[/pre]
Now, I can use &rxstart and &rxstop and my &wantpt variables in a PROC FREQ. In this example, I create a work table for the information...but I also just do the PROC FREQ as a report, too:
[pre]
** Now, run the same PROC FREQ for just that Patient and RX;
** that you picked with the macro variables.;
** Using ODS Output still allows you to create a unique;
** dataset for that patient/RXname interaction.;
** Note that we are using information derived from the original;
** file for the macro variables and that the PROC FREQ information;
** is based on the original file.;
ods output nlevels=work.rxlev_PT_&wantpt._RX_&wantrx;
proc freq data=rxdata nlevels;
title 'Proc Freq for dates of PTID/RXNAME Combo';
title2 "ptid = &wantpt and rxname = &wantrx and";
title3 "all rxdate values between &rxstart and &rxend";
where ptid = &wantpt and
rxdate between "&rxstart"d and "&rxend"d;
tables rxname;
run;
** You may or may not want the data for a single;
** patient in a SAS dataset.;
proc print data=work.rxlev_PT_&wantpt._RX_&wantrx;
title "Work.rxlev_PT_&wantpt._RX_&wantrx created with ODS";
title2 "ptid = &wantpt and rxname = &wantrx and";
title3 "rxdate between &rxstart and &rxend";
run;
[/pre]
And the output from that final PROC PRINT is:
[pre]
Work.rxlev_PT_1_RX_A created with ODS
ptid = 1 and rxname = A and
rxdate between 01JAN2008 and 05APR2008
Table
Obs Var NLevels
1 rxname 5
[/pre]
So, if I go back and change the &WANTPT and the &WANTRX:
[pre]
%let wantpt = 2;
%let wantrx = A;
[/pre]
and run the program from the PROC SQL step forward, then I get different results for the PROC PRINT:
[pre]
Work.rxlev_PT_2_RX_A created with ODS
ptid = 2 and rxname = A and
rxdate between 01FEB2008 and 04APR2008
Table
Obs Var NLevels
1 rxname 2
[/pre]
Once you get this far and get this working for hard-coding the patient ID and RX, then you can look at further macroizing the solution so that you make a macro program that takes patient ID and RX for ALL the patients. At that point, I'd probably put all the output from PROC FREQ into 1 big dataset (because I assume that you do NOT want to make a bunch of 1 obs datasets).
But, figuring out how to macroize this into a re-callable macro program is another, more advanced topic.
cynthia