Dear Stata masters,
I analyse a clinical trial where some patient switch treatment arms. I seek an effective code to flag observations only before patients switch the treatment arm.
For concreteness, consider the following patient observed 6 times (admitted 6 times into medical wards). Admission 4 and 5 (N=4,5) are in treated wards (treat=1), others in control (treat=0).
I must flag observation but only before "treat" changes value.
I have a solution that (for data sorted by URN WardAdmTime) involves creating a variable "or_treat" equal to the first value of "treat" by URN. I then put a "flag" when "treat" and "or_treat" are different and then replace the value of "or_treat" with "flag" if the previous value is "flag."
An extract from data
The code
sort URN WardAdmTime
browse URN WardAdmTime treat
bysort URN (WardAdmTime): gen or_treat=treat[1]
bysort URN (WardAdmTime): replace or_treat=999 if or_treat!=treat
forvalues i=1/20 {
bysort URN (WardAdmTime): replace or_treat=999 if or_treat[_n-`i']==999
}
gen sample=test1!=999
I'd like to learn better ways to do this in STATA.
Kind regards,
Sergey Alexeev | Senior Research Fellow - Health Economist
NHMRC Clinical Trials Centre
The University of Sydney, Faculty of Medicine and Health
[email protected]
I analyse a clinical trial where some patient switch treatment arms. I seek an effective code to flag observations only before patients switch the treatment arm.
For concreteness, consider the following patient observed 6 times (admitted 6 times into medical wards). Admission 4 and 5 (N=4,5) are in treated wards (treat=1), others in control (treat=0).
I must flag observation but only before "treat" changes value.
| N | URN | WardAdmTime | treat | sample |
| 1 | 15928 | 5/10/2019 14:27 | 0 | 1 |
| 2 | 15928 | 18/11/2019 21:02 | 0 | 1 |
| 3 | 15928 | 4/01/2020 12:03 | 0 | 1 |
| 4 | 15928 | 17/01/2020 9:49 | 1 | 0 |
| 5 | 15928 | 21/02/2020 18:53 | 0 | 0 |
| 6 | 15928 | 18/03/2020 12:38 | 1 | 0 |
An extract from data
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str7 URN double WardAdmTime float treat "1156015" 1888661460000 1 "1156015" 1.888839e+12 0 "1156015" 1889784359999.998 0 "115621" 1900952700000.002 1 "115621" 1901388300000.002 0 "115621" 1903238940000 0 "115621" 1903291800000.002 0 "115621" 1903949700000.002 0 "115649" 1898796539999.998 0 "115658" 1898358240000 0 "1158249" 1894534919999.998 0 "1158255" 1887288960000 0 "1158411" 1.8842607e+12 0 "1158577" 1883843280000 1 "1158577" 1.8843471e+12 1 "1158577" 1884528960000 1 "1158577" 1.8858339e+12 0 "1158577" 1885896420000 1 "1158577" 1.8898806e+12 1 "1158577" 1894974360000 1 "1158577" 1897739160000 1 "115889" 1902539579999.998 0 "115889" 1902744120000 0 end format %tcdd-Mon-CCYY_HH:MM WardAdmTime
sort URN WardAdmTime
browse URN WardAdmTime treat
bysort URN (WardAdmTime): gen or_treat=treat[1]
bysort URN (WardAdmTime): replace or_treat=999 if or_treat!=treat
forvalues i=1/20 {
bysort URN (WardAdmTime): replace or_treat=999 if or_treat[_n-`i']==999
}
gen sample=test1!=999
I'd like to learn better ways to do this in STATA.
Kind regards,
Sergey Alexeev | Senior Research Fellow - Health Economist
NHMRC Clinical Trials Centre
The University of Sydney, Faculty of Medicine and Health
[email protected]

Comment