Hi--I have a study looking at healthcare encounters before and after an intervention. I'm trying to determine if (and how many) study participants were still being seen for patient encounters after the study formally closed data collection. I want to identify the last date a participant had an encounter ("HA_Contact_Date_HAcontactsthro"= encounter date) before the study close out date (March 15, 2024), and then if they had visits after the close out date. Data are in long form; I am using Stata 15.1 (but in process of upgrading to 18).
I tried this:
bysort PatientID:generate last_before_march=(_n==_N) if HA_Contact_Date_HAcontactsthro<23450
this:
bysort PatientID (HA_Contact_Date_HAcontactsthro):generate last_before_march24=HA_Contact_Date_HAcontactsthro[_N] if HA_Contact_Date_HAcontactsthro<23450
and this:
generate close_date=23450
bysort PatientID:generate last_before_march24=HA_Contact_Date_HAcontactsthro[_N] if HA_Contact_Date_HAcontactsthro< close_date
but they all repeat a similar error; when the variable HA_Contact_Date_HAcontactsthro is <23450 it gives me the final contact date by participant (or a 0, if I'm comparing to the close_date), and when it is not, it gives me a blank. What I *think* I want is for the variable "last_before_march24" to list the date that, for each person, was the last HA_Contact_Date_HAcontactsthro before 23450. But would love advice if there's a simpler way too. What should I be doing differently to determine which record is the last before the cutoff date?
Many thanks!
Ann Miller
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str9 PatientID int HA_Contact_Date_HAcontactsthro float(close_date last_before_march last_before_march24) "1" 23359 23450 0 23548 "1" 23377 23450 0 23548 "1" 23394 23450 0 23548 "1" 23422 23450 0 23548 "1" 23485 23450 . . "1" 23498 23450 . . "1" 23513 23450 . . "1" 23519 23450 . . "1" 23548 23450 . . "2" 23348 23450 0 23485 "2" 23355 23450 0 23485 "2" 23380 23450 0 23485 "2" 23411 23450 0 23485 "2" 23418 23450 0 23485 "2" 23428 23450 0 23485 "2" 23432 23450 0 23485 "2" 23439 23450 0 23485 "2" 23440 23450 0 23485 "2" 23446 23450 0 23485 "2" 23462 23450 . . "2" 23467 23450 . . "2" 23474 23450 . . "2" 23481 23450 . . "2" 23485 23450 . . end format %tdnn/dd/CCYY HA_Contact_Date_HAcontactsthro format %td close_date format %td last_before_march24
I tried this:
bysort PatientID:generate last_before_march=(_n==_N) if HA_Contact_Date_HAcontactsthro<23450
this:
bysort PatientID (HA_Contact_Date_HAcontactsthro):generate last_before_march24=HA_Contact_Date_HAcontactsthro[_N] if HA_Contact_Date_HAcontactsthro<23450
and this:
generate close_date=23450
bysort PatientID:generate last_before_march24=HA_Contact_Date_HAcontactsthro[_N] if HA_Contact_Date_HAcontactsthro< close_date
but they all repeat a similar error; when the variable HA_Contact_Date_HAcontactsthro is <23450 it gives me the final contact date by participant (or a 0, if I'm comparing to the close_date), and when it is not, it gives me a blank. What I *think* I want is for the variable "last_before_march24" to list the date that, for each person, was the last HA_Contact_Date_HAcontactsthro before 23450. But would love advice if there's a simpler way too. What should I be doing differently to determine which record is the last before the cutoff date?
Many thanks!
Ann Miller
Comment