Hi Statalist,
I hope your day is going well. I need some help with counting individuals in the dataset who meet certain criteria (marked as flag in the dataset). I created a hypothetical example to demonstrate what I'm trying to do.
Basically, I want to count the number of flags a person has on a monthly basis. The problem arises because some individuals may have the flag in some instances but not in others within the same month. Below are the data and code I've attempted so far:
After running this code, I successfully counted the flag for almost every individual except the 5th person. I have been trying to figure out how to include this last observation, but I'm out of ideas. I would appreciate any suggestions or ideas on how to modify the code to include the 5th person in this scenario. Please note that the 5th person is not the last person, and there is no consistent pattern for time and flag across individuals.
Thank you!
I hope your day is going well. I need some help with counting individuals in the dataset who meet certain criteria (marked as flag in the dataset). I created a hypothetical example to demonstrate what I'm trying to do.
Basically, I want to count the number of flags a person has on a monthly basis. The problem arises because some individuals may have the flag in some instances but not in others within the same month. Below are the data and code I've attempted so far:
Code:
*please install dataex clear all input float person float time byte flag 1 1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 2 1 0 2 1 1 2 2 1 2 3 0 2 4 0 3 1 0 3 2 0 3 3 0 3 4 1 4 1 0 4 1 1 4 2 0 4 3 0 4 4 0 4 5 0 4 6 0 5 1 0 5 2 0 5 3 0 5 4 0 5 5 0 5 6 0 5 6 1 end by person (flag time), sort: egen test = sum(flag & (time != time[_n - 1]))
Thank you!
Comment