This is supposed to be easy but not sure why I am not getting it right (just a few months into Stata):
Please see the attached dataset. I am trying to find out if patients received a combination of three meds on each of their visits to the pharmacy. So the logic would be to sort by patients, then by date of pharmacy visit and then search for strings within each date (td). The strings would be "TDF(300mg)+3TC(300mg)+EFV(600mg)", "Cotrimoxazole 960mg" and "Isoniazid 300mg". In trying to solve the logic, I also encoded the strings as follows: "TDF(300mg)+3TC(300mg)+EFV(600mg)" - 41, "Cotrimoxazole 960mg" - 30 and "Isoniazid 300mg" - 33. I would like to generate a new variable called SameDay that returns "Yes" or "No" depending on if all three meds are administered during the visit. I have tried the following without success:
  1. bysort HospitalNum (DateVisitPharm): g SameDay = cond((Regimen[_n] == [30|33|41] & Regimen[_n+1] == [30|33|41] & Regimen[_n+2] == [30|33|41]),"Yes","No")
  2. bysort HospitalNum (DateVisitPharm): g SameDay = cond((Regimen[_n] == [30|33|41] & Regimen[_n] == [30|33|41] & Regimen[_n] == [30|33|41]),"Yes","No")
  3. bysort HospitalNum (DateVisitPharm): g SameDay = cond((Regimen[_n] == 30 & Regimen[_n] == 33 & Regimen[_n] == 41),"Yes","No")
  4. bysort HospitalNum (DateVisitPharm): egen SameDay = anymatch(Regimen1),v(30 33 41)
  5. bysort HospitalNum (DateVisitPharm): egen SameDay = anyvalue(Regimen1),v(30 33 41)
  6. bysort HospitalNum (DateVisitPharm): egen SameDay = anymatch(Regimen), values("*Cotrim*","*Isonia*")
I would also appreciate a succinct explanation on the role of () before the : in bysort commands, in addition to any other possible combinations here including having two variables within the () and before it. If there is any advanced material on this I would appreciate it too. Also if there is any advanced material on explicit subscripting I could go through, that would be much appreciated. I know this is a bit much but I will be very appreciative.