Hi all, I am a new user to STATA and using a dataset with ~9000 observations and two main variables of interest: ir_no and ir_no_1. I am trying to count the number of times a given value of ir_no_1 appears for each ir_no. Here is a mock table of what the data looks like with the variable I am trying to make ("new_var").
I tried the following code, but something must be incorrect since it gives me the total number of observations per group:
Is there an easy way to do this? I looked around the forums for a quick solution, but have been unsuccessful. Thanks in advance for any advice.
ir_no | ir_no_1 | new_var |
111 | abc22 | 2 |
111 | abc22 | 2 |
111 | abc11 | 1 |
222 | abc33 | 2 |
222 | abc22 | 1 |
222 | abc33 | 2 |
Code:
gen calc = . ​foreach i in ir_no_1 { by ir_no: egen calc2 = count(ir_no_1) if ir_no_1 == `i' replace calc = calc2 if ir_no_1 == `i' drop calc2 }
Comment