I have a dataset of participants who are dispensed medicines on a particular date (identified by variable supply date). I want to be able to calculate the number of active ingredients that they use in a month. I need to also be able to calculate how many medicines they are adherent to the medicine by having it dispensed in two different time periods. I need to be able to account for people who use two or more products containing a single active ingredient so I don't double count them.
The code
The code I've written works and gets me the outcome I want, but it's long winded and in-elegant. I've used amlodipine as the example here, but I need to do this again for other medicines, and I don't want to do this in such a long format for 100 different preparations. I've had to put it in such long form so that I don't end up with the two ATC codes being equal and the code simply telling me that they remain on the same preparation. How do I write this code so that I only need to do it once instead of having such a long winded way of running through the variables removing the ATC code one at a time?
local amlodipinecombos c09bb04 c09db01 c09db02 c09db04 c09dx01 c09dx03 c10bx03
foreach j of local amlodipinecombos {
forvalues i=0(1)5 {
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c08ca01") & ATCcode[[_n-`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c08ca01") & ATCcode[[_n+`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n-`i']] == "c08ca01"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n+`i']] == "c08ca01"
}
}
local amlodipinecombos c09db01 c09db02 c09db04 c09dx01 c09dx03 c10bx03
foreach j of local amlodipinecombos {
forvalues i=0(1)5 {
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09bb04") & ATCcode[[_n-`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09bb04") & ATCcode[[_n+`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n-`i']] == "c09bb04"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n+`i']] == "c09bb04"
}
}
local amlodipinecombos c09db02 c09db04 c09dx01 c09dx03 c10bx03
foreach j of local amlodipinecombos {
forvalues i=0(1)5 {
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09db01") & ATCcode[[_n-`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09db01") & ATCcode[[_n+`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n-`i']] == "c09db01"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n+`i']] == "c09db01"
}
}
local amlodipinecombos c09db04 c09dx01 c09dx03 c10bx03
foreach j of local amlodipinecombos {
forvalues i=0(1)5 {
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09db02") & ATCcode[[_n-`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09db02") & ATCcode[[_n+`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n-`i']] == "c09db02"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n+`i']] == "c09db02"
}
}
local amlodipinecombos c09dx01 c09dx03 c10bx03
foreach j of local amlodipinecombos {
forvalues i=0(1)5 {
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09db04") & ATCcode[[_n-`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09db04") & ATCcode[[_n+`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n-`i']] == "c09db04"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n+`i']] == "c09db04"
}
}
local amlodipinecombos c09dx03 c10bx03
foreach j of local amlodipinecombos {
forvalues i=0(1)5 {
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09dx01") & ATCcode[[_n-`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09dx01") & ATCcode[[_n+`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n-`i']] == "c09dx01"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n+`i']] == "c09dx01"
}
}
forvalues i=0(1)5 {
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09dx03") & ATCcode[[_n-`i']] == "c10bx03"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09dx03") & ATCcode[[_n+`i']] == "c10bx03"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c10bx03") & ATCcode[[_n-`i']] == "c09dx03"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c10bx03") & ATCcode[[_n+`i']] == "c09dx03"
}
foreach j of local amlodipinecombos {
forvalues i=0(1)5 {
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c08ca01") & ATCcode[[_n-`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c08ca01") & ATCcode[[_n+`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n-`i']] == "c08ca01"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n+`i']] == "c08ca01"
}
}
local amlodipinecombos c09db01 c09db02 c09db04 c09dx01 c09dx03 c10bx03
foreach j of local amlodipinecombos {
forvalues i=0(1)5 {
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09bb04") & ATCcode[[_n-`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09bb04") & ATCcode[[_n+`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n-`i']] == "c09bb04"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n+`i']] == "c09bb04"
}
}
local amlodipinecombos c09db02 c09db04 c09dx01 c09dx03 c10bx03
foreach j of local amlodipinecombos {
forvalues i=0(1)5 {
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09db01") & ATCcode[[_n-`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09db01") & ATCcode[[_n+`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n-`i']] == "c09db01"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n+`i']] == "c09db01"
}
}
local amlodipinecombos c09db04 c09dx01 c09dx03 c10bx03
foreach j of local amlodipinecombos {
forvalues i=0(1)5 {
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09db02") & ATCcode[[_n-`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09db02") & ATCcode[[_n+`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n-`i']] == "c09db02"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n+`i']] == "c09db02"
}
}
local amlodipinecombos c09dx01 c09dx03 c10bx03
foreach j of local amlodipinecombos {
forvalues i=0(1)5 {
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09db04") & ATCcode[[_n-`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09db04") & ATCcode[[_n+`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n-`i']] == "c09db04"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n+`i']] == "c09db04"
}
}
local amlodipinecombos c09dx03 c10bx03
foreach j of local amlodipinecombos {
forvalues i=0(1)5 {
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09dx01") & ATCcode[[_n-`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09dx01") & ATCcode[[_n+`i']] == "`j'"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n-`i']] == "c09dx01"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "`j'") & ATCcode[[_n+`i']] == "c09dx01"
}
}
forvalues i=0(1)5 {
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09dx03") & ATCcode[[_n-`i']] == "c10bx03"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c09dx03") & ATCcode[[_n+`i']] == "c10bx03"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c10bx03") & ATCcode[[_n-`i']] == "c09dx03"
bysort pat_id: replace switch_combination_single = 1 if (ATCcode == "c10bx03") & ATCcode[[_n+`i']] == "c09dx03"
}
Describing the dataset
The variable ATC code uniquely identifies each medicine, while the drug_name variable names the variable (so each ATCcode value uniquely matches one drug_name value). I have eight different products that contain amlodipine that are identified by the ATC codes c09bb04 c09db01 c09db02 c09db04 c09dx01 c09dx03 c10bx03 c08ca01.
I have created an active ingredients variable that tells me how many active ingredients there are in each product (i.e. amlodipine c08ca01 has only 1 active ingredient while perindopril + amlodipine c09bb04 and telmisartan + amlodipine c09db04 have two active ingredients).
Here is a sample of my data:
input double participant_id int supply_date str45 drug_name str7 ATCcode byte activeingredients
186331 22528 "amlodipine" "c08ca01" 1
461681 22415 "amlodipine" "c08ca01" 1
555339 22561 "amlodipine" "c08ca01" 1
568787 22538 "amlodipine" "c08ca01" 1
886834 22351 "amlodipine" "c08ca01" 1
895276 22517 "amlodipine" "c08ca01" 1
898571 22521 "amlodipine" "c08ca01" 1
1002382 22300 "amlodipine" "c08ca01" 1
1002382 22518 "perindopril + amlodipine" "c09bb04" 2
1029599 22434 "telmisartan + amlodipine" "c09db04" 2
1034746 22454 "amlodipine" "c08ca01" 1
1058374 22506 "perindopril + amlodipine" "c09bb04" 2
1109334 22556 "perindopril + amlodipine" "c09bb04" 2
1260694 22380 "perindopril + amlodipine" "c09bb04" 2
1313392 22530 "amlodipine" "c08ca01" 1
1330429 22409 "amlodipine + atorvastatin" "c10bx03" 2
1498674 22507 "amlodipine" "c08ca01" 1
1624554 22297 "telmisartan + amlodipine" "c09db04" 2
1818279 22454 "amlodipine" "c08ca01" 1
2027189 22266 "perindopril + amlodipine" "c09bb04" 2
2051112 22378 "perindopril + amlodipine" "c09bb04" 2
2113962 22546 "amlodipine + atorvastatin" "c10bx03" 2
2149775 22587 "amlodipine" "c08ca01" 1
2221068 22416 "amlodipine" "c08ca01" 1
2236548 22548 "amlodipine" "c08ca01" 1
2387755 22395 "amlodipine" "c08ca01" 1
2550852 22489 "amlodipine" "c08ca01" 1
2877316 22527 "perindopril + amlodipine" "c09bb04" 2
3011485 22499 "amlodipine" "c08ca01" 1
3099995 22553 "amlodipine" "c08ca01" 1
3293720 22448 "amlodipine" "c08ca01" 1
3519668 22497 "amlodipine + valsartan" "c09db01" 2
3546885 22521 "amlodipine" "c08ca01" 1
3608178 22383 "amlodipine + atorvastatin" "c10bx03" 2
3659138 22415 "amlodipine" "c08ca01" 1
4024556 22306 "perindopril + amlodipine" "c09bb04" 2
4101175 22572 "amlodipine + valsartan" "c09db01" 2
4216723 22391 "amlodipine" "c08ca01" 1
4216723 22564 "amlodipine + valsartan" "c09db01" 2
4271284 22307 "amlodipine + atorvastatin" "c10bx03" 2
4289779 22467 "olmesartan + amlodipine" "c09db02" 2
4531181 22573 "perindopril + amlodipine" "c09bb04" 2
4668914 22288 "olmesartan + amlodipine" "c09db02" 2
4922079 22340 "olmesartan + amlodipine" "c09db02" 2
4922079 22365 "olmesartan + amlodipine + hydrochlorothiazide" "c09dx03" 3
5188987 22351 "amlodipine + valsartan + hydrochlorothiazide" "c09dx01" 3
5428704 22385 "perindopril + amlodipine" "c09bb04" 2
5454184 22539 "perindopril + amlodipine" "c09bb04" 2
5642761 22227 "amlodipine" "c08ca01" 1
5647909 22299 "amlodipine" "c08ca01" 1
5678574 22429 "amlodipine + atorvastatin" "c10bx03" 2
5712496 22375 "amlodipine" "c08ca01" 1
5731271 22576 "amlodipine + valsartan + hydrochlorothiazide" "c09dx01" 3
5731271 22344 "telmisartan + amlodipine" "c09db04" 2
5765347 22332 "amlodipine" "c08ca01" 1
5807711 22277 "perindopril + amlodipine" "c09bb04" 2
5836639 22251 "telmisartan + amlodipine" "c09db04" 2
5838376 22329 "amlodipine + valsartan" "c09db01" 2
6032101 22417 "amlodipine + valsartan" "c09db01" 2
6073062 22613 "perindopril + amlodipine" "c09bb04" 2
6491177 22473 "telmisartan + amlodipine" "c09db04" 2
6645832 22342 "amlodipine" "c08ca01" 1
6645832 22519 "amlodipine + valsartan" "c09db01" 2
6650979 22351 "olmesartan + amlodipine" "c09db02" 2
6662870 22539 "amlodipine" "c08ca01" 1
6681465 22500 "amlodipine + valsartan + hydrochlorothiazide" "c09dx01" 3
6786860 22351 "perindopril + amlodipine" "c09bb04" 2
6865037 22531 "olmesartan + amlodipine + hydrochlorothiazide" "c09dx03" 3
6865037 22291 "telmisartan + amlodipine" "c09db04" 2
6868485 22495 "amlodipine" "c08ca01" 1
6975437 22397 "telmisartan + amlodipine" "c09db04" 2
7080985 22339 "amlodipine" "c08ca01" 1
7322259 22611 "perindopril + amlodipine" "c09bb04" 2
7463287 22269 "amlodipine + valsartan" "c09db01" 2
7616385 22357 "amlodipine" "c08ca01" 1
7791488 22284 "amlodipine" "c08ca01" 1
7791488 22460 "perindopril + amlodipine" "c09bb04" 2
7818705 22355 "amlodipine + valsartan" "c09db01" 2
7951137 22558 "amlodipine" "c08ca01" 1
8056685 22576 "amlodipine" "c08ca01" 1
8309850 22458 "amlodipine" "c08ca01" 1
8474800 22371 "amlodipine" "c08ca01" 1
8634603 22461 "olmesartan + amlodipine + hydrochlorothiazide" "c09dx03" 3
8671973 22283 "perindopril + amlodipine" "c09bb04" 2
8680568 22560 "amlodipine + valsartan + hydrochlorothiazide" "c09dx01" 3
8680568 22294 "perindopril + amlodipine" "c09bb04" 2
8753445 22503 "telmisartan + amlodipine" "c09db04" 2
8775630 22342 "telmisartan + amlodipine" "c09db04" 2
8954208 22416 "perindopril + amlodipine" "c09bb04" 2
8974540 22410 "perindopril + amlodipine" "c09bb04" 2
9047570 22567 "perindopril + amlodipine" "c09bb04" 2
9090088 22316 "amlodipine + atorvastatin" "c10bx03" 2
9168265 22501 "amlodipine" "c08ca01" 1
9356996 22566 "amlodipine" "c08ca01" 1
9397803 22448 "amlodipine + atorvastatin" "c10bx03" 2
9418136 22466 "amlodipine + atorvastatin" "c10bx03" 2
9712261 22424 "amlodipine" "c08ca01" 1
9727715 22480 "telmisartan + amlodipine" "c09db04" 2
9836251 22480 "amlodipine" "c08ca01" 1
9889101 22404 "amlodipine + valsartan + hydrochlorothiazide" "c09dx01" 3
end
format %d supply_date
[/CODE]
Comment