I am using Stata v16. I have a long format data set with multiple records for each patient, each patient may have different treatments that are given on different dates. I am trying to create a variable that gives the number of the patient's treatment regime. For example, patient A received treatment Z, then treatment X at a later date, then treatment Y at a later date. Whereas, patient B may have received treatment Z, then treatment Y at a later date, then treament X at a later date.
In other words, I would like a variable that indicates the regimen number of the patient. The treatments are not given in the same order for each patient, and the patient may recieve the same treatment on multiple days.
So, the data set for these two patients should look like:
Using the "sysuse auto" data set...
In the above example the data set should look like:
In the table above, notice in the last row that the value for foreign (treatment) is 0 but the regimen is 3. This is because this is the third treatment regimen the patient would have received (given the trunk [date] variable).
Any help is much appreciated.
In other words, I would like a variable that indicates the regimen number of the patient. The treatments are not given in the same order for each patient, and the patient may recieve the same treatment on multiple days.
So, the data set for these two patients should look like:
ID | Treatment | Date | Regimen number (I need this column) | ||||||
A | Z | 01/01/2000 | 1 | ||||||
A | Z | 02/01/2000 | 1 | ||||||
A | X | 15/01/2000 | 2 | ||||||
A | X | 16/01/2000 | 2 | ||||||
A | X | 17/01/2000 | 2 | ||||||
A | Y | 21/01/2000 | 3 | ||||||
A | Y | 22/01/2000 | 3 | ||||||
B | Z | 02/01/2000 | 1 | ||||||
B | Z | 03/01/2000 | 1 | ||||||
B | Z | 04/01/2000 | 1 | ||||||
B | Y | 10/01/2000 | 2 | ||||||
B | Y | 11/01/2000 | 2 | ||||||
B | X | 15/01/2000 | 3 | ||||||
B | X | 17/01/2000 | 3 |
Using the "sysuse auto" data set...
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input int mpg byte foreign int trunk 14 1 14 14 0 15 14 0 16 14 0 16 14 0 16 14 0 20 17 0 11 17 1 14 17 1 15 17 0 21 end label values foreign origin label def origin 0 "Domestic", modify label def origin 1 "Foreign", modify
In the above example the data set should look like:
mpg (ID) | foreign (Treatment) | trunk (date) | regimen number (I need this variable) |
14 | 1 | 14 | 1 |
14 | 0 | 15 | 2 |
14 | 0 | 16 | 2 |
14 | 0 | 16 | 2 |
14 | 0 | 16 | 2 |
14 | 0 | 20 | 2 |
17 | 0 | 11 | 1 |
17 | 1 | 14 | 2 |
17 | 1 | 15 | 2 |
17 | 0 | 21 | 3 |
Any help is much appreciated.
Comment