Hey all,

Wonderful community here. I'm new to coding in stata. I was curious if someone could walk me through how to drop observations of the same value within the variable. I'm working with data in long format where each patient has a separate ID. The ID then repeats itself on a separate row if the patient has multiple study visits.

I want to drop the IDs of all patients (and all of their associated study visits) if they were on a specific drug at the initial visit. To do this, I created a variable, "ondrug" equal 1 if they were on this drug and a corresponding placeholder variable "dropid", where:

dropid = id if ondrug == 1 & followup_num == 1

However, when I run the command:

drop if id == dropid

The only ID that gets dropped is the ID's first visit and al subsequent IDs are left. I tried a for loop and ran into the same issues:

foreach x in dropid {
drop if id == `x'
}

Any idea how I can get stata to drop all of the entries of these patients in long format?

Many thanks!