Hello,
I've been creating new variables in my dataset of panel data to show total days of follow-up by month of follow-up. I used the following code to do this successfully:
by month: egen followup= total(days)
I also needed to know the number of facilities monitored, and added this variable using the nvals() function:
by month: egen facilities=nvals(facility)
My question arose when I was trying to add further variables displaying only information regarding intervention and controls, but to all observations in the month (rather than interventions showing missing in the control column and vice versa). I initially tried the following code, which yielded missing values for controls:
by month: egen followup_int= total(days) if interv_site==1
by month: egen facilities_int=nvals(facility) if interv_site==1
I discovered a useful workaround for the total() function in Stata's FAQ pages:
by month: egen followup_int= total(days*(interv_site==1))
I'm wondering (a) if there's another way to tell Stata to apply the total (found using the if statement) to all observations (by month) without sort of tricking it as above, and (b) if there is a way to make Stata similarly fill all cells in the new column with the number of intervention facilities monitored by month.
I'm sure there are multiple ways of working around this, but thought I would ask the experts in case there's a simple command or option I'm not aware of. Thank you!
Julia
I've been creating new variables in my dataset of panel data to show total days of follow-up by month of follow-up. I used the following code to do this successfully:
by month: egen followup= total(days)
I also needed to know the number of facilities monitored, and added this variable using the nvals() function:
by month: egen facilities=nvals(facility)
My question arose when I was trying to add further variables displaying only information regarding intervention and controls, but to all observations in the month (rather than interventions showing missing in the control column and vice versa). I initially tried the following code, which yielded missing values for controls:
by month: egen followup_int= total(days) if interv_site==1
by month: egen facilities_int=nvals(facility) if interv_site==1
I discovered a useful workaround for the total() function in Stata's FAQ pages:
by month: egen followup_int= total(days*(interv_site==1))
I'm wondering (a) if there's another way to tell Stata to apply the total (found using the if statement) to all observations (by month) without sort of tricking it as above, and (b) if there is a way to make Stata similarly fill all cells in the new column with the number of intervention facilities monitored by month.
I'm sure there are multiple ways of working around this, but thought I would ask the experts in case there's a simple command or option I'm not aware of. Thank you!
Julia
Comment