# Help generating summary variable of medication data in Stata.

#### simonja

##### New Member
Hi everyone, I'm new to stata, so any advice on how to code this would be greatly appreciated.

I have medication data for 1500 patients. Each row represents a unique medication ID, so each patient may be represented in multiple rows if he/she has multiple meds (example below). I'd like to create a summary variable (ie, Total_Meds) that equals the total number of meds assigned to each patient; and assign it to the individual patient. For example, Total_Meds would equal 4 for the 1st patient below. Thanks in advance.

Patient_ID_____________ Med_ID
543789347___________________5435
543789347___________________9812
543789347___________________3242
543789347___________________1290

189340584__________________4590
189340584__________________9812
189340584_________________ 1290

483908535__________________5435
483908535_________________ 1290
483908535__________________5490
483908535__________________4334
etc.
etc.

#### RedOwl

##### New Member
Here is one approach, but it may not be the most efficient one.

Create a toy data set for the demonstration.

Code:
clear
input str1 patient str5 med
A drug1
A drug2
A drug2
A drug3
B drug2
C drug1
C drug3
D drug2
D drug3
D drug3
E drug1
end
Now create variable "sumdrugs" as the total number of unique meds taken by each patient.

Code:
egen onedrug = tag(patient med)
egen sumdrugs=sum(onedrug), by(patient)
egen onepatient = tag(patient)
Now list the patients and the number of meds each is taking.

Code:
list patient sumdrugs if onepatient

Last edited: