Dear all,
I would like to number observations within groups, however considering only observations that meet certain criteria.
See example and data excerpt below.
"identifier" is the group variable within which the observations should be numbered. The variables "cf1" and "cf2" are binary with values 0 or 1. Based on this data I would like to generate variables "cf1_seq" and "cf2_seq" which number the observations of values of 1 in the variables "cf1" and "cf2" respectively. Whenever the value of "cf1" or "cf2" is 0, this observation should not be considered in the numbering and marked as missing in the variables "cf1_seq" and "cf2_seq".
I am familiar with numbering observations by groups, e.g. with the following command
However, I am not sure how to number only those observations that meet certain criteria, such as taking a value of 1 in the example above.
I would very much appreciate your help, many thanks in advance!
I would like to number observations within groups, however considering only observations that meet certain criteria.
See example and data excerpt below.
"identifier" is the group variable within which the observations should be numbered. The variables "cf1" and "cf2" are binary with values 0 or 1. Based on this data I would like to generate variables "cf1_seq" and "cf2_seq" which number the observations of values of 1 in the variables "cf1" and "cf2" respectively. Whenever the value of "cf1" or "cf2" is 0, this observation should not be considered in the numbering and marked as missing in the variables "cf1_seq" and "cf2_seq".
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input int identifier byte(cf1 cf2 cf1_seq cf2_seq) 1000 0 1 . 1 1000 1 1 1 2 1000 1 0 2 . 1000 0 0 . . 1001 1 0 1 . 1001 1 1 2 1 1001 1 0 3 . 999 0 1 . 1 999 0 1 . 2 999 1 0 1 . 999 0 1 . 3 999 1 0 2 . end
I am familiar with numbering observations by groups, e.g. with the following command
Code:
forval j = 1/2 { bysort identifier: gen cf`j'_seq = [_n] }
I would very much appreciate your help, many thanks in advance!