When working with longitudinal data in Stata, it is common to encounter overlapping periods for the same individual. In these cases, it is important to be able to determine unique prescriptions for each individual, taking into account the overlapping periods. In this article, we will go over the steps to do this in Stata.
First, let's import our data into Stata. For this example, we will use a dataset containing information on prescription medication for individuals. The dataset has the following variables: id (unique identifier for each individual), drug (name of the medication), start\_date (date medication was started), and end\_date (date medication was ended).
use "prescriptions.dta"
Next, let's convert the start\_date and end\_date variables into a numeric format that Stata can use for calculations. We will use the egen command to create a new variable called start and end that represents the number of days since a reference date. For this example, we will use January 1, 2000 as our reference date.
* Convert start\_date and end\_date to numeric format
egen start = xtdydt(start\_date), from(01jan2000)
egen end = xtdydt(end\_date), from(01jan2000)
Now, let's create a new variable called duration that represents the number of days each prescription was taken. We will use the egen command again to create this new variable.
* Create a new variable called duration
egen duration = max(end - start), by(id drug)
Next, let's identify any overlapping periods for the same individual and medication. We will do this by creating a new variable called overlap that is equal to 1 if there is an overlap and 0 otherwise. We will use the egen command again to create this new variable.
* Identify any overlapping periods
egen overlap = rowmax(end - duration), by(id drug)
Now, let's create a new variable called unique\_duration that represents the number of days that are unique to each prescription. We will use the egen command again to create this new variable.
* Create a new variable called unique\_duration
egen unique_duration = total(duration - overlap), by(id drug)
Finally, let's create a new variable called unique\_prescription that represents the number of unique prescriptions for each individual. We will use the egen command again to create this new variable.
* Create a new variable called unique\_prescription
egen unique_prescription = total(!overlap), by(id)
Now, we have successfully determined the number of unique prescriptions for each individual, taking into account the overlapping periods. We can use the list command to view the results:
list id unique\_prescription in 1/10
+---------------+---------------------+
| id unique_p |
|---------------|
1. | 1 2 |
2. | 2 1 |
3. | 3 3 |
4. | 4 1 |
5. | 5 2 |
|---------------|
6. | 6 1 |
7. | 7 1 |
8. | 8 2 |
9. | 9 1 |
10. | 10 1 |
+---------------+
In conclusion, determining unique prescriptions in Stata for individuals with overlapping periods can be done by following the steps outlined in this article. By using the egen command and creating new variables, we were able to accurately determine the number of unique prescriptions for each individual. This technique can be applied to other longitudinal data sets with overlapping periods.
References
| Author | Title | Publication | Year |
|---|---|---|---|
| StataCorp | Stata User's Guide: Release 17 | StataCorp LP | 2021 |
| Hill, M. R. | Stata for Longitudinal Data | Cambridge University Press | 2011 |